Files
gh-christianlouis-docuelevate/migrations/versions/025_add_user_notifications.py
T
copilot-swe-agent[bot] c374ba3ae9 fix(migrations): resolve duplicate Alembic heads causing test failures
Two migration files (025_add_shared_links and 025_add_user_notifications)
both had down_revision pointing to 024_add_api_tokens, creating multiple
migration heads. This caused alembic stamp/upgrade head to fail with
'Multiple heads are present; please specify a single target revision'
whenever init_db() ran (e.g. in TestClient fixtures).

Fix: chain 025_add_user_notifications from 025_add_shared_links so the
migration history is linear:
024_add_api_tokens → 025_add_shared_links → 025_add_user_notifications

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
2026-03-09 01:11:26 +00:00

84 lines
4.1 KiB
Python

"""Add user notification tables (targets, preferences, in-app inbox)
Revision ID: 025_add_user_notifications
Revises: 025_add_shared_links
Create Date: 2026-03-09
"""
from typing import Union
import sqlalchemy as sa
from alembic import op
revision: str = "025_add_user_notifications"
down_revision: Union[str, None] = "025_add_shared_links"
depends_on: Union[str, None] = None
def upgrade() -> None:
"""Create user_notification_targets, user_notification_preferences, and in_app_notifications tables."""
op.create_table(
"user_notification_targets",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("owner_id", sa.String(), nullable=False),
sa.Column("channel_type", sa.String(20), nullable=False),
sa.Column("name", sa.String(255), nullable=False),
sa.Column("config", sa.Text(), nullable=True),
sa.Column("is_active", sa.Boolean(), nullable=False, server_default="1"),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_user_notification_targets_id", "user_notification_targets", ["id"])
op.create_index("ix_user_notification_targets_owner_id", "user_notification_targets", ["owner_id"])
op.create_table(
"user_notification_preferences",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("owner_id", sa.String(), nullable=False),
sa.Column("event_type", sa.String(50), nullable=False),
sa.Column("channel_type", sa.String(20), nullable=False),
sa.Column("target_id", sa.Integer(), nullable=True),
sa.Column("is_enabled", sa.Boolean(), nullable=False, server_default="1"),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("owner_id", "event_type", "channel_type", "target_id"),
)
op.create_index("ix_user_notification_preferences_id", "user_notification_preferences", ["id"])
op.create_index("ix_user_notification_preferences_owner_id", "user_notification_preferences", ["owner_id"])
op.create_table(
"in_app_notifications",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("owner_id", sa.String(), nullable=False),
sa.Column("event_type", sa.String(50), nullable=False),
sa.Column("title", sa.String(255), nullable=False),
sa.Column("message", sa.Text(), nullable=True),
sa.Column("is_read", sa.Boolean(), nullable=False, server_default="0"),
sa.Column("file_id", sa.Integer(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_in_app_notifications_id", "in_app_notifications", ["id"])
op.create_index("ix_in_app_notifications_owner_id", "in_app_notifications", ["owner_id"])
op.create_index("ix_in_app_notifications_is_read", "in_app_notifications", ["is_read"])
op.create_index("ix_in_app_notifications_created_at", "in_app_notifications", ["created_at"])
def downgrade() -> None:
"""Drop user notification tables."""
op.drop_index("ix_in_app_notifications_created_at", "in_app_notifications")
op.drop_index("ix_in_app_notifications_is_read", "in_app_notifications")
op.drop_index("ix_in_app_notifications_owner_id", "in_app_notifications")
op.drop_index("ix_in_app_notifications_id", "in_app_notifications")
op.drop_table("in_app_notifications")
op.drop_index("ix_user_notification_preferences_owner_id", "user_notification_preferences")
op.drop_index("ix_user_notification_preferences_id", "user_notification_preferences")
op.drop_table("user_notification_preferences")
op.drop_index("ix_user_notification_targets_owner_id", "user_notification_targets")
op.drop_index("ix_user_notification_targets_id", "user_notification_targets")
op.drop_table("user_notification_targets")