mirror of
https://github.com/x1xhlol/system-prompts-and-models-of-ai-tools.git
synced 2026-06-18 07:19:35 +00:00
62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from logging.config import fileConfig
|
|
|
|
from sqlalchemy import engine_from_config, pool
|
|
from alembic import context
|
|
|
|
_backend = Path(__file__).resolve().parents[1]
|
|
if str(_backend) not in sys.path:
|
|
sys.path.insert(0, str(_backend))
|
|
|
|
from app.database import Base
|
|
import app.models # noqa: F401 — register all models on Base.metadata
|
|
|
|
config = context.config
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def get_sync_url() -> str:
|
|
url = os.environ.get("DATABASE_URL", "")
|
|
if not url:
|
|
url = config.get_main_option("sqlalchemy.url", "")
|
|
if "+asyncpg" in url:
|
|
url = url.replace("postgresql+asyncpg", "postgresql", 1)
|
|
return url
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
url = get_sync_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:
|
|
cfg = config.get_section(config.config_ini_section) or {}
|
|
cfg["sqlalchemy.url"] = get_sync_url()
|
|
connectable = engine_from_config(
|
|
cfg,
|
|
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()
|