Skip to content

Commit

Permalink
Rely on sqlalchemy and alembic to align code and prod db
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Oct 8, 2024
1 parent e81fcde commit c6992c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ctms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)


Expand Down
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit c6992c0

Please sign in to comment.