Merge branch 'cycle-notification_history-table' of github.com:alphagov/notifications-api into cycle-notification_history-table

This commit is contained in:
Rebecca Law
2020-05-05 17:23:23 +01:00
3 changed files with 34 additions and 1 deletions

View File

@@ -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")

View File

@@ -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

View File

@@ -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')