Files
post_bot/alembic/env.py
Choi A.K. c6104455d8
Some checks failed
continuous-integration/drone/push Build is failing
db creation fix
2025-09-06 09:09:07 +09:00

58 lines
1.7 KiB
Python

from logging.config import fileConfig
from sqlalchemy import create_engine, pool
from alembic import context
import os
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
import os
db_url = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///db/bot.db")
db_url_sync = db_url.replace("sqlite+aiosqlite", "sqlite") # Alembic нужен sync-драйвер
config.set_main_option("sqlalchemy.url", db_url_sync)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
from models import Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
url = os.getenv("DATABASE_URL", config.get_main_option("sqlalchemy.url"))
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
url = config.get_main_option("sqlalchemy.url")
connectable = create_engine(url, 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()