From 67b03294e2ae376ef7fb5798f059bb3f16561670 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 28 Apr 2020 13:05:13 +0100 Subject: [PATCH] Script to add constraints and indexes. --- database_maintenance/constraints.sql | 31 +++++++++++++++++++ .../create_temp_notification_history.sql | 28 ----------------- 2 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 database_maintenance/constraints.sql delete mode 100644 database_maintenance/create_temp_notification_history.sql diff --git a/database_maintenance/constraints.sql b/database_maintenance/constraints.sql new file mode 100644 index 000000000..20bc03c9d --- /dev/null +++ b/database_maintenance/constraints.sql @@ -0,0 +1,31 @@ + +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) + +-- we could possibly not create this check constraint in the new table since we want to drop it in an outstanding PR. +ALTER TABLE notification_history_pivot ADD CONSTRAINT chk_notification_history_postage_null CHECK ( +CASE + WHEN notification_type = 'letter'::notification_type + THEN postage IS NOT NULL AND (postage::text = ANY (ARRAY['first'::character varying, 'second'::character varying]::text[])) + ELSE postage IS NULL +END) + + +--- Create indexes after drop table since the names need to be unique +--- Or rename these indexes (index names have to be unique in db) +CREATE INDEX CONCURRENTLY ix_notification_history_job_id ON notification_history_pivot (job_id); +CREATE INDEX CONCURRENTLY ix_notification_history_reference ON notification_history_pivot (reference); +CREATE INDEX CONCURRENTLY ix_notification_history_template_id ON notification_history_pivot (template_id); +CREATE INDEX CONCURRENTLY ix_notifications_service_id_composite ON notification_history_pivot (service_id, key_type, notification_type, created_at); + +-- Not creating these ones since we want to drop them any way. +--DROP INDEX CONCURRENTLY ix_notification_history_api_key_id; +--DROP INDEX CONCURRENTLY ix_notification_history_created_at; +--DROP INDEX CONCURRENTLY ix_notification_history_notification_status; +--DROP INDEX CONCURRENTLY ix_notification_history_notification_type; +--DROP INDEX CONCURRENTLY ix_notification_history_service_id; +--DROP INDEX CONCURRENTLY ix_notification_history_service_id_created_at; diff --git a/database_maintenance/create_temp_notification_history.sql b/database_maintenance/create_temp_notification_history.sql deleted file mode 100644 index 4a6c69394..000000000 --- a/database_maintenance/create_temp_notification_history.sql +++ /dev/null @@ -1,28 +0,0 @@ ---- Just a holding place to create a table to cycle the Notification history table -create table temp_notification_history ( - id | uuid | not null -job_id | uuid | -job_row_number | integer | -service_id | uuid | -template_id | uuid | -template_version | integer | not null -api_key_id | uuid | -key_type | character varying | not null -notification_type | notification_type | not null -created_at | timestamp without time zone | not null -sent_at | timestamp without time zone | -sent_by | character varying | -updated_at | timestamp without time zone | -reference | character varying | -billable_units | integer | not null -client_reference | character varying | -international | boolean | -phone_prefix | character varying | -rate_multiplier | numeric | -notification_status | text | -created_by_id | uuid | -postage | character varying | -document_download_count | integer | - -) -with indexes \ No newline at end of file