From df69f7eedffb5aa6a64d2c5280d7247f0e06a4f9 Mon Sep 17 00:00:00 2001 From: Kang Ming Date: Mon, 28 Oct 2024 14:16:59 +0800 Subject: [PATCH] fix: make drop_uniqueness_constraint_on_phone idempotent --- ...06073726_drop_uniqueness_constraint_on_phone.up.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/migrations/20240806073726_drop_uniqueness_constraint_on_phone.up.sql b/migrations/20240806073726_drop_uniqueness_constraint_on_phone.up.sql index 644e58fdc..ade27eaf6 100644 --- a/migrations/20240806073726_drop_uniqueness_constraint_on_phone.up.sql +++ b/migrations/20240806073726_drop_uniqueness_constraint_on_phone.up.sql @@ -1,6 +1,16 @@ alter table {{ index .Options "Namespace" }}.mfa_factors drop constraint if exists mfa_factors_phone_key; do $$ begin + -- if both indexes exist, it means that the schema_migrations table was truncated and the migrations had to be rerun + if ( + select count(*) = 2 + from pg_indexes + where indexname in ('unique_verified_phone_factor', 'unique_phone_factor_per_user') + and schemaname = '{{ index .Options "Namespace" }}' + ) then + execute 'drop index {{ index .Options "Namespace" }}.unique_verified_phone_factor'; + end if; + if exists ( select 1 from pg_indexes