mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-07-30 15:26:14 +00:00
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
import asyncio
|
|
from logging.config import fileConfig
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
from alembic import context
|
|
from sqlmodel import SQLModel
|
|
import importlib.util
|
|
import pathlib
|
|
|
|
db_path = pathlib.Path(__file__).resolve().parents[1] / "router" / "db.py"
|
|
spec = importlib.util.spec_from_file_location("db", db_path)
|
|
db = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(db)
|
|
|
|
DATABASE_URL = getattr(db, "DATABASE_URL", "sqlite+aiosqlite:///keys.db")
|
|
|
|
config = context.config
|
|
fileConfig(config.config_file_name)
|
|
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
|
|
|
target_metadata = SQLModel.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=DATABASE_URL,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run_migrations(connection):
|
|
context.configure(connection=connection, target_metadata=target_metadata, compare_type=True)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_migrations_online() -> None:
|
|
connectable = create_async_engine(DATABASE_URL, poolclass=pool.NullPool)
|
|
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
|
|
await connectable.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
asyncio.run(run_migrations_online())
|