Skip to content

Commit

Permalink
TechDebt: Update existing migration: remove observation table (for no…
Browse files Browse the repository at this point in the history
…w) (#1084)

Update existing migration: remove observation table (for now)
  • Loading branch information
NickPhura authored Aug 31, 2023
1 parent 130364f commit ec42e38
Showing 1 changed file with 10 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Knex } from 'knex';

/**
* Added critter and observation tables with trigger to check if critter instances is less than observation.total_count
* Added critter and deployment tables.
*
* @export
* @param {Knex} knex
Expand All @@ -11,48 +11,14 @@ import { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
await knex.raw(`--sql
----------------------------------------------------------------------------------------
-- Create Observation Table
----------------------------------------------------------------------------------------
set search_path=biohub;
CREATE TABLE observation(
observation_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
survey_id integer NOT NULL,
total_count integer NOT NULL CHECK(total_count > 0),
observation_date date,
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 observation_pk PRIMARY KEY (observation_id)
);
COMMENT ON COLUMN observation.observation_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN observation.survey_id IS 'The id of the survey.';
COMMENT ON COLUMN observation.observation_date IS 'Date the observation occured.';
COMMENT ON COLUMN observation.total_count IS 'The number of individuals recorded in observation. Associated critters must be less than or equal.';
COMMENT ON COLUMN observation.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN observation.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN observation.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN observation.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN observation.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE observation IS 'Information about taxon counts.';
-- Create audit and journal triggers
create trigger audit_observation before insert or update or delete on observation for each row execute procedure tr_audit_trigger();
create trigger journal_observation after insert or update or delete on observation for each row execute procedure tr_journal_trigger();
----------------------------------------------------------------------------------------
-- Create Critter table
----------------------------------------------------------------------------------------
set search_path=biohub;
CREATE TABLE critter(
critter_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
observation_id integer NOT NULL,
survey_id integer NOT NULL,
critterbase_critter_id uuid NOT NULL,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
Expand All @@ -63,25 +29,25 @@ export async function up(knex: Knex): Promise<void> {
);
COMMENT ON COLUMN critter.critter_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN critter.observation_id IS 'The id of the observation';
COMMENT ON COLUMN critter.survey_id IS 'The id of the survey.';
COMMENT ON COLUMN critter.critterbase_critter_id IS 'The external system id of a Critterbase critter.';
COMMENT ON COLUMN critter.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN critter.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN critter.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN critter.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN critter.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE critter IS 'Information about individual animals associated to specific observations.';
COMMENT ON TABLE critter IS 'Information about individual animals associated.';
-- Add foreign key constraint from child table to parent table on observation_id
-- Add foreign key constraint from child table to parent table on survey_id
ALTER TABLE critter ADD CONSTRAINT critter_fk1
FOREIGN KEY (observation_id)
REFERENCES observation(observation_id);
FOREIGN KEY (survey_id)
REFERENCES survey(survey_id);
-- Add foreign key index
CREATE INDEX critter_idx1 ON critter(observation_id);
CREATE INDEX critter_idx1 ON critter(survey_id);
-- Add unique constraint
CREATE UNIQUE INDEX critter_uk1 ON critter(observation_id, critterbase_critter_id);
CREATE UNIQUE INDEX critter_uk1 ON critter(survey_id, critterbase_critter_id);
-- Create audit and journal triggers
create trigger audit_critter before insert or update or delete on critter for each row execute procedure tr_audit_trigger();
Expand Down Expand Up @@ -128,59 +94,12 @@ export async function up(knex: Knex): Promise<void> {
create trigger audit_critter before insert or update or delete on deployment for each row execute procedure tr_audit_trigger();
create trigger journal_critter after insert or update or delete on deployment for each row execute procedure tr_journal_trigger();
----------------------------------------------------------------------------------------
-- Create Critter Observation count trigger
----------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION biohub.tr_critter_observation_count()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
-- *******************************************************************
-- Procedure: tr_critter_observation_count
-- Purpose: Validates amount of critters of a observation is less than or equal to observation count.
--
-- MODIFICATION HISTORY
-- Person Date Comments
-- ---------------- ----------- --------------------------------------
-- mac.deluca@quartech.com
-- 2023-17-08 initial release
-- *******************************************************************
DECLARE
num_critters integer := (SELECT count(*) FROM critter WHERE critter.observation_id = NEW.observation_id);
observation_total_count integer := (SELECT total_count FROM observation WHERE observation.observation_id = NEW.observation_id);
BEGIN
IF (num_critters > observation_total_count) THEN
RAISE EXCEPTION 'More individual critters than specified by observation total count';
END IF;
RETURN NEW;
end;
$function$
;
COMMENT ON FUNCTION biohub.tr_critter_observation_count() IS 'Validates amount of individual critters in an observation is less than or equal to observation total count.';
-- Create observation count trigger
CREATE TRIGGER tr_critter_observation_count AFTER INSERT ON critter FOR EACH ROW EXECUTE PROCEDURE tr_critter_observation_count();
CREATE TRIGGER tr_critter_observation_count AFTER UPDATE ON observation FOR EACH ROW EXECUTE PROCEDURE tr_critter_observation_count();
----------------------------------------------------------------------------------------
-- Create views
----------------------------------------------------------------------------------------
set search_path=biohub_dapi_v1;
create or replace view observation as select * from biohub.observation;
create or replace view critter as select * from biohub.critter;
create or replace view deployment as select * from biohub.deployment;
`);
Expand Down

0 comments on commit ec42e38

Please sign in to comment.