This commit is contained in:
Rebecca Law
2020-05-05 11:40:57 +01:00
parent 0d9782c077
commit e64af5633a
4 changed files with 60 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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