Add alembic\env.py
Browse files- alembic//env.py +89 -0
alembic//env.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Romeo AI Research Assistant - Alembic Environment
|
| 2 |
+
# Database migration environment configuration for SQLite (HF Storage)
|
| 3 |
+
# Transitioned from Oracle to SQLite: 2026-03-15
|
| 4 |
+
|
| 5 |
+
import asyncio
|
| 6 |
+
from logging.config import fileConfig
|
| 7 |
+
from sqlalchemy import pool
|
| 8 |
+
from sqlalchemy.engine import Connection
|
| 9 |
+
from sqlalchemy.ext.asyncio import async_engine_from_config
|
| 10 |
+
from alembic import context
|
| 11 |
+
|
| 12 |
+
# Import application modules
|
| 13 |
+
import sys
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
sys.path.append(str(Path(__file__).parent.parent))
|
| 16 |
+
|
| 17 |
+
from app.core.config import settings
|
| 18 |
+
from app.models.base import Base
|
| 19 |
+
|
| 20 |
+
# Direct imports for each model to ensure Alembic detects them
|
| 21 |
+
from app.models.user import User
|
| 22 |
+
from app.models.paper import Paper
|
| 23 |
+
from app.models.library import LibraryItem
|
| 24 |
+
from app.models.seed import Seed
|
| 25 |
+
from app.models.extraction import Extraction
|
| 26 |
+
from app.models.proposal import Proposal
|
| 27 |
+
from app.models.data import Dataset
|
| 28 |
+
from app.models.writesage import Manuscript, ManuscriptSection
|
| 29 |
+
|
| 30 |
+
# This is the Alembic Config object
|
| 31 |
+
config = context.config
|
| 32 |
+
|
| 33 |
+
# 🔥 Force Alembic to use the SQLite URL from your config.py
|
| 34 |
+
# This ensures it looks at ./data/romeo_research.db
|
| 35 |
+
config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URI)
|
| 36 |
+
|
| 37 |
+
if config.config_file_name is not None:
|
| 38 |
+
fileConfig(config.config_file_name)
|
| 39 |
+
|
| 40 |
+
target_metadata = Base.metadata
|
| 41 |
+
|
| 42 |
+
def run_migrations_offline() -> None:
|
| 43 |
+
"""Run migrations in 'offline' mode."""
|
| 44 |
+
url = config.get_main_option("sqlalchemy.url")
|
| 45 |
+
context.configure(
|
| 46 |
+
url=url,
|
| 47 |
+
target_metadata=target_metadata,
|
| 48 |
+
literal_binds=True,
|
| 49 |
+
dialect_opts={"paramstyle": "named"},
|
| 50 |
+
# 🔥 REQUIRED FOR SQLITE: Allows table alterations by rebuilding tables
|
| 51 |
+
render_as_batch=True,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
with context.begin_transaction():
|
| 55 |
+
context.run_migrations()
|
| 56 |
+
|
| 57 |
+
def do_run_migrations(connection: Connection) -> None:
|
| 58 |
+
"""Configure migration context for online mode."""
|
| 59 |
+
context.configure(
|
| 60 |
+
connection=connection,
|
| 61 |
+
target_metadata=target_metadata,
|
| 62 |
+
# 🔥 REQUIRED FOR SQLITE: Allows table alterations by rebuilding tables
|
| 63 |
+
render_as_batch=True,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
with context.begin_transaction():
|
| 67 |
+
context.run_migrations()
|
| 68 |
+
|
| 69 |
+
async def run_async_migrations() -> None:
|
| 70 |
+
"""In this scenario we need to create an Engine and associate a connection with the context."""
|
| 71 |
+
connectable = async_engine_from_config(
|
| 72 |
+
config.get_section(config.config_ini_section, {}),
|
| 73 |
+
prefix="sqlalchemy.",
|
| 74 |
+
poolclass=pool.NullPool,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
async with connectable.connect() as connection:
|
| 78 |
+
await connection.run_sync(do_run_migrations)
|
| 79 |
+
|
| 80 |
+
await connectable.dispose()
|
| 81 |
+
|
| 82 |
+
def run_migrations_online() -> None:
|
| 83 |
+
"""Run migrations in 'online' mode."""
|
| 84 |
+
asyncio.run(run_async_migrations())
|
| 85 |
+
|
| 86 |
+
if context.is_offline_mode():
|
| 87 |
+
run_migrations_offline()
|
| 88 |
+
else:
|
| 89 |
+
run_migrations_online()
|