56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
from __future__ import annotations
|
|
import os
|
|
import sys
|
|
from logging.config import fileConfig
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
# Add ./src to sys.path
|
|
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
SRC_DIR = os.path.join(BASE_DIR, "src")
|
|
if SRC_DIR not in sys.path:
|
|
sys.path.append(SRC_DIR)
|
|
|
|
from app.db.session import Base # noqa
|
|
|
|
config = context.config
|
|
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
DATABASE_URL = os.getenv("DATABASE_URL")
|
|
|
|
def run_migrations_offline() -> None:
|
|
url = DATABASE_URL
|
|
if not url:
|
|
raise RuntimeError("DATABASE_URL is not set")
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
def run_migrations_online() -> None:
|
|
url = DATABASE_URL
|
|
if not url:
|
|
raise RuntimeError("DATABASE_URL is not set")
|
|
connectable = engine_from_config(
|
|
{"sqlalchemy.url": url},
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
with connectable.connect() as connection:
|
|
context.configure(connection=connection, target_metadata=target_metadata, compare_type=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|