"""Add map sharing tables Revision ID: a1b2c3d4e5f6 Revises: 915e5889d6d7 Create Date: 2025-12-12 16:00:00.000000 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. revision = 'a1b2c3d4e5f6' down_revision = '915e5889d6d7' branch_labels = None depends_on = None def upgrade() -> None: # Create enum for share permissions if it doesn't exist op.execute(""" DO $$ BEGIN CREATE TYPE sharepermission AS ENUM ('read', 'edit'); EXCEPTION WHEN duplicate_object THEN null; END $$; """) # Create map_shares table op.create_table('map_shares', sa.Column('id', sa.UUID(), nullable=False), sa.Column('map_id', sa.UUID(), nullable=False), sa.Column('user_id', sa.UUID(), nullable=False), sa.Column('permission', postgresql.ENUM('read', 'edit', name='sharepermission', create_type=False), nullable=False), sa.Column('shared_by', sa.UUID(), nullable=True), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.ForeignKeyConstraint(['map_id'], ['maps.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['shared_by'], ['users.id'], ondelete='SET NULL'), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_map_shares_map_id'), 'map_shares', ['map_id'], unique=False) op.create_index(op.f('ix_map_shares_user_id'), 'map_shares', ['user_id'], unique=False) # Create map_share_links table op.create_table('map_share_links', sa.Column('id', sa.UUID(), nullable=False), sa.Column('map_id', sa.UUID(), nullable=False), sa.Column('token', sa.String(length=64), nullable=False), sa.Column('permission', postgresql.ENUM('read', 'edit', name='sharepermission', create_type=False), nullable=False), sa.Column('is_active', sa.Boolean(), nullable=False), sa.Column('created_by', sa.UUID(), nullable=True), sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.ForeignKeyConstraint(['map_id'], ['maps.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['created_by'], ['users.id'], ondelete='SET NULL'), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_map_share_links_map_id'), 'map_share_links', ['map_id'], unique=False) op.create_index(op.f('ix_map_share_links_token'), 'map_share_links', ['token'], unique=True) def downgrade() -> None: op.drop_index(op.f('ix_map_share_links_token'), table_name='map_share_links') op.drop_index(op.f('ix_map_share_links_map_id'), table_name='map_share_links') op.drop_table('map_share_links') op.drop_index(op.f('ix_map_shares_user_id'), table_name='map_shares') op.drop_index(op.f('ix_map_shares_map_id'), table_name='map_shares') op.drop_table('map_shares') op.execute("DROP TYPE sharepermission")