Skip to content

Commit

Permalink
feat: added in env.py the way to get setted sqlalchemy.url
Browse files Browse the repository at this point in the history
  • Loading branch information
SteBaum committed Jun 10, 2024
1 parent c182d18 commit 0be76bf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
2 changes: 1 addition & 1 deletion alembic_migration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To use the databases, set the following environment variables:

```sh
TDP_ALEMBIC_SQLITE_DSN=sqlite:///sqlite.db
TDP_ALEMBIC_POSGRESQL_DSN=postgresql://postgres:postgres@localhost:5432/tdp
TDP_ALEMBIC_POSTGRESQL_DSN=postgresql://postgres:postgres@localhost:5432/tdp
TDP_ALEMBIC_MYSQL_DSN=mysql+pymysql://mysql:mysql@localhost:3306/tdp
```

Expand Down
38 changes: 27 additions & 11 deletions alembic_migration/mysql/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
# SPDX-License-Identifier: Apache-2.0

import os
from pathlib import Path

from alembic import context
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy import engine_from_config, pool

from tdp.core.models.base_model import BaseModel

Expand All @@ -18,6 +16,18 @@
target_metadata = BaseModel.metadata


def load_dotenv():
"""Load the .env file based on the TDP_ENV environment variable."""
import os
from pathlib import Path

from dotenv import load_dotenv

env_path = Path(os.environ.get("TDP_ENV", ".env"))
if env_path.exists:
load_dotenv(env_path)


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode. Not implemented."""
raise NotImplementedError()
Expand All @@ -26,17 +36,23 @@ def run_migrations_offline() -> None:
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""

env_path = Path(os.environ.get("TDP_ENV", ".env"))
if env_path.exists:
load_dotenv(env_path)

database_dsn = os.environ.get("TDP_ALEMBIC_MYSQL_DSN")
# Get the configuration based on the current section name
engine_config = config.get_section(config.config_ini_section, {})

if database_dsn is None:
raise ValueError("TDP_ALEMBIC_MYSQL_DSN env var is missing")
# Override the database URL with the one from the environment if not set
if not "sqlalchemy.url" in engine_config:
load_dotenv()
if not (database_dsn := os.environ.get("TDP_ALEMBIC_MYSQL_DSN")):
raise ValueError("TDP_ALEMBIC_MYSQL_DSN env var is missing")
engine_config["sqlalchemy.url"] = database_dsn

connectable = create_engine(database_dsn)
connectable = engine_from_config(
engine_config,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

# Ensure that the provided database url is of the proper dialect
if connectable.dialect.name != "mysql":
raise ValueError("You are not connected to a MySQL or MariaDB database")

Expand Down
37 changes: 27 additions & 10 deletions alembic_migration/postgresql/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

import os
from pathlib import Path

import alembic_postgresql_enum # noqa: F401
from alembic import context
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy import engine_from_config, pool

from tdp.core.models.base_model import BaseModel

Expand All @@ -19,24 +17,43 @@
target_metadata = BaseModel.metadata


def load_dotenv():
"""Load the .env file based on the TDP_ENV environment variable."""
import os
from pathlib import Path

from dotenv import load_dotenv

env_path = Path(os.environ.get("TDP_ENV", ".env"))
if env_path.exists:
load_dotenv(env_path)


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode. Not implemented."""
raise NotImplementedError()


def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
env_path = Path(os.environ.get("TDP_ENV", ".env"))
if env_path.exists:
load_dotenv(env_path)

database_dsn = os.environ.get("TDP_ALEMBIC_POSGRESQL_DSN")
# Get the configuration based on the current section name
engine_config = config.get_section(config.config_ini_section, {})

if database_dsn is None:
raise ValueError("TDP_ALEMBIC_POSGRESQL_DSN env var is missing")
# Override the database URL with the one from the environment if not set
if not "sqlalchemy.url" in engine_config:
load_dotenv()
if not (database_dsn := os.environ.get("TDP_ALEMBIC_POSTGRESQL_DSN")):
raise ValueError("TDP_ALEMBIC_POSTGRESQL_DSN env var is missing")
engine_config["sqlalchemy.url"] = database_dsn

connectable = create_engine(database_dsn)
connectable = engine_from_config(
engine_config,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

# Ensure that the provided database url is of the proper dialect
if connectable.dialect.name != "postgresql":
raise ValueError("You are not connected to a PostgreSQL database")

Expand Down
38 changes: 27 additions & 11 deletions alembic_migration/sqlite/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
# SPDX-License-Identifier: Apache-2.0

import os
from pathlib import Path

from alembic import context
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy import engine_from_config, pool

from tdp.core.models.base_model import BaseModel

Expand All @@ -18,6 +16,18 @@
target_metadata = BaseModel.metadata


def load_dotenv():
"""Load the .env file based on the TDP_ENV environment variable."""
import os
from pathlib import Path

from dotenv import load_dotenv

env_path = Path(os.environ.get("TDP_ENV", ".env"))
if env_path.exists:
load_dotenv(env_path)


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode. Not implemented."""
raise NotImplementedError()
Expand All @@ -26,17 +36,23 @@ def run_migrations_offline() -> None:
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""

env_path = Path(os.environ.get("TDP_ENV", ".env"))
if env_path.exists:
load_dotenv(env_path)

database_dsn = os.environ.get("TDP_ALEMBIC_SQLITE_DSN")
# Get the configuration based on the current section name
engine_config = config.get_section(config.config_ini_section, {})

if database_dsn is None:
raise ValueError("TDP_ALEMBIC_SQLITE_DSN env var is missing")
# Override the database URL with the one from the environment if not set
if not "sqlalchemy.url" in engine_config:
load_dotenv()
if not (database_dsn := os.environ.get("TDP_ALEMBIC_SQLITE_DSN")):
raise ValueError("TDP_ALEMBIC_SQLITE_DSN env var is missing")
engine_config["sqlalchemy.url"] = database_dsn

connectable = create_engine(database_dsn)
connectable = engine_from_config(
engine_config,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

# Ensure that the provided database url is of the proper dialect
if connectable.dialect.name != "sqlite":
raise ValueError("You are not connected to an SQLite database")

Expand Down

0 comments on commit 0be76bf

Please sign in to comment.