"""add users table and user_id to follows Revision ID: 0005 Revises: 0004 Create Date: 2026-03-01 00:00:00.000000 """ from typing import Sequence, Union import sqlalchemy as sa from sqlalchemy.dialects.postgresql import JSONB from alembic import op revision: str = "0005" down_revision: Union[str, None] = "0004" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: # 1. Clear existing global follows — they have no user and cannot be migrated op.execute("DELETE FROM follows") # 2. Create users table op.create_table( "users", sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), sa.Column("email", sa.String(), nullable=False), sa.Column("hashed_password", sa.String(), nullable=False), sa.Column("is_admin", sa.Boolean(), nullable=False, server_default="false"), sa.Column("notification_prefs", JSONB(), nullable=False, server_default="{}"), sa.Column( "created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=True, ), sa.PrimaryKeyConstraint("id"), ) op.create_index(op.f("ix_users_email"), "users", ["email"], unique=True) # 3. Add user_id to follows (nullable first, then tighten after FK is set) op.add_column("follows", sa.Column("user_id", sa.Integer(), nullable=True)) # 4. FK constraint op.create_foreign_key( "fk_follows_user_id", "follows", "users", ["user_id"], ["id"], ondelete="CASCADE", ) # 5. Drop old unique constraint and add user-scoped one op.drop_constraint("uq_follows_type_value", "follows", type_="unique") op.create_unique_constraint( "uq_follows_user_type_value", "follows", ["user_id", "follow_type", "follow_value"], ) # 6. Make user_id NOT NULL (table is empty so this is safe) op.alter_column("follows", "user_id", nullable=False) def downgrade() -> None: op.alter_column("follows", "user_id", nullable=True) op.drop_constraint("uq_follows_user_type_value", "follows", type_="unique") op.create_unique_constraint("uq_follows_type_value", "follows", ["follow_type", "follow_value"]) op.drop_constraint("fk_follows_user_id", "follows", type_="foreignkey") op.drop_column("follows", "user_id") op.drop_index(op.f("ix_users_email"), table_name="users") op.drop_table("users")