-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created migration to add NOT NULL constraint on all uuid columm…
…ns. (#1363)
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
database/src/migrations/20240911000000_add_not_null_for_uuid_columns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Knex } from 'knex'; | ||
|
||
/** | ||
* Migrate existing uuid columns that are NULL to have a UUID value. | ||
* | ||
* 1. Generate UUIDs for existing uuid columns that are NULL | ||
* 2. Set uuid columns to NOT NULL | ||
* | ||
* @export | ||
* @param {Knex} knex | ||
* @return {*} {Promise<void>} | ||
*/ | ||
export async function up(knex: Knex): Promise<void> { | ||
await knex.raw(`--sql | ||
---------------------------------------------------------------------------------------- | ||
-- Drop Views | ||
---------------------------------------------------------------------------------------- | ||
SET SEARCH_PATH=biohub_dapi_v1; | ||
DROP VIEW IF EXISTS survey; | ||
DROP VIEW IF EXISTS project; | ||
DROP VIEW IF EXISTS project_attachment; | ||
DROP VIEW IF EXISTS project_report_attachment; | ||
DROP VIEW IF EXISTS survey_attachment; | ||
DROP VIEW IF EXISTS survey_report_attachment; | ||
DROP VIEW IF EXISTS survey_summary_submission; | ||
DROP VIEW IF EXISTS survey_telemetry_credential_attachment; | ||
---------------------------------------------------------------------------------------- | ||
-- Generate UUIDs for existing uuid columns that are NULL | ||
---------------------------------------------------------------------------------------- | ||
SET SEARCH_PATH=biohub; | ||
UPDATE survey SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE project SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE project_attachment SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE project_report_attachment SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE survey_attachment SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE survey_report_attachment SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE survey_summary_submission SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
UPDATE survey_telemetry_credential_attachment SET uuid = public.gen_random_uuid() WHERE uuid IS NULL; | ||
---------------------------------------------------------------------------------------- | ||
-- Set uuid columns to NOT NULL | ||
---------------------------------------------------------------------------------------- | ||
ALTER TABLE survey ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE project ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE project_attachment ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE project_report_attachment ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE survey_attachment ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE survey_report_attachment ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE survey_summary_submission ALTER COLUMN uuid SET NOT NULL; | ||
ALTER TABLE survey_telemetry_credential_attachment ALTER COLUMN uuid SET NOT NULL; | ||
---------------------------------------------------------------------------------------- | ||
-- Recreate Views | ||
---------------------------------------------------------------------------------------- | ||
SET SEARCH_PATH=biohub_dapi_v1; | ||
CREATE OR REPLACE VIEW survey AS SELECT * FROM biohub.survey; | ||
CREATE OR REPLACE VIEW project AS SELECT * FROM biohub.project; | ||
CREATE OR REPLACE VIEW project_attachment AS SELECT * FROM biohub.project_attachment; | ||
CREATE OR REPLACE VIEW project_report_attachment AS SELECT * FROM biohub.project_report_attachment; | ||
CREATE OR REPLACE VIEW survey_attachment AS SELECT * FROM biohub.survey_attachment; | ||
CREATE OR REPLACE VIEW survey_report_attachment AS SELECT * FROM biohub.survey_report_attachment; | ||
CREATE OR REPLACE VIEW survey_summary_submission AS SELECT * FROM biohub.survey_summary_submission; | ||
CREATE OR REPLACE VIEW survey_telemetry_credential_attachment AS SELECT * FROM biohub.survey_telemetry_credential_attachment; | ||
`); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.raw(``); | ||
} |