From e64af5633a1721f24f76d3b778bd95899ec44b79 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 5 May 2020 11:40:57 +0100 Subject: [PATCH] [WIP] --- app/commands.py | 11 +++++++++++ database_maintenance/README.md | 18 +++++++++++++++++- database_maintenance/foreign_keys.sql | 6 ++++++ database_maintenance/script_1.sql | 26 ++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 database_maintenance/foreign_keys.sql create mode 100644 database_maintenance/script_1.sql diff --git a/app/commands.py b/app/commands.py index c102616f8..1a1eede95 100644 --- a/app/commands.py +++ b/app/commands.py @@ -907,6 +907,17 @@ def process_row_from_job(job_id, job_row_number): @click.option('-s', '--start_date', required=True, help='Start date', type=click_dt(format='%Y-%m-%d %H:%M')) @click.option('-e', '--end_date', required=True, help='End date', type=click_dt(format='%Y-%m-%d %H:%M')) def cycle_notification_history_table(limit_row_count, start_date, end_date): + print(f"Starting cycle_notification_history_table for {start_date} to {end_date}") + day_start = start_date + day_end = start_date + timedelta(days=1) + + while day_end < end_date: + populate_notification_history_pivot(limit_row_count, day_start, day_end) + day_start = day_start + timedelta(days=1) + day_end = day_end + timedelta(days=1) + + +def populate_notification_history_pivot(limit_row_count, start_date, end_date): # This relies on the notification_history_pivot table being created # and a trigger on notification_history has been created # what limit should we use here diff --git a/database_maintenance/README.md b/database_maintenance/README.md index e4798f352..5e3c8eef2 100644 --- a/database_maintenance/README.md +++ b/database_maintenance/README.md @@ -18,4 +18,20 @@ cf v3-apply-manifest -f cycle-history-manifest.yml cf v3-push notify-cycle-history cf run-task notify-cycle-history "flask command cycle-notification-history-table -l 100000 -s '2020-03-18 00:00' -e '2020-03-19 00:00" - \ No newline at end of file + + + + +- Deploy new command - cycle_notification_history_table to prod +- script_1.sql + - create nh_pivot + - create nh trigger + - create new index on nh +- foreign_keys.sql +- run command for each day in nh --> start October 1, 2019 +-- all data in NH +- + + + + diff --git a/database_maintenance/foreign_keys.sql b/database_maintenance/foreign_keys.sql new file mode 100644 index 000000000..871e59b1c --- /dev/null +++ b/database_maintenance/foreign_keys.sql @@ -0,0 +1,6 @@ +ALTER TABLE notification_history_pivot ADD CONSTRAINT fk_notification_history_notification_status FOREIGN KEY (notification_status) REFERENCES notification_status_types(name) +ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_api_key_id_fkey FOREIGN KEY (api_key_id) REFERENCES api_keys(id) +ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_job_id_fkey FOREIGN KEY (job_id) REFERENCES jobs(id) +ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_key_type_fkey FOREIGN KEY (key_type) REFERENCES key_types(name) +ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) +ALTER TABLE notification_history_pivot ADD CONSTRAINT notification_history_templates_history_fkey FOREIGN KEY (template_id, template_version) REFERENCES templates_history(id, version) diff --git a/database_maintenance/script_1.sql b/database_maintenance/script_1.sql new file mode 100644 index 000000000..9b3003d80 --- /dev/null +++ b/database_maintenance/script_1.sql @@ -0,0 +1,26 @@ +CREATE TABLE notification_history_pivot AS SELECT * FROM notification_history WHERE 1=2; +ALTER TABLE notification_history_pivot ADD PRIMARY KEY (id); + +-- Update values of notification_status, billable_units, updated_at, sent_by, sent_at based on new updates coming into the notification_history table. +CREATE OR REPLACE FUNCTION update_pivot_table() +RETURNS TRIGGER +LANGUAGE plpgsql AS +$$ +BEGIN + UPDATE notification_history_pivot SET notification_status = NEW.notification_status, + billable_units = NEW.billable_units, + updated_at = NEW.updated_at, + sent_by = NEW.sent_by, + sent_at = NEW.sent_at + WHERE notification_history_pivot.id = NEW.id; + + RETURN NEW; +END +$$; + +DROP TRIGGER IF EXISTS update_pivot on notification_history; +CREATE TRIGGER update_pivot AFTER UPDATE OF notification_status, billable_units, updated_at, sent_by, sent_at ON notification_history +FOR EACH ROW + EXECUTE PROCEDURE update_pivot_table(); + +create index concurrently created_id_nh on notification_history (created_at, id); \ No newline at end of file