Versión 1.3.0: Conexion completa del proyecto
This commit is contained in:
68
backend/migrations/env.py
Normal file
68
backend/migrations/env.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from logging.config import fileConfig
|
||||
import os
|
||||
from sqlalchemy import create_engine, pool
|
||||
from sqlalchemy.engine import engine_from_config
|
||||
from alembic import context
|
||||
|
||||
# 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()
|
||||
|
||||
|
||||
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"] = SYNC_DATABASE_URL
|
||||
|
||||
connectable = engine_from_config(
|
||||
alembic_config,
|
||||
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()
|
||||
Reference in New Issue
Block a user