63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
from logging.config import fileConfig
|
|
import os
|
|
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
from app.database import Base
|
|
from app.models.block_config import BlockConfig
|
|
from app.models.template import Template
|
|
from app.models.template_block import TemplateBlock
|
|
|
|
|
|
config = context.config
|
|
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def get_database_url() -> str:
|
|
db_host = os.getenv("DB_HOST", "localhost")
|
|
db_port = os.getenv("DB_PORT", "3306")
|
|
db_user = os.getenv("DB_USER", "root")
|
|
db_pass = os.getenv("DB_PASS", "root123")
|
|
db_name = os.getenv("DB_NAME", "ai_doc_template")
|
|
return f"mysql+pymysql://{db_user}:{db_pass}@{db_host}:{db_port}/{db_name}?charset=utf8mb4"
|
|
|
|
|
|
config.set_main_option("sqlalchemy.url", get_database_url())
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=get_database_url(),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
connectable = engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|