diff --git a/datastore/migrations/00008_AddText_Measurement_Types_down.sql b/datastore/migrations/00008_AddText_Measurement_Types_down.sql new file mode 100644 index 0000000..28b4d5d --- /dev/null +++ b/datastore/migrations/00008_AddText_Measurement_Types_down.sql @@ -0,0 +1,5 @@ +-- bactdb +-- Matthew R Dillon + +DROP TABLE text_measurement_types; + diff --git a/datastore/migrations/00008_AddText_Measurements_up.sql b/datastore/migrations/00008_AddText_Measurement_Types_up.sql similarity index 88% rename from datastore/migrations/00008_AddText_Measurements_up.sql rename to datastore/migrations/00008_AddText_Measurement_Types_up.sql index d0992ce..1f65741 100644 --- a/datastore/migrations/00008_AddText_Measurements_up.sql +++ b/datastore/migrations/00008_AddText_Measurement_Types_up.sql @@ -1,7 +1,7 @@ -- bactdb -- Matthew R Dillon -CREATE TABLE text_measurements ( +CREATE TABLE text_measurement_types ( id BIGSERIAL NOT NULL, text_measurement_name CHARACTER VARYING(100), diff --git a/datastore/migrations/00008_AddText_Measurements_down.sql b/datastore/migrations/00008_AddText_Measurements_down.sql deleted file mode 100644 index 3b8bc9b..0000000 --- a/datastore/migrations/00008_AddText_Measurements_down.sql +++ /dev/null @@ -1,5 +0,0 @@ --- bactdb --- Matthew R Dillon - -DROP TABLE text_measurements; - diff --git a/datastore/migrations/00010_AddNumerical_Measurements_down.sql b/datastore/migrations/00010_AddNumerical_Measurements_down.sql deleted file mode 100644 index b8fbc65..0000000 --- a/datastore/migrations/00010_AddNumerical_Measurements_down.sql +++ /dev/null @@ -1,5 +0,0 @@ --- bactdb --- Matthew R Dillon - -DROP TABLE numerical_measurements; - diff --git a/datastore/migrations/00010_AddNumerical_Measurements_up.sql b/datastore/migrations/00010_AddNumerical_Measurements_up.sql deleted file mode 100644 index b1deda2..0000000 --- a/datastore/migrations/00010_AddNumerical_Measurements_up.sql +++ /dev/null @@ -1,17 +0,0 @@ --- bactdb --- Matthew R Dillon - -CREATE TABLE numerical_measurements ( - id BIGSERIAL NOT NULL, - measurement_value NUMERIC(6, 4) NOT NULL, - confidence_interval NUMERIC(6,4) NULL, - unit_type_id BIGINT, - - created_at TIMESTAMP WITH TIME ZONE, - updated_at TIMESTAMP WITH TIME ZONE, - deleted_at TIMESTAMP WITH TIME ZONE, - - CONSTRAINT numerical_measurements_pkey PRIMARY KEY (id), - FOREIGN KEY (unit_type_id) REFERENCES unit_types(id) -); - diff --git a/datastore/migrations/00011_AddStrainObsMeasurements_down.sql b/datastore/migrations/00010_AddStrainObsMeasurements_down.sql similarity index 100% rename from datastore/migrations/00011_AddStrainObsMeasurements_down.sql rename to datastore/migrations/00010_AddStrainObsMeasurements_down.sql diff --git a/datastore/migrations/00010_AddStrainObsMeasurements_up.sql b/datastore/migrations/00010_AddStrainObsMeasurements_up.sql new file mode 100644 index 0000000..f28d366 --- /dev/null +++ b/datastore/migrations/00010_AddStrainObsMeasurements_up.sql @@ -0,0 +1,30 @@ +-- bactdb +-- Matthew R Dillon + +CREATE TABLE strainsobsmeasurements ( + id BIGSERIAL NOT NULL, + strainsobservations_id BIGINT, + text_measurement_type_id BIGINT NULL, + measurement_value NUMERIC(6, 4) NULL, + confidence_interval NUMERIC(6, 4) NULL, + unit_type_id BIGINT NULL, + + created_at TIMESTAMP WITH TIME ZONE, + updated_at TIMESTAMP WITH TIME ZONE, + deleted_at TIMESTAMP WITH TIME ZONE, + + CONSTRAINT strainsobsmeasurements_pkey PRIMARY KEY (id), + FOREIGN KEY (strainsobservations_id) REFERENCES strainsobservations(id), + FOREIGN KEY (text_measurement_type_id) REFERENCES text_measurement_types(id), + FOREIGN KEY (unit_type_id) REFERENCES unit_types(id), + CONSTRAINT exclusive_data_type CHECK ( + (text_measurement_type_id IS NOT NULL + AND measurement_value IS NULL + AND confidence_interval IS NULL + AND unit_type_id IS NULL) + OR + (text_measurement_type_id IS NULL + AND measurement_value IS NOT NULL)) + +); + diff --git a/datastore/migrations/00011_AddStrainObsMeasurements_up.sql b/datastore/migrations/00011_AddStrainObsMeasurements_up.sql deleted file mode 100644 index 9abf103..0000000 --- a/datastore/migrations/00011_AddStrainObsMeasurements_up.sql +++ /dev/null @@ -1,17 +0,0 @@ --- bactdb --- Matthew R Dillon - -CREATE TABLE strainsobsmeasurements ( - id BIGSERIAL NOT NULL, - strainsobservations_id BIGINT, - measurement_table CHARACTER VARYING(15), - measurement_id BIGINT, - - created_at TIMESTAMP WITH TIME ZONE, - updated_at TIMESTAMP WITH TIME ZONE, - deleted_at TIMESTAMP WITH TIME ZONE, - - CONSTRAINT strainsobsmeasurements_pkey PRIMARY KEY (id), - FOREIGN KEY (strainsobservations_id) REFERENCES strainsobservations(id) -); - diff --git a/datastore/migrations/00012_AddViewMeasurements_down.sql b/datastore/migrations/00012_AddViewMeasurements_down.sql deleted file mode 100644 index d38c298..0000000 --- a/datastore/migrations/00012_AddViewMeasurements_down.sql +++ /dev/null @@ -1,5 +0,0 @@ --- bactdb --- Matthew R Dillon - -DROP VIEW IF EXISTS v_measurements; - diff --git a/datastore/migrations/00012_AddViewMeasurements_up.sql b/datastore/migrations/00012_AddViewMeasurements_up.sql deleted file mode 100644 index 13c55ca..0000000 --- a/datastore/migrations/00012_AddViewMeasurements_up.sql +++ /dev/null @@ -1,26 +0,0 @@ --- bactdb --- Matthew R Dillon - -CREATE OR REPLACE VIEW v_measurements AS -SELECT s.strain_name, - o.observation_name, - som.measurement_table, - tm.text_measurement_name, - nm.measurement_value, - nm.confidence_interval, - nm.unit_type_id -FROM strainsobsmeasurements som -INNER JOIN strainsobservations so - ON som.strainsobservations_id = so.id -INNER JOIN strains s - ON so.strain_id = s.id -INNER JOIN observations o - ON so.observations_id = o.id -LEFT OUTER JOIN text_measurements tm - ON som.measurement_id = tm.id - AND som.measurement_table = 'text' -LEFT OUTER JOIN numerical_measurements nm - ON som.measurement_id = nm.id - AND som.measurement_table = 'num' -ORDER BY measurement_table, o.observation_name ASC -