diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 46ea39709..01d8b5269 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -372,6 +372,14 @@ def insert_notification_history_delete_notifications( ON CONFLICT ON CONSTRAINT notification_history_pkey DO NOTHING """ + # Insert data into the pivot table as well. Only needed while we are trying to cycle the history table. + insert_pivot_query = """ + insert into notification_history_pivot + SELECT * from NOTIFICATION_ARCHIVE + ON CONFLICT ON CONSTRAINT notification_history_pivot_pkey + DO NOTHING + """ + delete_query = """ DELETE FROM notifications where id in (select id from NOTIFICATION_ARCHIVE) @@ -390,6 +398,8 @@ def insert_notification_history_delete_notifications( db.session.execute(insert_query) + db.session.execute(insert_pivot_query) + db.session.execute(delete_query) db.session.execute("DROP TABLE NOTIFICATION_ARCHIVE") diff --git a/database_maintenance/cycle_notification_history.sql b/database_maintenance/cycle_notification_history.sql index beaf62ad4..0fa370491 100644 --- a/database_maintenance/cycle_notification_history.sql +++ b/database_maintenance/cycle_notification_history.sql @@ -22,7 +22,7 @@ END $$; DROP TRIGGER IF EXISTS update_pivot on notification_history; --- may need to cancel the autovacuum +-- Following may be blocked if running vacuum. -- select pg_cancel_backend(pid); CREATE TRIGGER update_pivot AFTER UPDATE OF notification_status, billable_units, updated_at, sent_by, sent_at ON notification_history FOR EACH ROW diff --git a/migrations/versions/0321_notification_history_pivot.py b/migrations/versions/0321_notification_history_pivot.py new file mode 100644 index 000000000..50f1bc144 --- /dev/null +++ b/migrations/versions/0321_notification_history_pivot.py @@ -0,0 +1,23 @@ +""" + +Revision ID: 0321_notification_history_pivot +Revises: 0320_optimise_notifications +Create Date: 2020-03-26 11:16:12.389524 + +""" +import os + +from alembic import op + +revision = '0321_notification_history_pivot' +down_revision = '0320_optimise_notifications' +environment = os.environ['NOTIFY_ENVIRONMENT'] + + +def upgrade(): + op.execute('CREATE TABLE notification_history_pivot AS SELECT * FROM notification_history WHERE 1=2') + op.execute('ALTER TABLE notification_history_pivot ADD PRIMARY KEY (id)') + + +def downgrade(): + op.execute('DROP TABLE notifications_history_pivot')