Nuevos cambios en el servidor
This commit is contained in:
74
backend/migrations/env.py
Normal file
74
backend/migrations/env.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from logging.config import fileConfig
|
||||
import os
|
||||
from sqlalchemy import create_engine, pool
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
|
||||
from sqlalchemy.engine import engine_from_config
|
||||
from alembic import context
|
||||
import asyncio
|
||||
|
||||
# Import Base and all models
|
||||
from app.core.database import Base
|
||||
from app.models import tenant # Import all models explicitly
|
||||
|
||||
# Alembic Config object
|
||||
config = context.config
|
||||
|
||||
# Logging configuration
|
||||
if config.config_file_name:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Get DATABASE_URL and convert to synchronous
|
||||
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||
if not DATABASE_URL:
|
||||
raise RuntimeError("DATABASE_URL environment variable is not set")
|
||||
|
||||
SYNC_DATABASE_URL = DATABASE_URL.replace("+asyncpg", "")
|
||||
|
||||
# Metadata for autogenerate
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""
|
||||
Run migrations in 'offline' mode.
|
||||
"""
|
||||
context.configure(
|
||||
url=SYNC_DATABASE_URL,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_migrations_online():
|
||||
"""
|
||||
Run migrations in 'online' mode.
|
||||
"""
|
||||
# Fetch the URL from Alembic configuration
|
||||
alembic_config = config.get_section(config.config_ini_section)
|
||||
alembic_config["sqlalchemy.url"] = DATABASE_URL # Use async URL
|
||||
|
||||
connectable: AsyncEngine = create_async_engine(
|
||||
DATABASE_URL,
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(
|
||||
lambda sync_connection: context.configure(
|
||||
connection=sync_connection, target_metadata=target_metadata,
|
||||
compare_type=True # Ensure column types are compared
|
||||
)
|
||||
)
|
||||
await connection.run_sync(context.run_migrations())
|
||||
|
||||
asyncio.run(do_run_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
Reference in New Issue
Block a user