From f97e082482cb89542f0988ce712cb058a66d8049 Mon Sep 17 00:00:00 2001 From: Jumana Bahrainwala Date: Wed, 18 Oct 2023 08:14:20 -0400 Subject: [PATCH] fix index --- migrations/versions/0439_add_index_n_history.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/migrations/versions/0439_add_index_n_history.py b/migrations/versions/0439_add_index_n_history.py index 8c9645d0db..b59a4ed8b4 100644 --- a/migrations/versions/0439_add_index_n_history.py +++ b/migrations/versions/0439_add_index_n_history.py @@ -13,12 +13,21 @@ down_revision = "0438_sms_templates_msgs_left" +def index_exists(name): + connection = op.get_bind() + result = connection.execute( + "SELECT exists(SELECT 1 from pg_indexes where indexname = '{}') as ix_exists;".format(name) + ).first() + return result.ix_exists + + # option 1 def upgrade(): op.execute("COMMIT") - op.create_index( - op.f("ix_notification_history_created_by_id"), "notification_history", ["created_by_id"], postgresql_concurrently=True - ) + if not index_exists("ix_notification_history_created_by_id"): + op.create_index( + op.f("ix_notification_history_created_by_id"), "notification_history", ["created_by_id"], postgresql_concurrently=True + ) def downgrade():