mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
Replace manual if-sqlite/if-postgres branching with Alembic: - Add alembic dependency - Create programmatic alembic env (no CLI/alembic.ini needed) - Support async engines via run_sync passthrough - render_as_batch=True for SQLite ALTER TABLE compatibility - Auto-stamp baseline on first run (existing DB at version 25) - Run alembic upgrade head after legacy migrations - Include sample migration showing schema + data migration patterns - Add alembic dir to package-data for distribution
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
"""Alembic environment for LangBot.
|
|
|
|
This env.py is designed to be called programmatically (not via CLI).
|
|
It supports both SQLite and PostgreSQL.
|
|
|
|
The sync connection is passed via config attributes by the runner.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from alembic import context
|
|
from sqlalchemy.engine import Connection
|
|
|
|
from langbot.pkg.entity.persistence.base import Base
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""Run migrations in 'offline' mode — emit SQL without a live connection."""
|
|
url = context.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:
|
|
"""Run migrations with a live sync connection passed via config attributes."""
|
|
connection: Connection = context.config.attributes.get('connection')
|
|
if connection is None:
|
|
raise RuntimeError('connection not provided in alembic config attributes')
|
|
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
# render_as_batch=True is critical for SQLite ALTER TABLE support
|
|
render_as_batch=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|