Skip to content

Commit

Permalink
refactoring: databases
Browse files Browse the repository at this point in the history
  • Loading branch information
NotBupyc committed Sep 26, 2024
1 parent 88a6daf commit 2398f06
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
4 changes: 0 additions & 4 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ sqlalchemy.url = %(DB_URL)s
# black.options = -l 79 REVISION_SCRIPT_FILENAME

# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
hooks = ruff

ruff.type = exec
ruff.executable = ruff
ruff.options = --fix REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
Expand Down
3 changes: 2 additions & 1 deletion bot/database/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine

from bot.database.repos import Repositories
from bot.enums.db import Databases

from bot.settings import settings

logger = logging.getLogger("Database")

URL = settings.db.build_postgres_url() if settings.db.used == "PostgreSQL" else settings.db.build_mysql_url()
URL = settings.db.build_postgres_url() if settings.db.used == Databases.PostgreSQl else settings.db.build_mysql_url()


engine = create_async_engine(URL, future=True, poolclass=NullPool, echo=settings.debug_mode)
Expand Down
16 changes: 16 additions & 0 deletions bot/enums/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from enum import StrEnum, IntEnum


class Databases(StrEnum):
PostgreSQl = "PostgreSQL"
MySQL = "MySQL"


class PostgreSQLDrivers(StrEnum):
SYNC_DRIVER = "psycopg2"
ASYNC_DRIVER = "asyncpg"


class MySQLDrivers(StrEnum):
SYNC_DRIVER = "pymysql"
ASYNC_DRIVER = "asyncmy"
13 changes: 10 additions & 3 deletions bot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from redis.asyncio import Redis

from bot.enums.db import Databases, PostgreSQLDrivers, MySQLDrivers

load_dotenv(find_dotenv())

Expand All @@ -24,7 +25,7 @@ class BotSettings(BaseSettings):


class DatabaseSettings(BaseSettings):
used: Literal["PostgreSQL", "MySQL"] = "PostgreSQL"
used: Databases = Databases.PostgreSQl
ip: str
user: str
password: str
Expand All @@ -34,10 +35,16 @@ class DatabaseSettings(BaseSettings):

model_config = SettingsConfigDict(env_prefix="DB_")

def build_postgres_url(self, driver: Literal["psycopg2", "asyncpg"] = "asyncpg") -> str:
def build_postgres_url(
self,
driver: PostgreSQLDrivers = PostgreSQLDrivers.ASYNC_DRIVER
) -> str:
return f"postgresql+{driver}://" f"{self.user}:{self.password}" f"@{self.ip}/{self.name}"

def build_mysql_url(self, driver: Literal["pymysql", "asyncmy"] = "asyncmy") -> str:
def build_mysql_url(
self,
driver: MySQLDrivers = MySQLDrivers.SYNC_DRIVER
) -> str:
return f"mysql+{driver}://" f"{self.user}:{self.password}" f"@{self.ip}/{self.name}"


Expand Down
10 changes: 4 additions & 6 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from alembic import context
from sqlalchemy import engine_from_config, pool


from bot.enums.db import Databases, PostgreSQLDrivers, MySQLDrivers
from bot.settings import settings

# this is the Alembic Config object, which provides
Expand All @@ -14,22 +14,20 @@
config = context.config



# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# Set sqlalchemy.url
if settings.db.used == "PostgreSQL":
url = settings.db.build_postgres_url(driver="psycopg2")
if settings.db.used == Databases.PostgreSQl:
url = settings.db.build_postgres_url(driver=PostgreSQLDrivers.SYNC_DRIVER)
else:
url = settings.db.build_mysql_url(driver="pymysql")
url = settings.db.build_mysql_url(driver=MySQLDrivers.SYNC_DRIVER)

config.set_main_option(
"sqlalchemy.url",
url

)

# add your model's MetaData object here
Expand Down
Empty file added migrations/versions/.gitkeep
Empty file.

0 comments on commit 2398f06

Please sign in to comment.