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

BHBC-1115 #344

Merged
merged 6 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
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
70 changes: 32 additions & 38 deletions database/src/migrations/20210225205948_biohub_release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import path from 'path';
const DB_USER_API_PASS = process.env.DB_USER_API_PASS;
const DB_USER_API = process.env.DB_USER_API;

const DB_RELEASE = 'release.0.17';

/**
* Apply biohub release changes.
*
Expand All @@ -13,62 +15,54 @@ const DB_USER_API = process.env.DB_USER_API;
* @return {*} {Promise<void>}
*/
export async function up(knex: Knex): Promise<void> {
const create_spatial_extensions = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'create_spatial_extensions.psql')
);
const biohub_ddl = fs.readFileSync(path.join(__dirname, 'release.0.16', 'biohub.sql'));
const create_spatial_extensions = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'create_spatial_extensions.psql'));
const biohub_ddl = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'biohub.sql'));
const populate_user_identity_source = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_user_identity_source.sql')
);
const api_set_context = fs.readFileSync(path.join(__dirname, 'release.0.16', 'api_set_context.sql'));
const tr_audit_trigger = fs.readFileSync(path.join(__dirname, 'release.0.16', 'tr_audit_trigger.sql'));
const project_audit_triggers = fs.readFileSync(path.join(__dirname, 'release.0.16', 'project_audit_triggers.sql'));
const api_get_context_user_id = fs.readFileSync(path.join(__dirname, 'release.0.16', 'api_get_context_user_id.sql'));
const tr_journal_trigger = fs.readFileSync(path.join(__dirname, 'release.0.16', 'tr_journal_trigger.sql'));
const project_journal_triggers = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'project_journal_triggers.sql')
path.join(__dirname, DB_RELEASE, 'populate_user_identity_source.sql')
);
const tr_project_funding_source = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'tr_project_funding_source.sql')
);
const api_delete_project = fs.readFileSync(path.join(__dirname, 'release.0.16', 'api_delete_project.sql'));
const api_set_context = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_set_context.sql'));
const tr_audit_trigger = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_audit_trigger.sql'));
const project_audit_triggers = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'project_audit_triggers.sql'));
const api_get_context_user_id = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_get_context_user_id.sql'));
const tr_journal_trigger = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_journal_trigger.sql'));
const project_journal_triggers = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'project_journal_triggers.sql'));
const tr_project_funding_source = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_project_funding_source.sql'));
const api_delete_project = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_delete_project.sql'));

const populate_first_nations = fs.readFileSync(path.join(__dirname, 'release.0.16', 'populate_first_nations.sql'));
const populate_first_nations = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_first_nations.sql'));
const populate_climate_change_initiatives = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_climate_change_initiatives.sql')
path.join(__dirname, DB_RELEASE, 'populate_climate_change_initiatives.sql')
);
const populate_management_action_type = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_management_action_type.sql')
path.join(__dirname, DB_RELEASE, 'populate_management_action_type.sql')
);
const populate_funding_source = fs.readFileSync(path.join(__dirname, 'release.0.16', 'populate_funding_source.sql'));
const populate_funding_source = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_funding_source.sql'));
const populate_investment_action_category = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_investment_action_category.sql')
path.join(__dirname, DB_RELEASE, 'populate_investment_action_category.sql')
);
const populate_project_type = fs.readFileSync(path.join(__dirname, 'release.0.16', 'populate_project_type.sql'));
const populate_activity = fs.readFileSync(path.join(__dirname, 'release.0.16', 'populate_activity.sql'));
const populate_project_type = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_project_type.sql'));
const populate_activity = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_activity.sql'));
const populate_iucn_classifications = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_iucn_classifications.sql')
path.join(__dirname, DB_RELEASE, 'populate_iucn_classifications.sql')
);
const populate_project_role = fs.readFileSync(path.join(__dirname, 'release.0.16', 'populate_project_role.sql'));
const populate_system_role = fs.readFileSync(path.join(__dirname, 'release.0.16', 'populate_system_role.sql'));
const populate_project_role = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_project_role.sql'));
const populate_system_role = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_system_role.sql'));
const populate_administrative_activity_type = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_administrative_activity_type.sql')
path.join(__dirname, DB_RELEASE, 'populate_administrative_activity_type.sql')
);
const populate_administrative_activity_status_type = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_administrative_activity_status_type.sql')
);
const populate_proprietor_type = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_proprietor_type.sql')
path.join(__dirname, DB_RELEASE, 'populate_administrative_activity_status_type.sql')
);
const populate_proprietor_type = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_proprietor_type.sql'));
const populate_wldtaxonomic_units = fs.readFileSync(
path.join(__dirname, 'release.0.16', 'populate_wldtaxonomic_units.sql')
path.join(__dirname, DB_RELEASE, 'populate_wldtaxonomic_units.sql')
);
const tr_survey_proprietor = fs.readFileSync(path.join(__dirname, 'release.0.16', 'tr_survey_proprietor.sql'));
const tr_project = fs.readFileSync(path.join(__dirname, 'release.0.16', 'tr_project.sql'));
const tr_survey = fs.readFileSync(path.join(__dirname, 'release.0.16', 'tr_survey.sql'));
const tr_permit = fs.readFileSync(path.join(__dirname, 'release.0.16', 'tr_permit.sql'));
const tr_survey_proprietor = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_survey_proprietor.sql'));
const tr_project = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_project.sql'));
const tr_survey = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_survey.sql'));
const tr_permit = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_permit.sql'));

const project_dapi_views = fs.readFileSync(path.join(__dirname, 'release.0.16', 'project_dapi_views.sql'));
const project_dapi_views = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'project_dapi_views.sql'));

await knex.raw(`
-- set up spatial extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ $$
-- 2021-04-19 initial release
-- *******************************************************************
declare

__count integer := 0;
begin
select count(*) into __count from survey_publish_history where s_id in (select id from survey where p_id = __project_id);
if (__count > 0) then
raise exception 'Project cannot be deleted as associated published data exists.';
end if;

delete from survey_proprietor where s_id in (select id from survey where p_id = __project_id);
delete from survey_attachment where s_id in (select id from survey where p_id = __project_id);
delete from study_species where s_id in (select id from survey where p_id = __project_id);
Expand All @@ -35,6 +40,9 @@ begin
delete from project_first_nation where p_id = __project_id;
delete from project_participation where p_id = __project_id;
delete from project where id = __project_id;


exception
when others THEN
raise;
end;
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- ER/Studio Data Architect SQL Code Generation
-- Project : BioHub.DM1
--
-- Date Created : Tuesday, May 18, 2021 10:47:16
-- Date Created : Thursday, June 03, 2021 12:01:34
-- Target DBMS : PostgreSQL 10.x-12.x
--

Expand Down Expand Up @@ -652,7 +652,15 @@ COMMENT ON COLUMN permit.update_user IS 'The id of the user who updated the reco
;
COMMENT ON COLUMN permit.revision_count IS 'Revision count used for concurrency control.'
;
COMMENT ON TABLE permit IS 'Provides a record of scientific permits.'
COMMENT ON TABLE permit IS 'Provides a record of scientific permits. Note that permits are first class objects in the data model and do not require an association to either a project or survey. Additionally:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

- Association to a survey or project implies that sampling was conducted related to the permit
- No association to a survey or project implies that sampling was not conducted related to the permit
- Permits that are associated with a project should eventually be related to a survey
- Permits can be associated with one or zero projects
- Permits can only be associated with one survey
- Permits that have no association with a project or survey require values for coordinator first name, last name, email address and agency name

NOTE: there are conceptual problems with associating permits to projects early instead of at the survey level and these should be addressed in subsequent versions of the application.'
;

--
Expand Down Expand Up @@ -783,7 +791,7 @@ CREATE TABLE project_attachment(
file_name varchar(300),
title varchar(300),
description varchar(250),
key varchar(300) NOT NULL,
key varchar(1000) NOT NULL,
file_size integer,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
Expand Down Expand Up @@ -1318,7 +1326,7 @@ COMMENT ON COLUMN study_species.update_user IS 'The id of the user who updated t
;
COMMENT ON COLUMN study_species.revision_count IS 'Revision count used for concurrency control.'
;
COMMENT ON TABLE study_species IS 'The study species for the project and survey.'
COMMENT ON TABLE study_species IS 'The study species for the survey.'
;

--
Expand Down Expand Up @@ -1397,7 +1405,7 @@ CREATE TABLE survey_attachment(
file_name varchar(300),
title varchar(300),
description varchar(250),
key varchar(300) NOT NULL,
key varchar(1000) NOT NULL,
file_size integer,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
Expand Down Expand Up @@ -1475,6 +1483,56 @@ COMMENT ON COLUMN survey_funding_source.revision_count IS 'Revision count used f
COMMENT ON TABLE survey_funding_source IS 'A associative entity that joins surveys and funding source details.'
;

--
-- TABLE: survey_occurrence
--

CREATE TABLE survey_occurrence(
id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
s_id integer NOT NULL,
associatedtaxa varchar(3000) NOT NULL,
lifestage varchar(3000) NOT NULL,
data json,
geometry geometry(geometry, 3005),
geography geography(geometry),
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
update_date timestamptz(6),
update_user integer,
revision_count integer DEFAULT 0 NOT NULL,
CONSTRAINT "PK169" PRIMARY KEY (id)
)
;



COMMENT ON COLUMN survey_occurrence.id IS 'System generated surrogate primary key identifier.'
;
COMMENT ON COLUMN survey_occurrence.s_id IS 'System generated surrogate primary key identifier.'
;
COMMENT ON COLUMN survey_occurrence.associatedtaxa IS 'A string representation of the value provided for the given Darwin Core term.'
;
COMMENT ON COLUMN survey_occurrence.lifestage IS 'A string representation of the value provided for the given Darwin Core term.'
;
COMMENT ON COLUMN survey_occurrence.data IS 'The json data associated with the record.'
;
COMMENT ON COLUMN survey_occurrence.geometry IS 'The containing geometry of the record.'
;
COMMENT ON COLUMN survey_occurrence.geography IS 'The containing geography of the record.'
;
COMMENT ON COLUMN survey_occurrence.create_date IS 'The datetime the record was created.'
;
COMMENT ON COLUMN survey_occurrence.create_user IS 'The id of the user who created the record as identified in the system user table.'
;
COMMENT ON COLUMN survey_occurrence.update_date IS 'The datetime the record was updated.'
;
COMMENT ON COLUMN survey_occurrence.update_user IS 'The id of the user who updated the record as identified in the system user table.'
;
COMMENT ON COLUMN survey_occurrence.revision_count IS 'Revision count used for concurrency control.'
;
COMMENT ON TABLE survey_occurrence IS 'Occurrence records associated with a survey.'
;

--
-- TABLE: survey_proprietor
--
Expand Down Expand Up @@ -1525,6 +1583,47 @@ COMMENT ON COLUMN survey_proprietor.revision_count IS 'Revision count used for c
COMMENT ON TABLE survey_proprietor IS 'Intersection table associating surveys to proprietary types and associated meta data.'
;

--
-- TABLE: survey_publish_history
--

CREATE TABLE survey_publish_history(
id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
s_id integer NOT NULL,
publish_date date NOT NULL,
key varchar(1000),
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
update_date timestamptz(6),
update_user integer,
revision_count integer DEFAULT 0 NOT NULL,
CONSTRAINT "PK165" PRIMARY KEY (id)
)
;



COMMENT ON COLUMN survey_publish_history.id IS 'System generated surrogate primary key identifier.'
;
COMMENT ON COLUMN survey_publish_history.s_id IS 'System generated surrogate primary key identifier.'
;
COMMENT ON COLUMN survey_publish_history.publish_date IS 'The date that the survey version was published.'
;
COMMENT ON COLUMN survey_publish_history.key IS 'The identifying key to the file in the storage system.'
;
COMMENT ON COLUMN survey_publish_history.create_date IS 'The datetime the record was created.'
;
COMMENT ON COLUMN survey_publish_history.create_user IS 'The id of the user who created the record as identified in the system user table.'
;
COMMENT ON COLUMN survey_publish_history.update_date IS 'The datetime the record was updated.'
;
COMMENT ON COLUMN survey_publish_history.update_user IS 'The id of the user who updated the record as identified in the system user table.'
;
COMMENT ON COLUMN survey_publish_history.revision_count IS 'Revision count used for concurrency control.'
;
COMMENT ON TABLE survey_publish_history IS 'Provides a historical listing of published dates and pointers to raw data versions.'
;

--
-- TABLE: system_constant
--
Expand Down Expand Up @@ -2239,6 +2338,12 @@ CREATE INDEX "Ref7487" ON survey_funding_source(pfs_id)
CREATE INDEX "Ref15388" ON survey_funding_source(s_id)
;
--
-- INDEX: "Ref15396"
--

CREATE INDEX "Ref15396" ON survey_occurrence(s_id)
;
--
-- INDEX: "Ref15983"
--

Expand All @@ -2257,6 +2362,12 @@ CREATE INDEX "Ref15384" ON survey_proprietor(s_id)
CREATE INDEX "Ref12785" ON survey_proprietor(fn_id)
;
--
-- INDEX: "Ref15395"
--

CREATE INDEX "Ref15395" ON survey_publish_history(s_id)
;
--
-- INDEX: sc_uk1
--

Expand Down Expand Up @@ -2580,6 +2691,16 @@ ALTER TABLE survey_funding_source ADD CONSTRAINT "Refsurvey88"
;


--
-- TABLE: survey_occurrence
--

ALTER TABLE survey_occurrence ADD CONSTRAINT "Refsurvey96"
FOREIGN KEY (s_id)
REFERENCES survey(id)
;


--
-- TABLE: survey_proprietor
--
Expand All @@ -2600,6 +2721,16 @@ ALTER TABLE survey_proprietor ADD CONSTRAINT "Reffirst_nations85"
;


--
-- TABLE: survey_publish_history
--

ALTER TABLE survey_publish_history ADD CONSTRAINT "Refsurvey95"
FOREIGN KEY (s_id)
REFERENCES survey(id)
;


--
-- TABLE: system_user
--
Expand Down
Loading