Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: Migration to add NOT NULL constraint to all uuid columns #1363

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(``);
}