diff --git a/backend/alembic/versions/034_add_user_id_to_ntfy_sent.py b/backend/alembic/versions/034_add_user_id_to_ntfy_sent.py index 81b3a3d..e40e66d 100644 --- a/backend/alembic/versions/034_add_user_id_to_ntfy_sent.py +++ b/backend/alembic/versions/034_add_user_id_to_ntfy_sent.py @@ -24,10 +24,14 @@ def upgrade() -> None: "fk_ntfy_sent_user_id", "ntfy_sent", "users", ["user_id"], ["id"], ondelete="CASCADE" ) + # On fresh DB ntfy_sent may be empty — clean up NULLs just in case + op.execute("DELETE FROM ntfy_sent WHERE user_id IS NULL") op.alter_column("ntfy_sent", "user_id", nullable=False) - # Drop old unique constraint on notification_key alone - op.drop_constraint("ntfy_sent_notification_key_key", "ntfy_sent", type_="unique") + # Migration 022 created a unique INDEX (ix_ntfy_sent_notification_key), not a + # named unique CONSTRAINT. Drop the index; the new composite unique constraint + # below replaces it. + op.drop_index("ix_ntfy_sent_notification_key", table_name="ntfy_sent") # Create composite unique constraint (per-user dedup) op.create_unique_constraint( @@ -39,8 +43,8 @@ def upgrade() -> None: def downgrade() -> None: op.drop_index("ix_ntfy_sent_user_id", table_name="ntfy_sent") op.drop_constraint("uq_ntfy_sent_user_key", "ntfy_sent", type_="unique") - op.create_unique_constraint( - "ntfy_sent_notification_key_key", "ntfy_sent", ["notification_key"] + op.create_index( + "ix_ntfy_sent_notification_key", "ntfy_sent", ["notification_key"], unique=True ) op.drop_constraint("fk_ntfy_sent_user_id", "ntfy_sent", type_="foreignkey") op.drop_column("ntfy_sent", "user_id")