49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
from logging.config import fileConfig
|
|
from sqlalchemy import engine_from_config
|
|
from sqlalchemy import pool
|
|
from alembic import context
|
|
import sys
|
|
import os
|
|
|
|
# Add project root to Python path
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
from core.database import Base, SQLALCHEMY_DATABASE_URL
|
|
from models.user import User # Import all models
|
|
|
|
config = context.config
|
|
fileConfig(config.config_file_name)
|
|
config.set_main_option('sqlalchemy.url', SQLALCHEMY_DATABASE_URL)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
def run_migrations_offline():
|
|
context.configure(
|
|
url=SQLALCHEMY_DATABASE_URL,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
def run_migrations_online():
|
|
connectable = engine_from_config(
|
|
{"sqlalchemy.url": SQLALCHEMY_DATABASE_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()
|