Alembix fixes
This commit is contained in:
@@ -27,27 +27,39 @@ def upgrade() -> None:
|
||||
"""
|
||||
|
||||
# Modify existing journal table to support all entity types
|
||||
# First add new columns
|
||||
op.add_column('journal', sa.Column('entity_type', sa.String(50), nullable=True))
|
||||
op.add_column('journal', sa.Column('entity_id', sa.BigInteger(), nullable=True))
|
||||
# First add new columns (check if they don't already exist)
|
||||
from sqlalchemy import inspect, text
|
||||
from alembic import op
|
||||
|
||||
# Get table columns to check if entity_type and entity_id already exist
|
||||
connection = op.get_context().bind
|
||||
inspector = inspect(connection)
|
||||
columns = [col['name'] for col in inspector.get_columns('journal')]
|
||||
|
||||
if 'entity_type' not in columns:
|
||||
op.add_column('journal', sa.Column('entity_type', sa.String(50), nullable=True))
|
||||
if 'entity_id' not in columns:
|
||||
op.add_column('journal', sa.Column('entity_id', sa.BigInteger(), nullable=True))
|
||||
|
||||
# Migrate existing PPR journal entries: backfill entity_type and entity_id
|
||||
op.execute("""
|
||||
UPDATE journal SET
|
||||
entity_type = 'PPR',
|
||||
entity_id = ppr_id
|
||||
WHERE entity_type IS NULL
|
||||
WHERE entity_type IS NULL AND ppr_id IS NOT NULL
|
||||
""")
|
||||
|
||||
# Make new columns NOT NULL after migration
|
||||
op.alter_column('journal', 'entity_type', nullable=False)
|
||||
op.alter_column('journal', 'entity_id', nullable=False)
|
||||
op.alter_column('journal', 'entity_type', existing_type=sa.String(50), nullable=False)
|
||||
op.alter_column('journal', 'entity_id', existing_type=sa.BigInteger(), nullable=False)
|
||||
|
||||
# Make ip column nullable (new entries won't always have it)
|
||||
op.alter_column('journal', 'ip', existing_type=sa.String(45), nullable=True)
|
||||
|
||||
# Drop the old ppr_id column
|
||||
op.drop_column('journal', 'ppr_id')
|
||||
# Drop the foreign key constraint before dropping the column
|
||||
if 'ppr_id' in columns:
|
||||
op.drop_constraint('journal_ibfk_1', 'journal', type_='foreignkey')
|
||||
op.drop_column('journal', 'ppr_id')
|
||||
|
||||
# Add composite index for efficient queries
|
||||
op.create_index('idx_entity_lookup', 'journal', ['entity_type', 'entity_id'])
|
||||
|
||||
Reference in New Issue
Block a user