Skip to content

Commit

Permalink
[Postgres] Fix broken migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jbygdell committed Sep 20, 2023
1 parent 06446b3 commit 021bcce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
18 changes: 7 additions & 11 deletions postgresql/migratedb.d/08.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ BEGIN
INSERT INTO sda.dbschema_version VALUES(sourcever+1, now(), changes);

-- add new permissions
GRANT USAGE, SELECT ON sda.file_event_log TO finalize;
GRANT USAGE, SELECT ON sda.file_event_log TO ingest;
GRANT USAGE, SELECT ON sda.file_event_log TO verify;
GRANT SELECT ON sda.file_event_log TO finalize;
GRANT SELECT ON sda.file_event_log TO ingest;
GRANT SELECT ON sda.file_event_log TO verify;

-- New ingestion specific functions
CREATE FUNCTION sda.set_archived(file_uuid UUID, corr_id UUID, file_path TEXT, file_size BIGINT, inbox_checksum_value TEXT, inbox_checksum_type TEXT)
CREATE FUNCTION set_archived(file_uuid UUID, corr_id UUID, file_path TEXT, file_size BIGINT, inbox_checksum_value TEXT, inbox_checksum_type TEXT)
RETURNS void AS $set_archived$
DECLARE
fid UUID;
BEGIN
SELECT file_id from sda.file_event_log where correlation_id = corr_id INTO fid;

UPDATE sda.files SET archive_file_path = file_path, archive_file_size = file_size WHERE id = fid;
UPDATE sda.files SET archive_file_path = file_path, archive_file_size = file_size WHERE id = file_uuid;

INSERT INTO sda.checksums(file_id, checksum, type, source)
VALUES(fid, inbox_checksum_value, upper(inbox_checksum_type)::sda.checksum_algorithm, upper(UPLOADED)::sda.checksum_source);
VALUES(file_uuid, inbox_checksum_value, upper(inbox_checksum_type)::sda.checksum_algorithm, upper('UPLOADED')::sda.checksum_source);

INSERT INTO sda.file_event_log(file_id, event, correlation_id) VALUES(fid, 'archived' corr_id);
INSERT INTO sda.file_event_log(file_id, event, correlation_id) VALUES(file_uuid, 'archived', corr_id);
END;

$set_archived$ LANGUAGE plpgsql;
Expand Down
7 changes: 4 additions & 3 deletions postgresql/migratedb.d/09.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ DECLARE
sourcever INTEGER := 8;
changes VARCHAR := 'Add dataset event log';
BEGIN
SET search_path TO sda;
IF (select max(version) from sda.dbschema_version) = sourcever then
RAISE NOTICE 'Doing migration from schema version % to %', sourcever, sourcever+1;
RAISE NOTICE 'Changes: %', changes;
INSERT INTO sda.dbschema_version VALUES(sourcever+1, now(), changes);

CREATE TABLE dataset_events (
CREATE TABLE sda.dataset_events (
id SERIAL PRIMARY KEY,
title VARCHAR(64) UNIQUE, -- short name of the action
description TEXT
);

INSERT INTO dataset_events(id,title,description)
INSERT INTO sda.dataset_events(id,title,description)
VALUES (10, 'registered', 'Register a dataset to recieve file accession IDs mappings.'),
(20, 'released' , 'The dataset is released on this date'),
(30, 'deprecated', 'The dataset is deprecated on this date');

CREATE TABLE dataset_event_log (
CREATE TABLE sda.dataset_event_log (
id SERIAL PRIMARY KEY,
dataset_id TEXT REFERENCES datasets(stable_id),
event TEXT REFERENCES dataset_events(title),
Expand Down
5 changes: 2 additions & 3 deletions postgresql/migratedb.d/10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ DECLARE
BEGIN
-- No explicit transaction handling here, this all happens in a transaction
-- automatically
IF (select max(version) from local_ega.dbschema_version) = sourcever then
IF (select max(version) from sda.dbschema_version) = sourcever then
RAISE NOTICE 'Doing migration from schema version % to %', sourcever, sourcever+1;
RAISE NOTICE 'Changes: %', changes;
INSERT INTO local_ega.dbschema_version VALUES(sourcever+1, now(), changes);
INSERT INTO sda.dbschema_version VALUES(sourcever+1, now(), changes);

-- Temporary function for creating roles if they do not already exist.
CREATE FUNCTION create_role_if_not_exists(role_name NAME) RETURNS void AS $created$
Expand All @@ -32,7 +32,6 @@ BEGIN
$created$ LANGUAGE plpgsql;

PERFORM create_role_if_not_exists('inbox');
CREATE ROLE inbox;
GRANT USAGE ON SCHEMA sda TO inbox;
GRANT SELECT, INSERT, UPDATE ON sda.files TO inbox;
GRANT SELECT, INSERT ON sda.file_event_log TO inbox;
Expand Down

0 comments on commit 021bcce

Please sign in to comment.