Script to add constraints and indexes.

This commit is contained in:
Rebecca Law
2020-04-28 13:05:13 +01:00
parent d6344d3370
commit 67b03294e2
2 changed files with 31 additions and 28 deletions

View File

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

View File

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