Files
saas_backend/alembic/versions/74b6ccfaee8e_add_module_registry.py
T

158 lines
8.0 KiB
Python

"""add_module_registry
Revision ID: 74b6ccfaee8e
Revises: 8acd83604252
Create Date: 2026-01-20 15:11:19.596874
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '74b6ccfaee8e'
down_revision: Union[str, Sequence[str], None] = '8acd83604252'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('modules',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('module_id', sa.String(), nullable=False),
sa.Column('module_name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=True),
sa.Column('status', sa.String(), nullable=True),
sa.Column('icon_url', sa.String(), nullable=True),
sa.Column('display_order', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_modules_module_id'), 'modules', ['module_id'], unique=True)
op.create_table('module_environments',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('module_id', sa.UUID(), nullable=False),
sa.Column('slug', sa.String(), nullable=False),
sa.Column('frontend_base_url', sa.String(), nullable=False),
sa.Column('sso_entry_path', sa.String(), nullable=True),
sa.Column('backend_base_url', sa.String(), nullable=False),
sa.Column('sso_exchange_endpoint', sa.String(), nullable=True),
sa.Column('permission_sync_endpoint', sa.String(), nullable=True),
sa.Column('trust_type', sa.String(), nullable=False),
sa.Column('trust_credentials', sa.JSON(), nullable=False),
sa.Column('is_default', sa.Boolean(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('module_id', 'slug', name='uq_module_env_slug')
)
op.create_index(op.f('ix_module_environments_module_id'), 'module_environments', ['module_id'], unique=False)
op.create_index(op.f('ix_module_environments_slug'), 'module_environments', ['slug'], unique=False)
op.create_table('tenant_modules',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('module_id', sa.UUID(), nullable=False),
sa.Column('assigned_environment_slug', sa.String(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('activated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('deactivated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('plan_tier', sa.String(), nullable=True),
sa.Column('module_config', sa.JSON(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tenant_id', 'module_id', name='uq_tenant_module')
)
op.create_index(op.f('ix_tenant_modules_module_id'), 'tenant_modules', ['module_id'], unique=False)
op.create_index(op.f('ix_tenant_modules_tenant_id'), 'tenant_modules', ['tenant_id'], unique=False)
op.create_table('sso_grants',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('grant_code', sa.String(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('module_id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=True),
sa.Column('environment_slug', sa.String(), nullable=False),
sa.Column('redirect_url', sa.String(), nullable=False),
sa.Column('is_used', sa.Boolean(), nullable=True),
sa.Column('used_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_sso_grants_grant_code'), 'sso_grants', ['grant_code'], unique=True)
op.create_index(op.f('ix_sso_grants_module_id'), 'sso_grants', ['module_id'], unique=False)
op.create_index(op.f('ix_sso_grants_tenant_id'), 'sso_grants', ['tenant_id'], unique=False)
op.create_index(op.f('ix_sso_grants_user_id'), 'sso_grants', ['user_id'], unique=False)
# Add scope column as nullable first
op.add_column('accesses', sa.Column('scope', sa.String(), nullable=True))
op.add_column('accesses', sa.Column('module_id', sa.UUID(), nullable=True))
op.add_column('accesses', sa.Column('sync_checksum', sa.String(), nullable=True))
op.add_column('accesses', sa.Column('last_synced_at', sa.DateTime(timezone=True), nullable=True))
# Update existing rows with default scope
op.execute("UPDATE accesses SET scope = 'saas' WHERE scope IS NULL")
# Now make it not null
op.alter_column('accesses', 'scope', nullable=False)
op.create_index(op.f('ix_accesses_module_id'), 'accesses', ['module_id'], unique=False)
op.create_index(op.f('ix_accesses_scope'), 'accesses', ['scope'], unique=False)
op.create_foreign_key(None, 'accesses', 'modules', ['module_id'], ['id'])
# Inspect to see if constraint/column exists to avoid transaction abortion on failure
bind = op.get_bind()
inspector = sa.inspect(bind)
# Check and drop foreign key
fks = inspector.get_foreign_keys('users')
if any(fk['name'] == 'users_palette_id_fkey' for fk in fks):
op.drop_constraint('users_palette_id_fkey', 'users', type_='foreignkey')
# Check and drop column
columns = [c['name'] for c in inspector.get_columns('users')]
if 'palette_id' in columns:
op.drop_column('users', 'palette_id')
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('palette_id', sa.UUID(), autoincrement=False, nullable=True))
op.create_foreign_key(op.f('users_palette_id_fkey'), 'users', 'color_palettes', ['palette_id'], ['id'])
op.drop_constraint(None, 'accesses', type_='foreignkey')
op.drop_index(op.f('ix_accesses_scope'), table_name='accesses')
op.drop_index(op.f('ix_accesses_module_id'), table_name='accesses')
op.drop_column('accesses', 'last_synced_at')
op.drop_column('accesses', 'sync_checksum')
op.drop_column('accesses', 'module_id')
op.drop_column('accesses', 'scope')
op.drop_index(op.f('ix_sso_grants_user_id'), table_name='sso_grants')
op.drop_index(op.f('ix_sso_grants_tenant_id'), table_name='sso_grants')
op.drop_index(op.f('ix_sso_grants_module_id'), table_name='sso_grants')
op.drop_index(op.f('ix_sso_grants_grant_code'), table_name='sso_grants')
op.drop_table('sso_grants')
op.drop_index(op.f('ix_tenant_modules_tenant_id'), table_name='tenant_modules')
op.drop_index(op.f('ix_tenant_modules_module_id'), table_name='tenant_modules')
op.drop_table('tenant_modules')
op.drop_index(op.f('ix_module_environments_slug'), table_name='module_environments')
op.drop_index(op.f('ix_module_environments_module_id'), table_name='module_environments')
op.drop_table('module_environments')
op.drop_index(op.f('ix_modules_module_id'), table_name='modules')
op.drop_table('modules')
# ### end Alembic commands ###