From b03cd6dfe5570ae626e98500b50179223d64554b Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Thu, 10 Oct 2024 10:39:22 +0200 Subject: [PATCH] Fix #238: migration to backport prod index (#979) * Fix #238: migration to backport prod index * Revert "Fix #238: migration to backport prod index" This reverts commit 15d461c8e36a6ff2d64d3546138d9d8e965e62dd. * Rely on sqlalchemy and alembic to align code and prod db --- ctms/models.py | 6 ++- ...08_fd04e56b249e_add_uniqueness_to_index.py | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/20241008_fd04e56b249e_add_uniqueness_to_index.py diff --git a/ctms/models.py b/ctms/models.py index dd0bba2f..23adde3a 100644 --- a/ctms/models.py +++ b/ctms/models.py @@ -93,7 +93,11 @@ def primary_email_insensitive_comparator(cls): # Indexes __table_args__ = ( Index("bulk_read_index", "update_timestamp", "email_id"), - Index("idx_email_primary_email_lower", func.lower(primary_email)), + Index( + "idx_email_primary_unique_email_lower", + func.lower(primary_email), + unique=True, + ), ) diff --git a/migrations/versions/20241008_fd04e56b249e_add_uniqueness_to_index.py b/migrations/versions/20241008_fd04e56b249e_add_uniqueness_to_index.py new file mode 100644 index 00000000..1ac88ea9 --- /dev/null +++ b/migrations/versions/20241008_fd04e56b249e_add_uniqueness_to_index.py @@ -0,0 +1,50 @@ +"""Add uniqueness to index + +Revision ID: fd04e56b249e +Revises: 3689b813fe22 +Create Date: 2024-10-08 12:30:13.902342 + +""" +# pylint: disable=no-member invalid-name +# no-member is triggered by alembic.op, which has dynamically added functions +# invalid-name is triggered by migration file names with a date prefix +# invalid-name is triggered by top-level alembic constants like revision instead of REVISION + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "fd04e56b249e" # pragma: allowlist secret +down_revision = "3689b813fe22" # pragma: allowlist secret +branch_labels = None +depends_on = None + + +def upgrade(): + # In STAGE/PROD we have both the indexes `idx_email_primary_email_lower` and + # `idx_email_primary_unique_email_lower`. + # The goal of this migration is to align the code base with STAGE/PROD. + # We only keep `idx_email_primary_unique_email_lower`. + # This migration will: + # - create it if not exist (DEV) + # - drop the `idx_email_primary_email_lower` (without uniqueness) + op.create_index( + "idx_email_primary_unique_email_lower", + "emails", + [sa.text("lower(primary_email)")], + unique=True, + if_not_exists=True, + ) + op.drop_index("idx_email_primary_email_lower", table_name="emails") + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index("idx_email_primary_unique_email_lower", table_name="emails") + op.create_index( + "idx_email_primary_email_lower", + "emails", + [sa.text("lower(primary_email::text)")], + unique=False, + ) + # ### end Alembic commands ###