Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions src/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,13 @@ class DatabaseSettings(BaseSettings):
pass


class SQLiteSettings(DatabaseSettings):
SQLITE_URI: str = "./sql_app.db"
SQLITE_SYNC_PREFIX: str = "sqlite:///"
SQLITE_ASYNC_PREFIX: str = "sqlite+aiosqlite:///"


class MySQLSettings(DatabaseSettings):
MYSQL_USER: str = "username"
MYSQL_PASSWORD: str = "password"
MYSQL_SERVER: str = "localhost"
MYSQL_PORT: int = 5432
MYSQL_DB: str = "dbname"
MYSQL_SYNC_PREFIX: str = "mysql://"
MYSQL_ASYNC_PREFIX: str = "mysql+aiomysql://"
MYSQL_URL: str | None = None

@computed_field # type: ignore[prop-decorator]
@property
def MYSQL_URI(self) -> str:
credentials = f"{self.MYSQL_USER}:{self.MYSQL_PASSWORD}"
location = f"{self.MYSQL_SERVER}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
return f"{credentials}@{location}"


class PostgresSettings(DatabaseSettings):
POSTGRES_USER: str = "postgres"
POSTGRES_PASSWORD: str = "postgres"
POSTGRES_SERVER: str = "localhost"
POSTGRES_PORT: int = 5432
POSTGRES_DB: str = "postgres"
POSTGRES_SYNC_PREFIX: str = "postgresql://"
POSTGRES_ASYNC_PREFIX: str = "postgresql+asyncpg://"
POSTGRES_URL: str | None = None

@computed_field # type: ignore[prop-decorator]
@property
Expand All @@ -66,6 +40,11 @@ def POSTGRES_URI(self) -> str:
location = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}"
return f"{credentials}@{location}"

@computed_field # type: ignore[prop-decorator]
@property
def POSTGRES_URL(self) -> str:
return f"{self.POSTGRES_ASYNC_PREFIX}{self.POSTGRES_URI}"


class FirstUserSettings(BaseSettings):
ADMIN_NAME: str = "admin"
Expand Down Expand Up @@ -151,7 +130,6 @@ class CORSSettings(BaseSettings):

class Settings(
AppSettings,
SQLiteSettings,
PostgresSettings,
CryptSettings,
FirstUserSettings,
Expand Down
4 changes: 1 addition & 3 deletions src/app/core/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class Base(DeclarativeBase, MappedAsDataclass):
pass


DATABASE_URI = settings.POSTGRES_URI
DATABASE_PREFIX = settings.POSTGRES_ASYNC_PREFIX
DATABASE_URL = f"{DATABASE_PREFIX}{DATABASE_URI}"
DATABASE_URL = settings.POSTGRES_URL


async_engine = create_async_engine(DATABASE_URL, echo=False, future=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from src.app.core.config import settings
from src.app.main import app

DATABASE_URI = settings.POSTGRES_URI
DATABASE_PREFIX = settings.POSTGRES_SYNC_PREFIX
DATABASE_URL = settings.POSTGRES_URL

sync_engine = create_engine(DATABASE_PREFIX + DATABASE_URI)

sync_engine = create_engine(DATABASE_URL)
local_session = sessionmaker(autocommit=False, autoflush=False, bind=sync_engine)


Expand Down