Subir todos los cambios recientes a la rama principal
This commit is contained in:
65
backend/alembic/env.py
Normal file
65
backend/alembic/env.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
from alembic import context
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import sys
|
||||
import os
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
|
||||
import asyncio
|
||||
|
||||
# Cargar variables de entorno desde .env
|
||||
load_dotenv()
|
||||
|
||||
# Agregar el directorio 'backend' al PYTHONPATH
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
# Configuración de Alembic
|
||||
config = context.config
|
||||
|
||||
# Configurar logging
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Agregar la URL de la base de datos desde las variables de entorno
|
||||
config.set_main_option(
|
||||
"sqlalchemy.url",
|
||||
os.getenv("DATABASE_URL", "postgresql+asyncpg://user:password@localhost/dbname")
|
||||
)
|
||||
|
||||
# Importar modelos para las migraciones
|
||||
from app.models import * # Importa todos los modelos aquí
|
||||
from app.core.database import Base
|
||||
|
||||
target_metadata = Base.metadata # Reemplaza con tu metadata
|
||||
|
||||
# Cambiar el motor a asíncrono para Alembic
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
|
||||
|
||||
# Crear el motor asíncrono
|
||||
connectable = create_async_engine(
|
||||
os.getenv("DATABASE_URL", "postgresql+asyncpg://user:password@localhost/dbname"),
|
||||
echo=True, # Habilitar logs para depuración
|
||||
)
|
||||
|
||||
async def run_migrations():
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
def do_run_migrations(connection):
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
# Configurar migraciones en modo offline
|
||||
def run_migrations_offline():
|
||||
context.configure(url=os.getenv("DATABASE_URL"), target_metadata=target_metadata, literal_binds=True)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
asyncio.run(run_migrations())
|
||||
28
backend/alembic/versions/add_clients_table.py
Normal file
28
backend/alembic/versions/add_clients_table.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Add clients table
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "add_clients_table"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
inspector = inspect(bind)
|
||||
if 'clients' not in inspector.get_table_names():
|
||||
op.create_table(
|
||||
'clients',
|
||||
sa.Column('id', sa.Integer, primary_key=True, autoincrement=True),
|
||||
sa.Column('name', sa.String, nullable=False),
|
||||
sa.Column('email', sa.String, nullable=False, unique=True),
|
||||
sa.Column('phone', sa.String, nullable=False),
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('clients')
|
||||
20
backend/alembic/versions/add_system_id_to_tickets.py
Normal file
20
backend/alembic/versions/add_system_id_to_tickets.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
Agregar columna system_id a la tabla tickets
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# Revisión ID y dependencias
|
||||
revision = 'add_system_id_to_tickets'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
"""Agregar columna system_id a la tabla tickets."""
|
||||
op.add_column('tickets', sa.Column('system_id', sa.UUID, nullable=True))
|
||||
|
||||
def downgrade():
|
||||
"""Eliminar columna system_id de la tabla tickets."""
|
||||
op.drop_column('tickets', 'system_id')
|
||||
18
backend/alembic/versions/add_updated_at_to_category.py
Normal file
18
backend/alembic/versions/add_updated_at_to_category.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""
|
||||
Add updated_at column to ticket_categories
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "add_updated_at_to_category"
|
||||
down_revision = "add_clients_table"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
op.add_column("ticket_categories", sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True))
|
||||
|
||||
def downgrade():
|
||||
op.drop_column("ticket_categories", "updated_at")
|
||||
Reference in New Issue
Block a user