diff --git a/fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql b/fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql index d574591e..90e3bd6c 100644 --- a/fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql @@ -1,11 +1,11 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_guaranteed_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_guaranteed_creation() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new guaranteed_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET guaranteed_ids = array_append(guaranteed_ids, NEW.id) WHERE label_dimension.label_id = NEW.label_id; ELSE @@ -17,19 +17,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS guaranteed_creation ON "fertiscan_0.0.17".guaranteed; +DROP TRIGGER IF EXISTS guaranteed_creation ON "fertiscan_0.0.18".guaranteed; CREATE TRIGGER guaranteed_creation -AFTER INSERT ON "fertiscan_0.0.17".guaranteed +AFTER INSERT ON "fertiscan_0.0.18".guaranteed FOR EACH ROW EXECUTE FUNCTION olap_guaranteed_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_guaranteed_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_guaranteed_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new guaranteed_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET guaranteed_ids = array_remove(guaranteed_ids, OLD.id) WHERE label_dimension.label_id = OLD.label_id; ELSE @@ -41,8 +41,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS guaranteed_deletion ON "fertiscan_0.0.17".guaranteed; +DROP TRIGGER IF EXISTS guaranteed_deletion ON "fertiscan_0.0.18".guaranteed; CREATE TRIGGER guaranteed_deletion -AFTER DELETE ON "fertiscan_0.0.17".guaranteed +AFTER DELETE ON "fertiscan_0.0.18".guaranteed FOR EACH ROW EXECUTE FUNCTION olap_guaranteed_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/ingredient_trigger.sql b/fertiscan/db/bytebase/OLAP/ingredient_trigger.sql index faa33063..2078fef4 100644 --- a/fertiscan/db/bytebase/OLAP/ingredient_trigger.sql +++ b/fertiscan/db/bytebase/OLAP/ingredient_trigger.sql @@ -1,11 +1,11 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_ingredient_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_ingredient_creation() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new ingredient_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET ingredient_ids = array_append(ingredient_ids, NEW.id) WHERE label_dimension.label_id = NEW.label_id; ELSE @@ -17,19 +17,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS ingredient_creation ON "fertiscan_0.0.17".ingredient; +DROP TRIGGER IF EXISTS ingredient_creation ON "fertiscan_0.0.18".ingredient; CREATE TRIGGER ingredient_creation -AFTER INSERT ON "fertiscan_0.0.17".ingredient +AFTER INSERT ON "fertiscan_0.0.18".ingredient FOR EACH ROW EXECUTE FUNCTION olap_ingredient_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_ingredient_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_ingredient_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new ingredient_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET ingredient_ids = array_remove(ingredient_ids, OLD.id) WHERE label_dimension.label_id = OLD.label_id; ELSE @@ -41,8 +41,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS ingredient_deletion ON "fertiscan_0.0.17".ingredient; +DROP TRIGGER IF EXISTS ingredient_deletion ON "fertiscan_0.0.18".ingredient; CREATE TRIGGER ingredient_deletion -AFTER DELETE ON "fertiscan_0.0.17".ingredient +AFTER DELETE ON "fertiscan_0.0.18".ingredient FOR EACH ROW EXECUTE FUNCTION olap_ingredient_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/inspection_triggers.sql b/fertiscan/db/bytebase/OLAP/inspection_triggers.sql index f89b857a..c62e67cd 100644 --- a/fertiscan/db/bytebase/OLAP/inspection_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/inspection_triggers.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_inspection_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_inspection_creation() RETURNS TRIGGER AS $$ DECLARE time_id UUID; @@ -7,7 +7,7 @@ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) AND (NEW.label_info_id IS NOT NULL) THEN -- Time Dimension - INSERT INTO "fertiscan_0.0.17".time_dimension ( + INSERT INTO "fertiscan_0.0.18".time_dimension ( date_value, year,month,day) VALUES ( CURRENT_DATE, @@ -16,7 +16,7 @@ BEGIN EXTRACT(DAY FROM CURRENT_DATE) ) RETURNING id INTO time_id; -- Create the Inspection_factual entry - INSERT INTO "fertiscan_0.0.17".inspection_factual ( + INSERT INTO "fertiscan_0.0.18".inspection_factual ( inspection_id, inspector_id, label_info_id, time_id, sample_id, company_id, manufacturer_id, picture_set_id, original_dataset ) VALUES ( NEW.id, @@ -38,19 +38,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS inspection_creation ON "fertiscan_0.0.17".inspection; +DROP TRIGGER IF EXISTS inspection_creation ON "fertiscan_0.0.18".inspection; CREATE TRIGGER inspection_creation -AFTER INSERT ON "fertiscan_0.0.17".inspection +AFTER INSERT ON "fertiscan_0.0.18".inspection FOR EACH ROW EXECUTE FUNCTION olap_inspection_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_inspection_update() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_inspection_update() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'UPDATE') THEN IF (NEW.id IS NOT NULL) THEN IF (NEW.label_info_id != OLD.label_info_id) OR (NEW.inspector_id != OLD.inspector_id) OR (NEW.picture_set_id != OLD.picture_set_id) THEN - UPDATE "fertiscan_0.0.17".inspection_factual + UPDATE "fertiscan_0.0.18".inspection_factual SET inspector_id = NEW.inspector_id, label_info_id = NEW.label_info_id, picture_set_id = NEW.picture_set_id WHERE inspection_id = NEW.id; END IF; @@ -63,18 +63,18 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS inspection_update ON "fertiscan_0.0.17".inspection; +DROP TRIGGER IF EXISTS inspection_update ON "fertiscan_0.0.18".inspection; CREATE TRIGGER inspection_update -BEFORE UPDATE ON "fertiscan_0.0.17".inspection +BEFORE UPDATE ON "fertiscan_0.0.18".inspection FOR EACH ROW EXECUTE FUNCTION olap_inspection_update(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_inspection_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_inspection_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) THEN - DELETE FROM "fertiscan_0.0.17".inspection_factual + DELETE FROM "fertiscan_0.0.18".inspection_factual WHERE inspection_id = OLD.id; ELSE -- Raise a warning if the condition is not met @@ -85,8 +85,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS inspection_deletion ON "fertiscan_0.0.17".inspection; +DROP TRIGGER IF EXISTS inspection_deletion ON "fertiscan_0.0.18".inspection; CREATE TRIGGER inspection_deletion -AFTER DELETE ON "fertiscan_0.0.17".inspection +AFTER DELETE ON "fertiscan_0.0.18".inspection FOR EACH ROW EXECUTE FUNCTION olap_inspection_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/label_information_triggers.sql b/fertiscan/db/bytebase/OLAP/label_information_triggers.sql index ebbe70d5..ddca34c2 100644 --- a/fertiscan/db/bytebase/OLAP/label_information_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/label_information_triggers.sql @@ -1,10 +1,10 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_label_information_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_label_information_creation() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) THEN - INSERT INTO "fertiscan_0.0.17"."label_dimension" ( + INSERT INTO "fertiscan_0.0.18"."label_dimension" ( label_id ) VALUES ( NEW.id @@ -18,19 +18,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS label_information_creation ON "fertiscan_0.0.17".label_information; +DROP TRIGGER IF EXISTS label_information_creation ON "fertiscan_0.0.18".label_information; CREATE TRIGGER label_information_creation -AFTER INSERT ON "fertiscan_0.0.17".label_information +AFTER INSERT ON "fertiscan_0.0.18".label_information FOR EACH ROW EXECUTE FUNCTION olap_label_information_creation(); --- CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_label_information_update() +-- CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_label_information_update() -- RETURNS TRIGGER AS $$ -- BEGIN -- IF (TG_OP = 'UPDATE') THEN -- IF (NEW.id IS NOT NULL) THEN -- IF (NEW.company_info_id !=OLD.company_info_id) OR (NEW.manufacturer_info_id != OLD.manufacturer_info_id) THEN --- UPDATE "fertiscan_0.0.17"."label_dimension" +-- UPDATE "fertiscan_0.0.18"."label_dimension" -- SET company_info_id = NEW.company_info_id, manufacturer_info_id = NEW.manufacturer_info_id -- WHERE label_id = NEW.id; -- END IF; @@ -43,18 +43,18 @@ EXECUTE FUNCTION olap_label_information_creation(); -- END; -- $$ LANGUAGE plpgsql; --- DROP TRIGGER IF EXISTS label_information_update ON "fertiscan_0.0.17".label_information; +-- DROP TRIGGER IF EXISTS label_information_update ON "fertiscan_0.0.18".label_information; -- CREATE TRIGGER label_information_update --- BEFORE UPDATE ON "fertiscan_0.0.17".label_information +-- BEFORE UPDATE ON "fertiscan_0.0.18".label_information -- FOR EACH ROW -- EXECUTE FUNCTION olap_label_information_update(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_label_information_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_label_information_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) THEN - DELETE FROM "fertiscan_0.0.17"."label_dimension" + DELETE FROM "fertiscan_0.0.18"."label_dimension" WHERE label_id = OLD.id; ELSE -- Raise a warning if the condition is not met @@ -65,8 +65,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS label_information_deletion ON "fertiscan_0.0.17".label_information; +DROP TRIGGER IF EXISTS label_information_deletion ON "fertiscan_0.0.18".label_information; CREATE TRIGGER label_information_deletion -AFTER DELETE ON "fertiscan_0.0.17".label_information +AFTER DELETE ON "fertiscan_0.0.18".label_information FOR EACH ROW EXECUTE FUNCTION olap_label_information_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/metrics_triggers.sql b/fertiscan/db/bytebase/OLAP/metrics_triggers.sql index e8a95cb4..104c2c0f 100644 --- a/fertiscan/db/bytebase/OLAP/metrics_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/metrics_triggers.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_metrics_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_metrics_creation() RETURNS TRIGGER AS $$ DECLARE metric_type TEXT; @@ -11,7 +11,7 @@ BEGIN IF (metric_type ILIKE 'test%') THEN RETURN NEW; END IF; - EXECUTE format('UPDATE "fertiscan_0.0.17"."label_dimension" + EXECUTE format('UPDATE "fertiscan_0.0.18"."label_dimension" SET %I = array_append(%I, %L) WHERE label_dimension.label_id = %L', metric_type, metric_type, NEW.id, NEW.label_id); @@ -24,13 +24,13 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS metrics_creation ON "fertiscan_0.0.17".metric; +DROP TRIGGER IF EXISTS metrics_creation ON "fertiscan_0.0.18".metric; CREATE TRIGGER metrics_creation -AFTER INSERT ON "fertiscan_0.0.17".metric +AFTER INSERT ON "fertiscan_0.0.18".metric FOR EACH ROW EXECUTE FUNCTION olap_metrics_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_metrics_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_metrics_deletion() RETURNS TRIGGER AS $$ DECLARE metric_type TEXT; @@ -42,7 +42,7 @@ BEGIN IF (metric_type ILIKE 'test%') THEN RETURN OLD; END IF; - EXECUTE format('UPDATE "fertiscan_0.0.17"."label_dimension" + EXECUTE format('UPDATE "fertiscan_0.0.18"."label_dimension" SET %I = array_remove(%I, %L) WHERE label_dimension.label_id = %L', metric_type, metric_type, OLD.id, OLD.label_id); @@ -55,8 +55,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS metrics_deletion ON "fertiscan_0.0.17".metric; +DROP TRIGGER IF EXISTS metrics_deletion ON "fertiscan_0.0.18".metric; CREATE TRIGGER metrics_deletion -AFTER DELETE ON "fertiscan_0.0.17".metric +AFTER DELETE ON "fertiscan_0.0.18".metric FOR EACH ROW EXECUTE FUNCTION olap_metrics_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/micronutrient_triggers.sql b/fertiscan/db/bytebase/OLAP/micronutrient_triggers.sql index e1587a57..f23fefa1 100644 --- a/fertiscan/db/bytebase/OLAP/micronutrient_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/micronutrient_triggers.sql @@ -1,11 +1,11 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_micronutrient_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_micronutrient_creation() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new micronutrient_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET micronutrient_ids = array_append(micronutrient_ids, NEW.id) WHERE label_dimension.label_id = NEW.label_id; ELSE @@ -17,19 +17,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS micronutrient_creation ON "fertiscan_0.0.17".micronutrient; +DROP TRIGGER IF EXISTS micronutrient_creation ON "fertiscan_0.0.18".micronutrient; CREATE TRIGGER micronutrient_creation -AFTER INSERT ON "fertiscan_0.0.17".micronutrient +AFTER INSERT ON "fertiscan_0.0.18".micronutrient FOR EACH ROW EXECUTE FUNCTION olap_micronutrient_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_micronutrient_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_micronutrient_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new micronutrient_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET micronutrient_ids = array_remove(micronutrient_ids, OLD.id) WHERE label_dimension.label_id = OLD.label_id; ELSE @@ -41,8 +41,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS micronutrient_deletion ON "fertiscan_0.0.17".micronutrient; +DROP TRIGGER IF EXISTS micronutrient_deletion ON "fertiscan_0.0.18".micronutrient; CREATE TRIGGER micronutrient_deletion -AFTER DELETE ON "fertiscan_0.0.17".micronutrient +AFTER DELETE ON "fertiscan_0.0.18".micronutrient FOR EACH ROW EXECUTE FUNCTION olap_micronutrient_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/organization_triggers.sql b/fertiscan/db/bytebase/OLAP/organization_triggers.sql index 17d04b77..e897b694 100644 --- a/fertiscan/db/bytebase/OLAP/organization_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/organization_triggers.sql @@ -1,10 +1,10 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_organization_information_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_organization_information_creation() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) THEN - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET organization_info_ids = array_append(organization_info_ids, NEW.id) WHERE label_dimension.label_id = NEW.label_id; ELSE @@ -16,18 +16,18 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS organization_information_creation ON "fertiscan_0.0.17".organization_information; +DROP TRIGGER IF EXISTS organization_information_creation ON "fertiscan_0.0.18".organization_information; CREATE TRIGGER organization_information_creation -AFTER INSERT ON "fertiscan_0.0.17".organization_information +AFTER INSERT ON "fertiscan_0.0.18".organization_information FOR EACH ROW EXECUTE FUNCTION olap_organization_information_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_organization_information_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_organization_information_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) THEN - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET organization_info_ids = array_remove(organization_info_ids, OLD.id) WHERE label_dimension.label_id = OLD.label_id; ELSE diff --git a/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql b/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql index 6613ecca..fcccc1ef 100644 --- a/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql @@ -1,5 +1,5 @@ -- Trigger function -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_registration_number_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_registration_number_creation() RETURNS TRIGGER AS $$ BEGIN -- Check if the operation is an INSERT @@ -7,7 +7,7 @@ BEGIN -- Check if the NEW.id is not NULL IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) AND (NEW.identifier IS NOT NULL) THEN -- Update the label_dimension table with the new guaranteed_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET registration_number_ids = array_append(registration_number_ids, NEW.id) WHERE label_dimension.label_id = NEW.label_id; ELSE @@ -20,14 +20,14 @@ END; $$ LANGUAGE plpgsql; -- Trigger -DROP TRIGGER IF EXISTS registration_number_creation ON "fertiscan_0.0.17".registration_number_information; +DROP TRIGGER IF EXISTS registration_number_creation ON "fertiscan_0.0.18".registration_number_information; CREATE TRIGGER registration_number_creation -AFTER INSERT ON "fertiscan_0.0.17".registration_number_information +AFTER INSERT ON "fertiscan_0.0.18".registration_number_information FOR EACH ROW EXECUTE FUNCTION olap_registration_number_creation(); -- Trigger function -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_registration_number_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_registration_number_deletion() RETURNS TRIGGER AS $$ BEGIN -- Check if the operation is an INSERT @@ -35,7 +35,7 @@ BEGIN -- Check if the NEW.id is not NULL IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new guaranteed_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET registration_number_ids = array_remove(registration_number_ids, OLD.id) WHERE label_dimension.label_id = OLD.label_id; ELSE @@ -48,8 +48,8 @@ END; $$ LANGUAGE plpgsql; -- Trigger -DROP TRIGGER IF EXISTS registration_number_deletion ON "fertiscan_0.0.17".registration_number_information; +DROP TRIGGER IF EXISTS registration_number_deletion ON "fertiscan_0.0.18".registration_number_information; CREATE TRIGGER registration_number_deletion -AFTER INSERT ON "fertiscan_0.0.17".registration_number_information +AFTER INSERT ON "fertiscan_0.0.18".registration_number_information FOR EACH ROW EXECUTE FUNCTION olap_registration_number_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/specification_triggers.sql b/fertiscan/db/bytebase/OLAP/specification_triggers.sql index 6e37cea3..840e0678 100644 --- a/fertiscan/db/bytebase/OLAP/specification_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/specification_triggers.sql @@ -1,11 +1,11 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_specification_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_specification_creation() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new specification_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET specification_ids = array_append(specification_ids, NEW.id) WHERE label_dimension.label_id = NEW.label_id; ELSE @@ -17,19 +17,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS specification_creation ON "fertiscan_0.0.17".specification; +DROP TRIGGER IF EXISTS specification_creation ON "fertiscan_0.0.18".specification; CREATE TRIGGER specification_creation -AFTER INSERT ON "fertiscan_0.0.17".specification +AFTER INSERT ON "fertiscan_0.0.18".specification FOR EACH ROW EXECUTE FUNCTION olap_specification_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_specification_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_specification_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN -- Update the label_dimension table with the new specification_analysis_id - UPDATE "fertiscan_0.0.17"."label_dimension" + UPDATE "fertiscan_0.0.18"."label_dimension" SET specification_ids = array_remove(specification_ids, OLD.id) WHERE label_dimension.label_id = OLD.label_id; ELSE @@ -41,8 +41,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS specification_deletion ON "fertiscan_0.0.17".specification; +DROP TRIGGER IF EXISTS specification_deletion ON "fertiscan_0.0.18".specification; CREATE TRIGGER specification_deletion -AFTER DELETE ON "fertiscan_0.0.17".specification +AFTER DELETE ON "fertiscan_0.0.18".specification FOR EACH ROW EXECUTE FUNCTION olap_specification_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/sub_label_triggers.sql b/fertiscan/db/bytebase/OLAP/sub_label_triggers.sql index 8fc8bba1..e8ba9298 100644 --- a/fertiscan/db/bytebase/OLAP/sub_label_triggers.sql +++ b/fertiscan/db/bytebase/OLAP/sub_label_triggers.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_sub_label_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_sub_label_creation() RETURNS TRIGGER AS $$ DECLARE type_str TEXT; @@ -7,12 +7,12 @@ BEGIN IF (TG_OP = 'INSERT') THEN IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) AND (NEW.sub_type_id IS NOT NULL) THEN -- FIND THE SUB_TYPE TO GET THE COLUMN IDENTIFIER - SELECT sub_type.type_en INTO type_str FROM "fertiscan_0.0.17".sub_type WHERE sub_type.id = NEW.sub_type_id; + SELECT sub_type.type_en INTO type_str FROM "fertiscan_0.0.18".sub_type WHERE sub_type.id = NEW.sub_type_id; IF (type_str ILIKE 'test%') THEN RETURN NEW; -- Do not update the OLAP dimension for test sub_labels END IF; type_str = type_str || '_ids'; - EXECUTE format('UPDATE "fertiscan_0.0.17".label_dimension + EXECUTE format('UPDATE "fertiscan_0.0.18".label_dimension SET %I = array_append(%I, %L) WHERE label_id = %L;', type_str, type_str, NEW.id, NEW.label_id); @@ -23,13 +23,13 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS sub_label_creation ON "fertiscan_0.0.17".sub_label; +DROP TRIGGER IF EXISTS sub_label_creation ON "fertiscan_0.0.18".sub_label; CREATE TRIGGER sub_label_creation -AFTER INSERT ON "fertiscan_0.0.17".sub_label +AFTER INSERT ON "fertiscan_0.0.18".sub_label FOR EACH ROW EXECUTE FUNCTION olap_sub_label_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_sub_label_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".olap_sub_label_deletion() RETURNS TRIGGER AS $$ DECLARE type_str TEXT; @@ -37,12 +37,12 @@ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) AND (OLD.sub_type_id IS NOT NULL) THEN -- FIND THE SUB_TYPE TO GET THE COLUMN IDENTIFIER - SELECT sub_type.type_en INTO type_str FROM "fertiscan_0.0.17".sub_type WHERE sub_type.id = OLD.sub_type_id; + SELECT sub_type.type_en INTO type_str FROM "fertiscan_0.0.18".sub_type WHERE sub_type.id = OLD.sub_type_id; IF (type_str ILIKE 'test%') THEN RETURN OLD; -- Do not update the OLAP dimension for test sub_labels END IF; type_str = type_str || '_ids'; - EXECUTE format('UPDATE "fertiscan_0.0.17".label_dimension + EXECUTE format('UPDATE "fertiscan_0.0.18".label_dimension SET %I = array_remove(%I, %L) WHERE label_id = %L;', type_str, type_str, OLD.id, OLD.label_id); @@ -55,8 +55,8 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS sub_label_deletion ON "fertiscan_0.0.17".sub_label; +DROP TRIGGER IF EXISTS sub_label_deletion ON "fertiscan_0.0.18".sub_label; CREATE TRIGGER sub_label_deletion -AFTER DELETE ON "fertiscan_0.0.17".sub_label +AFTER DELETE ON "fertiscan_0.0.18".sub_label FOR EACH ROW EXECUTE FUNCTION olap_sub_label_deletion(); diff --git a/fertiscan/db/bytebase/delete_inspection_function.sql b/fertiscan/db/bytebase/delete_inspection_function.sql index cea09081..a433e0ee 100644 --- a/fertiscan/db/bytebase/delete_inspection_function.sql +++ b/fertiscan/db/bytebase/delete_inspection_function.sql @@ -1,15 +1,15 @@ -- To avoid potential schema drift issues -SET search_path TO "fertiscan_0.0.17"; +SET search_path TO "fertiscan_0.0.18"; -- Trigger function to handle after organization_information delete for location deletion -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".after_org_info_delete_location_trig() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".after_org_info_delete_location_trig() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN IF OLD.location_id IS NOT NULL THEN BEGIN - DELETE FROM "fertiscan_0.0.17".location + DELETE FROM "fertiscan_0.0.18".location WHERE id = OLD.location_id; EXCEPTION WHEN foreign_key_violation THEN RAISE NOTICE 'Location % is still referenced by another record and cannot be deleted.', OLD.location_id; @@ -21,14 +21,14 @@ END; $$; -- Trigger definition on organization_information table for location deletion -DROP TRIGGER IF EXISTS after_organization_information_delete_location ON "fertiscan_0.0.17".organization_information; +DROP TRIGGER IF EXISTS after_organization_information_delete_location ON "fertiscan_0.0.18".organization_information; CREATE TRIGGER after_organization_information_delete_location -AFTER DELETE ON "fertiscan_0.0.17".organization_information +AFTER DELETE ON "fertiscan_0.0.18".organization_information FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.17".after_org_info_delete_location_trig(); +EXECUTE FUNCTION "fertiscan_0.0.18".after_org_info_delete_location_trig(); -- Function to delete an inspection and related data -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".delete_inspection( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".delete_inspection( p_inspection_id uuid, p_inspector_id uuid ) @@ -40,7 +40,7 @@ DECLARE BEGIN IF NOT EXISTS ( SELECT 1 - FROM "fertiscan_0.0.17".inspection + FROM "fertiscan_0.0.18".inspection WHERE id = p_inspection_id AND inspector_id = p_inspector_id ) THEN @@ -49,7 +49,7 @@ BEGIN -- Delete the inspection record and retrieve it WITH deleted_inspection AS ( - DELETE FROM "fertiscan_0.0.17".inspection + DELETE FROM "fertiscan_0.0.18".inspection WHERE id = p_inspection_id RETURNING * ) @@ -63,28 +63,48 @@ END; $$; -- Combined trigger function to handle after inspection delete for sample and label_information deletion -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".after_insp_delete_cleanup_trig() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".after_insp_delete_cleanup_trig() RETURNS TRIGGER LANGUAGE plpgsql AS $$ +DECLARE + newest_inspection_id uuid; BEGIN IF OLD.sample_id IS NOT NULL THEN - DELETE FROM "fertiscan_0.0.17".sample + DELETE FROM "fertiscan_0.0.18".sample WHERE id = OLD.sample_id; END IF; IF OLD.label_info_id IS NOT NULL THEN - DELETE FROM "fertiscan_0.0.17".label_information + DELETE FROM "fertiscan_0.0.18".label_information WHERE id = OLD.label_info_id; END IF; + IF OLD.fertilizer_id IS NOT NULL THEN + -- Select the newest inspection for the fertilizer.latest_inspection_id + SELECT id INTO newest_inspection_id + from "fertiscan_0.0.18".inspection + WHERE fertilizer_id = OLD.fertilizer_id AND verified_date IS NOT NULL AND id != OLD.id + ORDER BY verified_date DESC + LIMIT 1; + + if newest_inspection_id IS NOT NULL THEN + UPDATE "fertiscan_0.0.18".fertilizer + SET latest_inspection_id = newest_inspection_id + WHERE id = OLD.fertilizer_id; + ELSE + --This was the only inspection for the fertilizer so we delete it + DELETE FROM "fertiscan_0.0.18".fertilizer + WHERE id = OLD.fertilizer_id; + END IF; + RETURN NULL; END; $$; -- Trigger definition on inspection table for combined cleanup (sample and label_information deletion) -DROP TRIGGER IF EXISTS after_inspection_delete_cleanup ON "fertiscan_0.0.17".inspection; +DROP TRIGGER IF EXISTS after_inspection_delete_cleanup ON "fertiscan_0.0.18".inspection; CREATE TRIGGER after_inspection_delete_cleanup -AFTER DELETE ON "fertiscan_0.0.17".inspection +AFTER DELETE ON "fertiscan_0.0.18".inspection FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.17".after_insp_delete_cleanup_trig(); +EXECUTE FUNCTION "fertiscan_0.0.18".after_insp_delete_cleanup_trig(); diff --git a/fertiscan/db/bytebase/get_inspection/get_guaranteed_analysis.sql b/fertiscan/db/bytebase/get_inspection/get_guaranteed_analysis.sql index e97540b0..e652cfb7 100644 --- a/fertiscan/db/bytebase/get_inspection/get_guaranteed_analysis.sql +++ b/fertiscan/db/bytebase/get_inspection/get_guaranteed_analysis.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_guaranteed_analysis_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_guaranteed_analysis_json( label_info_id uuid ) RETURNS jsonb @@ -28,7 +28,7 @@ BEGIN ) FILTER (WHERE guaranteed.language = 'fr'), '[]'::jsonb) ) INTO result_json - FROM "fertiscan_0.0.17".guaranteed + FROM "fertiscan_0.0.18".guaranteed WHERE guaranteed.label_id = label_info_id; -- build Guaranteed_analysis title json @@ -38,7 +38,7 @@ BEGIN 'is_minimal', title_is_minimal ) INTO result_json_title - FROM "fertiscan_0.0.17".label_information + FROM "fertiscan_0.0.18".label_information WHERE id = label_info_id; -- merge JSONs diff --git a/fertiscan/db/bytebase/get_inspection/get_ingredients_json.sql b/fertiscan/db/bytebase/get_inspection/get_ingredients_json.sql index fa8ea2ed..5df6576d 100644 --- a/fertiscan/db/bytebase/get_inspection/get_ingredients_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_ingredients_json.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_ingredients_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_ingredients_json( label_info_id uuid) RETURNS jsonb LANGUAGE plpgsql diff --git a/fertiscan/db/bytebase/get_inspection/get_label_info_json.sql b/fertiscan/db/bytebase/get_inspection/get_label_info_json.sql index efae1a8b..011cd1d1 100644 --- a/fertiscan/db/bytebase/get_inspection/get_label_info_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_label_info_json.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_label_info_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_label_info_json( label_id uuid) RETURNS jsonb LANGUAGE plpgsql diff --git a/fertiscan/db/bytebase/get_inspection/get_metrics_json.sql b/fertiscan/db/bytebase/get_inspection/get_metrics_json.sql index f369b211..93c340ec 100644 --- a/fertiscan/db/bytebase/get_inspection/get_metrics_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_metrics_json.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_metrics_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_metrics_json( label_info_id uuid ) RETURNS jsonb diff --git a/fertiscan/db/bytebase/get_inspection/get_micronutrients_json.sql b/fertiscan/db/bytebase/get_inspection/get_micronutrients_json.sql index 86b0f3a0..420f51d8 100644 --- a/fertiscan/db/bytebase/get_inspection/get_micronutrients_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_micronutrients_json.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_micronutrient_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_micronutrient_json( label_info_id uuid) RETURNS jsonb LANGUAGE plpgsql diff --git a/fertiscan/db/bytebase/get_inspection/get_organizations_json.sql b/fertiscan/db/bytebase/get_inspection/get_organizations_json.sql index 9cc2f264..5d3a2e11 100644 --- a/fertiscan/db/bytebase/get_inspection/get_organizations_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_organizations_json.sql @@ -1,4 +1,7 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_organizations_information_json( + +--Unverified organization data +DROP IF EXISTS FUNCTION "fertiscan_0.0.18".get_organizations_information_json(label_id uuid); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_organizations_information_json( label_id uuid) RETURNS jsonb LANGUAGE plpgsql @@ -7,11 +10,11 @@ DECLARE result_json jsonb; BEGIN SELECT jsonb_agg(jsonb_build_object( - 'organization', + 'organizations', jsonb_build_object( 'id', COALESCE(org.id, Null), 'name', COALESCE(org.name, Null), - 'address', COALESCE(location.address, Null), + 'address', COALESCE(org.address, Null), 'phone_number', COALESCE(org.phone_number, Null), 'website', COALESCE(org.website, Null), 'edited', COALESCE(org.edited, Null), @@ -24,3 +27,28 @@ BEGIN RETURN result_json; END; $function$; + +-- verified organization +DROP IF EXISTS FUNCTION "fertiscan_0.0.18".get_organizations_json(); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_organizations_json() +RETURNS jsonb +LANGUAGE plpgsql +AS $function$ +DECLARE + result_json jsonb; +BEGIN + SELECT jsonb_agg(jsonb_build_object( + 'organizations', + jsonb_build_object( + 'id', COALESCE(org.id, Null), + 'name', COALESCE(org.name, Null), + 'address', COALESCE(org.address, Null), + 'phone_number', COALESCE(org.phone_number, Null), + 'website', COALESCE(org.website, Null), + 'updated_at', COALESCE(org.updated_at, Null), + ) + )) + INTO result_json + FROM organization_information as org; + RETURN result_json; +END; diff --git a/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql b/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql index cf599ae7..cf2e372a 100644 --- a/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_registration_numbers_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_registration_numbers_json( label_info_id uuid) RETURNS jsonb LANGUAGE plpgsql diff --git a/fertiscan/db/bytebase/get_inspection/get_specification_json.sql b/fertiscan/db/bytebase/get_inspection/get_specification_json.sql index 1b3ad288..5dc0cce9 100644 --- a/fertiscan/db/bytebase/get_inspection/get_specification_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_specification_json.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_specification_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_specification_json( label_info_id uuid) RETURNS jsonb LANGUAGE plpgsql diff --git a/fertiscan/db/bytebase/get_inspection/get_sub_label.sql b/fertiscan/db/bytebase/get_inspection/get_sub_label.sql index 8ff354be..4393c15f 100644 --- a/fertiscan/db/bytebase/get_inspection/get_sub_label.sql +++ b/fertiscan/db/bytebase/get_inspection/get_sub_label.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_sub_label_json(label_info_id uuid) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_sub_label_json(label_info_id uuid) RETURNS jsonb LANGUAGE plpgsql AS $function$ diff --git a/fertiscan/db/bytebase/new_inspection/new_guaranteed_analysis.sql b/fertiscan/db/bytebase/new_inspection/new_guaranteed_analysis.sql index 3282b899..9f865123 100644 --- a/fertiscan/db/bytebase/new_inspection/new_guaranteed_analysis.sql +++ b/fertiscan/db/bytebase/new_inspection/new_guaranteed_analysis.sql @@ -1,9 +1,9 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_guaranteed_analysis( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_guaranteed_analysis( name TEXT, value FLOAT, unit TEXT, label_id UUID, -language "fertiscan_0.0.17".language, +language "fertiscan_0.0.18".language, edited BOOLEAN = FALSE, element_id int = NULL ) diff --git a/fertiscan/db/bytebase/new_inspection/new_ingredient.sql b/fertiscan/db/bytebase/new_inspection/new_ingredient.sql index c8b8e613..07438b89 100644 --- a/fertiscan/db/bytebase/new_inspection/new_ingredient.sql +++ b/fertiscan/db/bytebase/new_inspection/new_ingredient.sql @@ -1,9 +1,9 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_ingredient( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_ingredient( name TEXT, value FLOAt, read_unit TEXT, label_id UUID, -language "fertiscan_0.0.17".language, +language "fertiscan_0.0.18".language, organic BOOLEAN, active BOOLEAN, edited BOOLEAN = FALSE diff --git a/fertiscan/db/bytebase/new_inspection/new_label_information.sql b/fertiscan/db/bytebase/new_inspection/new_label_information.sql index 4db98c1e..004bfb02 100644 --- a/fertiscan/db/bytebase/new_inspection/new_label_information.sql +++ b/fertiscan/db/bytebase/new_inspection/new_label_information.sql @@ -1,5 +1,5 @@ -drOp FUNCTION IF EXISTS "fertiscan_0.0.17".new_label_information(TEXT, TEXT, TEXT, FLOAT, FLOAT, FLOAT, TEXT, TEXT, BOOLEAN, UUID, UUID); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_label_information( +drOp FUNCTION IF EXISTS "fertiscan_0.0.18".new_label_information(TEXT, TEXT, TEXT, FLOAT, FLOAT, FLOAT, TEXT, TEXT, BOOLEAN, UUID, UUID); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_label_information( name TEXT, lot_number TEXT, npk TEXT, @@ -18,7 +18,7 @@ DECLARE label_id uuid; record RECORD; BEGIN - SET SEARCH_PATH TO "fertiscan_0.0.17"; + SET SEARCH_PATH TO "fertiscan_0.0.18"; -- LABEL INFORMATION INSERT INTO label_information ( product_name,lot_number, npk, n, p, k, guaranteed_title_en, guaranteed_title_fr, title_is_minimal, record_keeping diff --git a/fertiscan/db/bytebase/new_inspection/new_metric_unit.sql b/fertiscan/db/bytebase/new_inspection/new_metric_unit.sql index 04ab341c..0adc4bc4 100644 --- a/fertiscan/db/bytebase/new_inspection/new_metric_unit.sql +++ b/fertiscan/db/bytebase/new_inspection/new_metric_unit.sql @@ -1,8 +1,8 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_metric_unit( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_metric_unit( value FLOAT, read_unit TEXT, label_id UUID, - metric_type "fertiscan_0.0.17".metric_type, + metric_type "fertiscan_0.0.18".metric_type, edited BOOLEAN = FALSE ) RETURNS UUID diff --git a/fertiscan/db/bytebase/new_inspection/new_micronutrient.sql b/fertiscan/db/bytebase/new_inspection/new_micronutrient.sql index 6e11300a..59c7a9f5 100644 --- a/fertiscan/db/bytebase/new_inspection/new_micronutrient.sql +++ b/fertiscan/db/bytebase/new_inspection/new_micronutrient.sql @@ -1,9 +1,9 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_micronutrient( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_micronutrient( name TEXT, value FLOAT, unit TEXT, label_id UUID, -language "fertiscan_0.0.17".language, +language "fertiscan_0.0.18".language, edited BOOLEAN = FALSE, element_id int = NULL ) diff --git a/fertiscan/db/bytebase/new_inspection/new_organization_information.sql b/fertiscan/db/bytebase/new_inspection/new_organization_information.sql new file mode 100644 index 00000000..f1647203 --- /dev/null +++ b/fertiscan/db/bytebase/new_inspection/new_organization_information.sql @@ -0,0 +1,40 @@ + +DROP FUNCTION IF EXISTS "fertiscan_0.0.18".new_organization_located(TEXT, TEXT, TEXT, TEXT, BOOLEAN, UUID, BOOLEAN); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_organization_information( + name TEXT, + address_str TEXT, + website TEXT, + phone_number TEXT, + edited BOOLEAN = FALSE, + label_id UUID = NULL, + is_main_contact_val BOOLEAN = FALSE + ) +RETURNS uuid +LANGUAGE plpgsql +AS $function$ +DECLARE + record RECORD; + organization_id uuid; +BEGIN +-- CHECK IF ANY OF THE INPUTS ARE NOT NULL +IF COALESCE(name, address_str, website, phone_number,'') = '' THEN + RAISE EXCEPTION 'ALL of the input parameters are null'; +END IF; +IF label_id IS NULL THEN + RAISE EXCEPTION 'Label_id cannot be null when creating an organization_information'; +END IF; + INSERT INTO organization_information ("name","website","phone_number","address","edited","label_id","is_main_contact") + VALUES ( + name, + website, + phone_number, + address_str, + edited, + label_id, + is_main_contact_val + ) + RETURNING id INTO organization_id; + + RETURN organization_id; +END; +$function$; diff --git a/fertiscan/db/bytebase/new_inspection/new_organization_located.sql b/fertiscan/db/bytebase/new_inspection/new_organization_located.sql index d72674e5..0d92aae2 100644 --- a/fertiscan/db/bytebase/new_inspection/new_organization_located.sql +++ b/fertiscan/db/bytebase/new_inspection/new_organization_located.sql @@ -1,6 +1,6 @@ -DROP FUNCTION IF EXISTS "fertiscan_0.0.17".new_organization_located(TEXT, TEXT, TEXT, TEXT, BOOLEAN, UUID, BOOLEAN); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_organization_info_located( +DROP FUNCTION IF EXISTS "fertiscan_0.0.18".new_organization_located(TEXT, TEXT, TEXT, TEXT, BOOLEAN, UUID, BOOLEAN); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_organization_info_located( name TEXT, address_str TEXT, website TEXT, diff --git a/fertiscan/db/bytebase/new_inspection/new_registration_number.sql b/fertiscan/db/bytebase/new_inspection/new_registration_number.sql index 9c8bbb6c..e0e2eaab 100644 --- a/fertiscan/db/bytebase/new_inspection/new_registration_number.sql +++ b/fertiscan/db/bytebase/new_inspection/new_registration_number.sql @@ -1,6 +1,6 @@ -DROP FUNCTION IF EXISTS "fertiscan_0.0.17".new_registration_number(TEXT, UUID,BOOLEAN, TEXT, BOOLEAN); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_registration_number( +DROP FUNCTION IF EXISTS "fertiscan_0.0.18".new_registration_number(TEXT, UUID,BOOLEAN, TEXT, BOOLEAN); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_registration_number( identifier TEXT, label_info_id UUID, is_an_ingredient_val BOOLEAN DEFAULT NULL, diff --git a/fertiscan/db/bytebase/new_inspection/new_specification.sql b/fertiscan/db/bytebase/new_inspection/new_specification.sql index 2c98e107..de586aea 100644 --- a/fertiscan/db/bytebase/new_inspection/new_specification.sql +++ b/fertiscan/db/bytebase/new_inspection/new_specification.sql @@ -1,8 +1,8 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_specification( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_specification( humidity FLOAT, ph FLOAT, solubility FLOAT, -language "fertiscan_0.0.17".language, +language "fertiscan_0.0.18".language, label_id UUID, edited BOOLEAN = FALSE ) diff --git a/fertiscan/db/bytebase/new_inspection/new_sub_label.sql b/fertiscan/db/bytebase/new_inspection/new_sub_label.sql index 99da6536..9f4cb8c1 100644 --- a/fertiscan/db/bytebase/new_inspection/new_sub_label.sql +++ b/fertiscan/db/bytebase/new_inspection/new_sub_label.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_sub_label( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_sub_label( content_fr text DEFAULT NULL::text, content_en text DEFAULT NULL::text, label_id uuid DEFAULT NULL::uuid, diff --git a/fertiscan/db/bytebase/new_inspection_function.sql b/fertiscan/db/bytebase/new_inspection_function.sql index 9b10734e..02e76487 100644 --- a/fertiscan/db/bytebase/new_inspection_function.sql +++ b/fertiscan/db/bytebase/new_inspection_function.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_inspection(user_id uuid, picture_set_id uuid, input_json jsonb) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".new_inspection(user_id uuid, picture_set_id uuid, input_json jsonb) RETURNS jsonb LANGUAGE plpgsql AS $function$ @@ -54,7 +54,7 @@ BEGIN -- phone_number_string, -- '') <> '' -- THEN --- company_id := "fertiscan_0.0.17".new_organization_info_located( +-- company_id := "fertiscan_0.0.18".new_organization_info_located( -- input_json->'company'->>'name', -- input_json->'company'->>'address', -- input_json->'company'->>'website', @@ -79,7 +79,7 @@ BEGIN -- phone_number_string, -- '') <> '' -- THEN --- manufacturer_id := "fertiscan_0.0.17".new_organization_info_located( +-- manufacturer_id := "fertiscan_0.0.18".new_organization_info_located( -- input_json->'manufacturer'->>'name', -- input_json->'manufacturer'->>'address', -- input_json->'manufacturer'->>'website', @@ -92,7 +92,7 @@ BEGIN -- Manufacturer end -- LABEL INFORMATION - label_info_id := "fertiscan_0.0.17".new_label_information( + label_info_id := "fertiscan_0.0.18".new_label_information( input_json->'product'->>'name', input_json->'product'->>'lot_number', input_json->'product'->>'npk', @@ -122,11 +122,11 @@ BEGIN '') <> '' THEN -- Insert the new weight - weight_id = "fertiscan_0.0.17".new_metric_unit( + weight_id = "fertiscan_0.0.18".new_metric_unit( read_value::float, record->>'unit', label_info_id, - 'weight'::"fertiscan_0.0.17".metric_type, + 'weight'::"fertiscan_0.0.18".metric_type, FALSE ); END IF; @@ -143,11 +143,11 @@ BEGIN read_unit, '') <> '' THEN - density_id := "fertiscan_0.0.17".new_metric_unit( + density_id := "fertiscan_0.0.18".new_metric_unit( read_value::float, read_unit, label_info_id, - 'density'::"fertiscan_0.0.17".metric_type, + 'density'::"fertiscan_0.0.18".metric_type, FALSE ); END IF; @@ -166,11 +166,11 @@ BEGIN '') <> '' THEN -- Insert the new volume - volume_id := "fertiscan_0.0.17".new_metric_unit( + volume_id := "fertiscan_0.0.18".new_metric_unit( value_float, read_unit, label_info_id, - 'volume'::"fertiscan_0.0.17".metric_type, + 'volume'::"fertiscan_0.0.18".metric_type, FALSE ); END IF; @@ -189,11 +189,11 @@ BEGIN -- '') <> '' -- THEN -- -- Insert the new specification --- specification_id := "fertiscan_0.0.17".new_specification( +-- specification_id := "fertiscan_0.0.18".new_specification( -- (record->>'humidity')::float, -- (record->>'ph')::float, -- (record->>'solubility')::float, --- ingredient_language::"fertiscan_0.0.17".language, +-- ingredient_language::"fertiscan_0.0.18".language, -- label_info_id, -- FALSE -- ); @@ -219,12 +219,12 @@ BEGIN '') <> '' THEN -- Insert the new ingredient - ingredient_id := "fertiscan_0.0.17".new_ingredient( + ingredient_id := "fertiscan_0.0.18".new_ingredient( record->>'name', read_value::float, read_unit, label_info_id, - ingredient_language::"fertiscan_0.0.17".language, + ingredient_language::"fertiscan_0.0.18".language, NULL, --We cant tell atm NULL, --We cant tell atm FALSE --preset @@ -263,7 +263,7 @@ BEGIN en_value := en_values->>i; -- Insert sub-label without deleting existing data - sub_label_id := "fertiscan_0.0.17".new_sub_label( + sub_label_id := "fertiscan_0.0.18".new_sub_label( fr_value, en_value, label_info_id, @@ -287,12 +287,12 @@ BEGIN -- '') <> '' -- THEN -- -- Insert the new Micronutrient --- micronutrient_id := "fertiscan_0.0.17".new_micronutrient( +-- micronutrient_id := "fertiscan_0.0.18".new_micronutrient( -- record->> 'name', -- (record->> 'value')::float, -- record->> 'unit', -- label_info_id, --- micronutrient_language::"fertiscan_0.0.17".language +-- micronutrient_language::"fertiscan_0.0.18".language -- ); -- END IF; -- END LOOP; @@ -302,7 +302,7 @@ BEGIN -- GUARANTEED -- Loop through each language ('en' and 'fr') - FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.17".LANGUAGE)) + FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.18".LANGUAGE)) LOOP FOR record IN SELECT * FROM jsonb_array_elements(input_json->'guaranteed_analysis'->guaranteed_analysis_language) LOOP @@ -313,12 +313,12 @@ BEGIN '') <> '' THEN -- Insert the new guaranteed_analysis - guaranteed_analysis_id := "fertiscan_0.0.17".new_guaranteed_analysis( + guaranteed_analysis_id := "fertiscan_0.0.18".new_guaranteed_analysis( record->>'name', (record->>'value')::float, record->>'unit', label_info_id, - guaranteed_analysis_language::"fertiscan_0.0.17".language, + guaranteed_analysis_language::"fertiscan_0.0.18".language, FALSE, NULL -- We arent handeling element_id yet ); @@ -337,7 +337,7 @@ BEGIN '') <> '' THEN -- Insert the new registration number - registration_number_id := "fertiscan_0.0.17".new_registration_number( + registration_number_id := "fertiscan_0.0.18".new_registration_number( record->>'registration_number', label_info_id, (record->>'is_an_ingredient')::BOOLEAN, @@ -361,7 +361,7 @@ BEGIN '') <> '' THEN -- Insert the new organization info - organization_id := "fertiscan_0.0.17".new_organization_info_located( + organization_id := "fertiscan_0.0.18".new_organization_information( record->>'name', record->>'address', record->>'website', @@ -379,7 +379,7 @@ BEGIN -- ORGANIZATIONS INFO END -- INSPECTION - INSERT INTO "fertiscan_0.0.17".inspection ( + INSERT INTO "fertiscan_0.0.18".inspection ( inspector_id, label_info_id, sample_id, picture_set_id, inspection_comment ) VALUES ( user_id, -- Assuming inspector_id is handled separately @@ -396,7 +396,7 @@ BEGIN -- TODO: remove olap transactions from Operational transactions -- Update the Inspection_factual entry with the json - UPDATE "fertiscan_0.0.17".inspection_factual + UPDATE "fertiscan_0.0.18".inspection_factual SET original_dataset = input_json WHERE inspection_factual."inspection_id" = inspection_id_value; diff --git a/fertiscan/db/bytebase/schema_0.0.18.sql b/fertiscan/db/bytebase/schema_0.0.18.sql new file mode 100644 index 00000000..62fc58d2 --- /dev/null +++ b/fertiscan/db/bytebase/schema_0.0.18.sql @@ -0,0 +1,367 @@ +--Schema creation "fertiscan_0.0.18" +DO +$do$ +BEGIN +IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'fertiscan_0.0.18')) THEN + +drop schema "fertiscan_0.0.18" Cascade; +create schema "fertiscan_0.0.18"; + CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + + CREATE TABLE "fertiscan_0.0.18"."users" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "email" text NOT NULL UNIQUE, + "registration_date" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" timestamp + ); + + -- CREATE A TYPE FOR FRENCH/ENGLISH LANGUAGE + CREATE TYPE "fertiscan_0.0.18".LANGUAGE AS ENUM ('fr', 'en'); + + CREATE TABLE "fertiscan_0.0.18"."picture_set" ( + "id" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, + "picture_set" json NOT NULL, + "owner_id" uuid NOT NULL REFERENCES "fertiscan_0.0.18".users(id), + "upload_date" date NOT NULL DEFAULT current_timestamp, + "name" text + ); + + alter table "fertiscan_0.0.18".users ADD "default_set_id" uuid REFERENCES "fertiscan_0.0.18".picture_set(id); + + CREATE TABLE "fertiscan_0.0.18"."picture" ( + "id" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, + "picture" json NOT NULL, + "nb_obj" int, + "picture_set_id" uuid NOT NULL REFERENCES "fertiscan_0.0.18".picture_set(id), + "verified" boolean NOT NULL DEFAULT false, + "upload_date" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE "fertiscan_0.0.18"."province" ( + "id" SERIAL PRIMARY KEY, + "name" text UNIQUE NOT NULL + ); + + CREATE TABLE "fertiscan_0.0.18"."region" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "province_id" int REFERENCES "fertiscan_0.0.18".province(id), + "name" text NOT NULL + ); + + CREATE TABLE "fertiscan_0.0.18"."unit" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "unit" text NOT NULL, + "to_si_unit" float + ); + + CREATE TABLE "fertiscan_0.0.18"."element_compound" ( + "id" SERIAL PRIMARY KEY, + "number" int NOT NULL, + "name_fr" text NOT NULL, + "name_en" text NOT NULL, + "symbol" text NOT NULL UNIQUE + ); + + CREATE TABLE "fertiscan_0.0.18"."label_information" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "product_name" text, + "lot_number" text, + "npk" text, + "n" float, + "p" float, + "k" float, + "guaranteed_title_en" text, + "guaranteed_title_fr" text, + "title_is_minimal" boolean, + "record_keeping" boolean + ); + + CREATE TABLE "fertiscan_0.0.18"."label_dimension" ( + "label_id" uuid PRIMARY KEY, + "organization_info_ids" uuid[] DEFAULT '{}', + "instructions_ids" uuid[] DEFAULT '{}', + "cautions_ids" uuid[] DEFAULT '{}', + "first_aid_ids" uuid[] DEFAULT '{}', + "warranties_ids" uuid[] DEFAULT '{}', + "specification_ids" uuid[] DEFAULT '{}', + "ingredient_ids" uuid[] DEFAULT '{}', + "micronutrient_ids" uuid[] DEFAULT '{}', + "guaranteed_ids" uuid[] DEFAULT '{}', + "registration_number_ids" uuid[] DEFAULT '{}', + "weight_ids" uuid[] DEFAULT '{}', + "volume_ids" uuid[] DEFAULT '{}', + "density_ids" uuid[] DEFAULT '{}' + ); + + CREATE TABLE "fertiscan_0.0.18"."time_dimension" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "date_value" date, + "year" int, + "month" int, + "day" int, + "month_name" text, + "day_name" text + ); + + CREATE TABLE "fertiscan_0.0.18"."inspection_factual" ( + "inspection_id" uuid PRIMARY KEY, + "inspector_id" uuid , + "label_info_id" uuid , + "time_id" uuid REFERENCES "fertiscan_0.0.18".time_dimension(id), + "sample_id" uuid, + "picture_set_id" uuid, + "inspection_date" timestamp DEFAULT CURRENT_TIMESTAMP, + "original_dataset" json + ); + + CREATE TABLE "fertiscan_0.0.18"."organization_information" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" text, + "website" text, + "phone_number" text, + "address" text, + "edited" boolean DEFAULT false, + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "is_main_contact" boolean DEFAULT false NOT NULL, + CONSTRAINT check_not_all_null CHECK ( + (name IS NOT NULL)::integer + + (website IS NOT NULL)::integer + + (phone_number IS NOT NULL)::integer + + (address IS NOT NULL)::integer >= 1) + ); + + + CREATE TABLE "fertiscan_0.0.18"."location" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" text, + "address" text NOT NULL, + "address_number" text, + "city" text NOT NULL, + "postal_code" text NOT NULL, + "region_id" uuid REFERENCES "fertiscan_0.0.18".region(id) + ); + + CREATE TABLE "fertiscan_0.0.18"."organization" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" text UNIQUE NOT NULL, + "website" text, + "phone_number" text, + "address" text, + "main_location_id" uuid REFERENCES "fertiscan_0.0.18".location(id), + "upload_date" timestamp DEFAULT CURRENT_TIMESTAMP, + "updated_at" timestamp DEFAULT CURRENT_TIMESTAMP + ); + + Alter table "fertiscan_0.0.18".location ADD "organization_id" uuid REFERENCES "fertiscan_0.0.18".organization(id); + + CREATE TABLE "fertiscan_0.0.18"."sample" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "number" uuid, + "collection_date" date, + "location" uuid REFERENCES "fertiscan_0.0.18".location(id) + ); + + CREATE TYPE "fertiscan_0.0.18".metric_type as ENUM ('volume', 'weight','density'); + + CREATE TABLE "fertiscan_0.0.18"."metric" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "value" float, + "edited" boolean, + "unit_id" uuid REFERENCES "fertiscan_0.0.18".unit(id), + "metric_type" "fertiscan_0.0.18".metric_type, + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + CONSTRAINT check_not_all_null CHECK ( + (value IS NOT NULL)::integer + + (unit_id IS NOT NULL)::integer >= 1) + ); + + CREATE TABLE "fertiscan_0.0.18"."sub_type" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "type_fr" text Unique NOT NULL, + "type_en" text unique NOT NULL + ); + + CREATE TABLE "fertiscan_0.0.18"."registration_number_information" ( + "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + "identifier" text NOT NULL, + "name" text, + "is_an_ingredient" BOOLEAN, + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "edited" BOOLEAN DEFAULT FALSE + ); + + CREATE TABLE "fertiscan_0.0.18"."specification" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "humidity" float, + "ph" float, + "solubility" float, + "edited" boolean, + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "language" "fertiscan_0.0.18".LANGUAGE + ); + + CREATE TABLE "fertiscan_0.0.18"."sub_label" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "text_content_fr" text NOT NULL DEFAULT '', + "text_content_en" text NOT NULL DEFAULT '', + "label_id" uuid NOT NULL REFERENCES "fertiscan_0.0.18"."label_information" ("id") ON DELETE CASCADE, + "edited" boolean, --this is because with the current upsert we can not determine if it was edited or not + "sub_type_id" uuid NOT NULL REFERENCES "fertiscan_0.0.18"."sub_type" ("id"), + CONSTRAINT check_not_all_null CHECK ( + (COALESCE(sub_label.text_content_en, '') <> '') OR + (COALESCE(sub_label.text_content_fr, '') <> '') + ) + ); + + CREATE TABLE "fertiscan_0.0.18"."micronutrient" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "read_name" text, + "value" float, + "unit" text , + "element_id" int REFERENCES "fertiscan_0.0.18".element_compound(id), + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "edited" boolean, + "language" "fertiscan_0.0.18".LANGUAGE + ); + + CREATE TABLE "fertiscan_0.0.18"."guaranteed" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "read_name" text, + "value" float , + "unit" text , + "language" "fertiscan_0.0.18".LANGUAGE, + "element_id" int REFERENCES "fertiscan_0.0.18".element_compound(id), + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "edited" boolean, + CONSTRAINT check_not_all_null CHECK ( + (read_name IS NOT NULL)::integer + + (value IS NOT NULL)::integer + + (unit IS NOT NULL)::integer >= 1) + ); + + CREATE TABLE "fertiscan_0.0.18"."ingredient" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "organic" boolean, + "active" boolean, + "name" text, + "value" float, + "unit" text, + "edited" boolean, + "label_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "language" "fertiscan_0.0.18".LANGUAGE + ); + + CREATE TABLE "fertiscan_0.0.18"."inspection" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "verified" boolean DEFAULT false, + "upload_date" timestamp DEFAULT CURRENT_TIMESTAMP, + "updated_at" timestamp DEFAULT CURRENT_TIMESTAMP, + "inspector_id" uuid NOT NULL REFERENCES "fertiscan_0.0.18".users(id), + "label_info_id" uuid REFERENCES "fertiscan_0.0.18".label_information(id) ON DELETE CASCADE, + "sample_id" uuid REFERENCES "fertiscan_0.0.18".sample(id), + "picture_set_id" uuid REFERENCES "fertiscan_0.0.18".picture_set(id), + "inspection_comment" text, + "verified_date" timestamp default null + ); + + CREATE TABLE "fertiscan_0.0.18"."fertilizer" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" text UNIQUE NOT NULL, + "registration_number" text, + "upload_date" timestamp DEFAULT CURRENT_TIMESTAMP, + "update_at" timestamp DEFAULT CURRENT_TIMESTAMP, + "latest_inspection_id" uuid REFERENCES "fertiscan_0.0.18".inspection(id) ON DELETE SET NULL, + "main_contact" uuid REFERENCES "fertiscan_0.0.18".organization(id) ON DELETE SET NULL + );-- It should actually try to seek if there are any other organization that can be found under the latest_inspection organisation_information instead of setting it to null + + Alter table "fertiscan_0.0.18".inspection ADD "fertilizer_id" uuid REFERENCES "fertiscan_0.0.18".fertilizer(id); + + -- Trigger function for the `user` table + CREATE OR REPLACE FUNCTION update_user_timestamp() + RETURNS TRIGGER AS $$ + BEGIN + NEW.updated_at = CURRENT_TIMESTAMP; + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + -- Trigger for the `user` table + CREATE TRIGGER user_update_before + BEFORE UPDATE ON "fertiscan_0.0.18".users + FOR EACH ROW + EXECUTE FUNCTION update_user_timestamp(); + + -- Trigger function for the `analysis` table + CREATE OR REPLACE FUNCTION update_analysis_timestamp() + RETURNS TRIGGER AS $$ + BEGIN + NEW.updated_at = CURRENT_TIMESTAMP; + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + -- Trigger for the `analysis` table + CREATE TRIGGER analysis_update_before + BEFORE UPDATE ON "fertiscan_0.0.18".inspection + FOR EACH ROW + EXECUTE FUNCTION update_analysis_timestamp(); + + -- Trigger function for the `fertilizer` table + CREATE OR REPLACE FUNCTION update_fertilizer_timestamp() + RETURNS TRIGGER AS $$ + BEGIN + NEW.update_at = CURRENT_TIMESTAMP; + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + -- Trigger for the `fertilizer` table + CREATE TRIGGER fertilizer_update_before + BEFORE UPDATE ON "fertiscan_0.0.18".fertilizer + FOR EACH ROW + EXECUTE FUNCTION update_fertilizer_timestamp(); + + -- Trigger function for the `organization` table + CREATE OR REPLACE FUNCTION update_organization_timestamp() + RETURNS TRIGGER AS $$ + BEGIN + NEW.updated_at = CURRENT_TIMESTAMP; + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + -- Trigger for the `organization` table + CREATE TRIGGER organization_update_before + BEFORE UPDATE ON "fertiscan_0.0.18".organization + FOR EACH ROW + EXECUTE FUNCTION update_organization_timestamp(); + + -- Trigger function for the `inspection` table + CREATE OR REPLACE FUNCTION update_inspection_original_dataset_protection() + RETURNS TRIGGER AS $$ + BEGIN + IF (TG_OP = 'UPDATE') AND (OLD.original_dataset IS NULL) THEN + RETURN NEW; + ELSIF (TG_OP = 'UPDATE') AND (OLD.original_dataset IS NOT NULL) THEN + -- Protect the original dataset from being updated + NEW.original_dataset = OLD.original_dataset; + RETURN NEW; + END IF; + END; + $$ LANGUAGE plpgsql; + + -- Trigger for the `inspection` table + CREATE TRIGGER inspection_update_protect_original_dataset + BEFORE UPDATE ON "fertiscan_0.0.18".inspection_factual + FOR EACH ROW + EXECUTE FUNCTION update_inspection_original_dataset_protection(); + + -- Insert the default types : [instruction, caution,first_aid, warranty] + INSERT INTO "fertiscan_0.0.18".sub_type(type_fr,type_en) VALUES + ('instructions','instructions'), + ('mises_en_garde','cautions'); + -- ('premier_soin','first_aid'), -- We are not using this anymore + -- ('garanties','warranties'); -- we are not using this anymore +end if; +END +$do$; diff --git a/fertiscan/db/bytebase/update_inspection/update_registration_number.sql b/fertiscan/db/bytebase/update_inspection/update_registration_number.sql index db1c15c7..ff184794 100644 --- a/fertiscan/db/bytebase/update_inspection/update_registration_number.sql +++ b/fertiscan/db/bytebase/update_inspection/update_registration_number.sql @@ -1,7 +1,7 @@ -drop FUNCTION IF EXISTS "fertiscan_0.0.17".update_registration_number; +drop FUNCTION IF EXISTS "fertiscan_0.0.18".update_registration_number; -- Function to update guaranteed analysis: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_registration_number( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_registration_number( p_label_id uuid, new_registration_numbers jsonb ) diff --git a/fertiscan/db/bytebase/update_inspection/upsert_organization.sql b/fertiscan/db/bytebase/update_inspection/upsert_organization.sql new file mode 100644 index 00000000..49497ab9 --- /dev/null +++ b/fertiscan/db/bytebase/update_inspection/upsert_organization.sql @@ -0,0 +1,36 @@ + +DROP IF EXISTS FUNCTION "fertiscan_0.0.18".upsert_organization(); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_organization(org_info_id uuid) +RETURNS id AS $$ +DECLARE + record record; + address_str TEXT; + org_id uuid; +BEGIN +-- Select the row from the organization_information table +SELECT * INTO record FROM organization_information WHERE id = org_info_id; + +-- Check if the organization exists +Select id INTO org_id FROM organization WHERE name ILIKE record.name; + +-- UPSERT DATA INTO THE ORGANIZATION TABLE +IF org_id IS NULL THEN + INSERT INTO organization ("website","phone_number","address","main_location_id") + VALUES ( + record.website, + record.phone_number, + record.address, + Null -- Not re-implemented yet + ) + RETURNING id INTO org_id; +ELSE + UPDATE organization SET + "website" = record.website, + "phone_number" = record.phone_number, + "address" = record.address, + "main_location_id" = Null -- Not re-implemented yet + WHERE id = org_id + RETURNING id INTO org_id; +END IF; +RETURN org_id; +END; diff --git a/fertiscan/db/bytebase/update_inspection/update_organization_information.sql b/fertiscan/db/bytebase/update_inspection/upsert_organization_info.sql similarity index 58% rename from fertiscan/db/bytebase/update_inspection/update_organization_information.sql rename to fertiscan/db/bytebase/update_inspection/upsert_organization_info.sql index 8a092c6d..a7d94e91 100644 --- a/fertiscan/db/bytebase/update_inspection/update_organization_information.sql +++ b/fertiscan/db/bytebase/update_inspection/upsert_organization_info.sql @@ -1,7 +1,7 @@ -- Function to upsert organization information -DROP FUNCTION IF EXISTS "fertiscan_0.0.17".upsert_organization_info(jsonb, uuid); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_organization_info(input_org_info jsonb, label_info_id uuid) +DROP FUNCTION IF EXISTS "fertiscan_0.0.18".upsert_organization_info(jsonb, uuid); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_organization_info(input_org_info jsonb, label_info_id uuid) RETURNS void AS $$ DECLARE record jsonb; @@ -23,27 +23,12 @@ BEGIN (record->>'is_main_contact')::boolean ); else - -- UPDATE THE LOCATION - address_str := input_org_info->>'address'; - - -- CHECK IF ADRESS IS NULL - IF address_str IS NULL or COALESCE(address_str,'')='' THEN - RAISE WARNING 'Address should not be null'; - ELSE - -- Check if organization location exists by address - SELECT id INTO location_id - FROM location - WHERE location.address ILIKE address_str - LIMIT 1; - -- Use upsert_location to insert or update the location - location_id := upsert_location(location_id, address_str); - END IF; -- UPDATE THE ORGANIZATION INFORMATION UPDATE organization_information SET "name" = record->>'name', "website" = record->>'website', "phone_number" = record->>'phone_number', - "location_id" = location_id, + "address" = address_str, "edited" = (record->>'edited')::boolean, "is_main_contact" = (record->>'is_main_contact')::boolean WHERE "id" = record->>'id'; diff --git a/fertiscan/db/bytebase/update_inspection_function.sql b/fertiscan/db/bytebase/update_inspection_function.sql index cd21ac95..1852dd6d 100644 --- a/fertiscan/db/bytebase/update_inspection_function.sql +++ b/fertiscan/db/bytebase/update_inspection_function.sql @@ -1,7 +1,7 @@ -SET search_path TO "fertiscan_0.0.17"; +SET search_path TO "fertiscan_0.0.18"; -- Function to upsert location information based on location_id and address -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_location(location_id uuid, address text) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_location(location_id uuid, address text) RETURNS uuid AS $$ DECLARE new_location_id uuid; @@ -23,7 +23,7 @@ $$ LANGUAGE plpgsql; -- Function to update metrics: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_metrics( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_metrics( p_label_id uuid, metrics jsonb ) @@ -98,7 +98,7 @@ $$ LANGUAGE plpgsql; -- Function to update specifications: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_specifications( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_specifications( p_label_id uuid, new_specifications jsonb ) @@ -144,7 +144,7 @@ $$ LANGUAGE plpgsql; -- Function to update ingredients: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_ingredients( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_ingredients( p_label_id uuid, new_ingredients jsonb ) @@ -192,7 +192,7 @@ $$ LANGUAGE plpgsql; -- Function to update micronutrients: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_micronutrients( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_micronutrients( p_label_id uuid, new_micronutrients jsonb ) @@ -236,9 +236,9 @@ BEGIN END; $$ LANGUAGE plpgsql; -drop FUNCTION IF EXISTS "fertiscan_0.0.17".update_guaranteed; +drop FUNCTION IF EXISTS "fertiscan_0.0.18".update_guaranteed; -- Function to update guaranteed analysis: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_guaranteed( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_guaranteed( p_label_id uuid, new_guaranteed jsonb ) @@ -252,7 +252,7 @@ BEGIN DELETE FROM guaranteed WHERE label_id = p_label_id; -- Loop through each language ('en' and 'fr') - FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.17".LANGUAGE)) + FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.18".LANGUAGE)) LOOP FOR guaranteed_record IN SELECT * FROM jsonb_array_elements(new_guaranteed->guaranteed_analysis_language) LOOP @@ -285,7 +285,7 @@ $$ LANGUAGE plpgsql; -- Function to check if both text_content_fr and text_content_en are NULL or empty, and skip insertion if true -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".check_null_or_empty_sub_label() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".check_null_or_empty_sub_label() RETURNS TRIGGER AS $$ BEGIN -- Check if both text_content_fr and text_content_en are NULL or empty @@ -312,15 +312,15 @@ $$ LANGUAGE plpgsql; -- Trigger to call check_null_or_empty_sub_label() before inserting into sub_label -DROP TRIGGER IF EXISTS before_insert_sub_label ON "fertiscan_0.0.17".sub_label; +DROP TRIGGER IF EXISTS before_insert_sub_label ON "fertiscan_0.0.18".sub_label; CREATE TRIGGER before_insert_sub_label -BEFORE INSERT ON "fertiscan_0.0.17".sub_label +BEFORE INSERT ON "fertiscan_0.0.18".sub_label FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.17".check_null_or_empty_sub_label(); +EXECUTE FUNCTION "fertiscan_0.0.18".check_null_or_empty_sub_label(); -- Function to update sub labels: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_sub_labels( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_sub_labels( p_label_id uuid, new_sub_labels jsonb ) @@ -379,9 +379,9 @@ BEGIN END; $$ LANGUAGE plpgsql; -Drop FUNCTION IF EXISTS "fertiscan_0.0.17".upsert_inspection; +Drop FUNCTION IF EXISTS "fertiscan_0.0.18".upsert_inspection; -- Function to upsert inspection information -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_inspection( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_inspection( p_inspection_id uuid, p_label_info_id uuid, p_inspector_id uuid, @@ -431,7 +431,7 @@ $$ LANGUAGE plpgsql; -- Function to upsert fertilizer information based on unique fertilizer name -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_fertilizer( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_fertilizer( p_name text, p_registration_number text, p_owner_id uuid, @@ -465,9 +465,9 @@ BEGIN END; $$ LANGUAGE plpgsql; -drop FUNCTION IF EXISTS "fertiscan_0.0.17".update_inspection; +drop FUNCTION IF EXISTS "fertiscan_0.0.18".update_inspection; -- Function to update inspection data and related entities, returning an updated JSON -CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_inspection( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_inspection( p_inspection_id uuid, p_inspector_id uuid, p_input_json jsonb @@ -592,7 +592,7 @@ BEGIN SELEct id,location_id into org_info_id,location_id_value from organization_information where label_id = label_info_id_value; else org_info_id := NULL; - for org_record in select * from "fertiscan_0.0.17".organization_information where label_id = label_info_id_value + for org_record in select * from "fertiscan_0.0.18".organization_information where label_id = label_info_id_value loop if org_record.is_main_contact then org_info_id := org_record.id; @@ -601,13 +601,14 @@ BEGIN end loop; end if; if org_info_id is null then - RAISE EXCEPTION 'Main contact organization information is required and was not found'; + RAISE Warning 'Main contact organization information is required and was not found'; + else + -- This will either update the existing organization (based on matching name) or insert a new one + organization_id := upsert_organization( + org_info_id + ); END IF; - INSERT INTO organization (information_id, main_location_id) - VALUES (org_info_id, location_id_value) - RETURNING id INTO organization_id; - -- Upsert the fertilizer record PERFORM upsert_fertilizer( fertilizer_name, diff --git a/fertiscan/db/queries/organization/__init__.py b/fertiscan/db/queries/organization/__init__.py index 3b15bc89..271017bf 100644 --- a/fertiscan/db/queries/organization/__init__.py +++ b/fertiscan/db/queries/organization/__init__.py @@ -28,7 +28,7 @@ @handle_query_errors(OrganizationInformationCreationError) -def new_organization(cursor: Cursor, information_id, location_id=None): +def new_organization(cursor: Cursor, name, website, phone_number, address): """ This function create a new organization in the database using a query. @@ -37,41 +37,33 @@ def new_organization(cursor: Cursor, information_id, location_id=None): - name (str): The name of the organization. - website (str): The website of the organization. - phone_number (str): The phone number of the organization. + - address (str): The address of the organization. Returns: - str: The UUID of the organization """ - if location_id is None: - query = """ - SELECT - location_id - FROM - organization_information - WHERE - id = %s - """ - cursor.execute(query, (information_id,)) - location_id = cursor.fetchone()[0] query = """ INSERT INTO organization ( - information_id, - main_location_id + name, + website, + phone_number, + address ) VALUES (%s, %s) RETURNING id """ - cursor.execute(query, (information_id, location_id)) + cursor.execute(query, (name, website, phone_number, address)) if result := cursor.fetchone(): return result[0] raise OrganizationCreationError("Failed to create Organization. No data returned.") @handle_query_errors(OrganizationInformationCreationError) -def new_organization_info_located( - cursor: Cursor, address: str, name: str, website: str, phone_number: str, label_id: UUID +def new_organization_information( + cursor: Cursor, address: str, name: str, website: str, phone_number: str, label_id: UUID, edited: bool = False, is_main_contact: bool = False ): """ This function create a new organization information in the database using function. @@ -81,10 +73,15 @@ def new_organization_info_located( - name (str): The name of the organization. - website (str): The website of the organization. - phone_number (str): The phone number of the organization. + - label_id (str): The UUID of the label. + - edited (bool): The edited status of the organization information. + - is_main_contact (bool): The main contact status of the organization information. Returns: - str: The UUID of the organization information """ + if label_id is None: + raise OrganizationInformationCreationError("Label ID is required for organization information creation.") query = """ SELECT new_organization_info_located(%s, %s, %s, %s); """ @@ -95,53 +92,15 @@ def new_organization_info_located( address, website, phone_number, + edited, label_id, + is_main_contact, ), ) if result := cursor.fetchone(): return result[0] raise OrganizationCreationError("Failed to create Organization. No data returned.") - -@handle_query_errors(OrganizationInformationCreationError) -def new_organization_info( - cursor: Cursor, name, website, phone_number, location_id=None, label_id=None, is_main_contact=False -): - """ - This function create a new organization information in the database. - - Parameters: - - cursor (cursor): The cursor of the database. - - name (str): The name of the organization. - - website (str): The website of the organization. - - phone_number (str): The phone number of the organization. - - Returns: - - str: The UUID of the organization information - """ - query = """ - INSERT INTO - organization_information ( - name, - website, - phone_number, - location_id, - label_id - ) - VALUES - (%s, %s, %s, %s, %s, %s) - RETURNING - id - """ - cursor.execute( - query, - (name, website, phone_number, location_id, label_id, is_main_contact), - ) - if result := cursor.fetchone(): - return result[0] - raise OrganizationCreationError("Failed to create Organization. No data returned.") - - @handle_query_errors(OrganizationInformationRetrievalError) def get_organization_info(cursor: Cursor, information_id): """ @@ -208,7 +167,7 @@ def get_organizations_info_label(cursor: Cursor, label_id: UUID): @handle_query_errors(OrganizationInformationRetrievalError) -def get_organizations_info_json(cursor: Cursor, label_id) -> dict: +def get_organizations_info_json(cursor: Cursor, label_id: UUID) -> dict: """ This function get a organization information from the database. @@ -235,6 +194,34 @@ def get_organizations_info_json(cursor: Cursor, label_id) -> dict: return res[0][0] else: return {} + +def get_organization_json(cursor: Cursor, fertilizer_id: UUID) -> dict: + """ + This function get a organization information from the database. + + Parameters: + - cursor (cursor): The cursor of the database. + - fertilizer_id (str): The UUID of a Fertilizer owned by the orgganization. + + Returns: + - dict: The organization information + """ + query = """ + SELECT get_organization_json(%s); + """ + cursor.execute(query, (str(fertilizer_id),)) + + res = cursor.fetchone() + if res is None or res[0] is None: + # raise OrganizationNotFoundError + # There might not be any organization information + return {} + if len(res[0]) == 2: + return {**res[0][0], **res[0][1]} + elif len(res[0]) == 1: + return res[0][0] + else: + return {} @handle_query_errors(OrganizationUpdateError) @@ -328,6 +315,23 @@ def upsert_organization_info(cursor: Cursor, organization_info, label_id: UUID): cursor.execute(query, (organization_info, label_id,)) return cursor.fetchone()[0] +def upsert_organization(cursor: Cursor, organization_info_id: UUID): + """ + This function upserts an organization information in the database. + + Parameters: + - cursor (cursor): The cursor of the database. + - organization_info (JSON: string): The organization information in a json. + + Returns: + - str: The UUID of the organization information + """ + query = """ + SELECT upsert_organization(%s, %s); + """ + cursor.execute(query, ( organization_info_id,)) + return cursor.fetchone()[0] + @handle_query_errors(OrganizationRetrievalError) def get_organization(cursor: Cursor, organization_id): @@ -339,12 +343,14 @@ def get_organization(cursor: Cursor, organization_id): - organization_id (str): The UUID of the organization. Returns: - - dict: The organization + - tuple: The organization """ query = """ SELECT - information_id, - main_location_id + name, + website, + phone_number, + address, FROM organization WHERE @@ -374,22 +380,22 @@ def get_full_organization(cursor: Cursor, org_id): query = """ SELECT organization.id, - information.name, - information.website, - information.phone_number, + organization.name, + organization.website, + organization.phone_number, + organization.address, location.id, location.name, location.address, + location.address_number, + location.city, + location.postal_code, region.id, region.name, province.id, province.name FROM organization - LEFT JOIN - organization_information as information - ON - organization.information_id = information.id LEFT JOIN location ON diff --git a/tests/fertiscan/db/test_organization.py b/tests/fertiscan/db/test_organization.py index ef6fa5eb..d9d41450 100644 --- a/tests/fertiscan/db/test_organization.py +++ b/tests/fertiscan/db/test_organization.py @@ -163,24 +163,12 @@ def setUp(self): self.con = db.connect_db(DB_CONNECTION_STRING, DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor, DB_SCHEMA) - self.province_name = "a-test-province" - self.region_name = "test-region" + self.name = "test-organization" self.website = "www.test.com" self.phone = "123456789" - self.location_name = "test-location" - self.location_address = "test-address" - self.province_id = organization.new_province(self.cursor, self.province_name) - - self.region_id = organization.new_region( - self.cursor, self.region_name, self.province_id - ) - - self.location_id = organization.new_location( - self.cursor, self.location_name, self.location_address, self.region_id - ) + self.address = "test-address" - self.lot_number = "lot_number" self.product_name = "product_name" self.npk = "npk" @@ -215,20 +203,20 @@ def tearDown(self): self.con.rollback() db.end_query(self.con, self.cursor) - def test_new_organization_info(self): - id = organization.new_organization_info( - self.cursor, self.name, self.website, self.phone, self.location_id, self.label_information_id, True + def test_new_organization_information(self): + id = organization.new_organization_information( + self.cursor, self.address, self.name, self.website, self.phone, self.label_information_id, False, True ) self.assertTrue(validator.is_valid_uuid(id)) def test_new_organization_located(self): - id = organization.new_organization_info( - self.cursor, self.name, self.website, self.phone, self.location_id, self.label_information_id, True + id = organization.new_organization_information( + self.cursor, self.address, self.name, self.website, self.phone, self.label_information_id, False, True ) self.assertTrue(validator.is_valid_uuid(id)) def test_new_organization_info_no_location(self): - id = organization.new_organization_info( + id = organization.new_organization_information( self.cursor, self.name, self.website, self.phone, None, self.label_information_id, True ) self.assertTrue(validator.is_valid_uuid(id)) @@ -250,7 +238,7 @@ def test_new_organization_located_no_address(self): self.assertIsNone(location_id) def test_get_organization_info(self): - id = organization.new_organization_info( + id = organization.new_organization_information( self.cursor, self.name, self.website, self.phone, self.location_id ) data = organization.get_organization_info(self.cursor, id) @@ -264,8 +252,8 @@ def test_get_organization_info_not_found(self): organization.get_organization_info(self.cursor, str(uuid.uuid4())) def test_get_organization_info_label(self): - organization.new_organization_info( - self.cursor, self.name, self.website, self.phone, self.location_id, label_id=self.label_information_id + organization.new_organization_information( + self.cursor, self.name, self.website, self.phone, label_id=self.label_information_id ) data = organization.get_organizations_info_label(self.cursor, self.label_id) self.assertEqual(data[0], self.name) @@ -277,8 +265,8 @@ def test_update_organization_info(self): new_name = "new-name" new_website = "www.new.com" new_phone = "987654321" - id = organization.new_organization_info( - self.cursor, self.name, self.website, self.phone, self.location_id, self.label_information_id + id = organization.new_organization_information( + self.cursor, self.name, self.website, self.phone, self.label_information_id ) old_data = organization.get_organization_info(self.cursor, id) self.assertEqual(old_data[0], self.name) @@ -369,8 +357,8 @@ def test_upsert_organizations_info(self): def test_delete_label_with_linked_manufacturer(self): # create a organization information - organization_id = organization.new_organization_info( - self.cursor, self.name, self.website, self.phone, self.location_id, self.label_information_id + organization_id = organization.new_organization_information( + self.cursor, self.name, self.website, self.phone, self.label_information_id ) label_info = label.get_label_info_json(self.cursor, self.label_information_id) # Attempt to delete the inspection, which should raise a notice but not fail @@ -440,7 +428,7 @@ def setUp(self): self.location_id = organization.new_location( self.cursor, self.location_name, self.location_address, self.region_id ) - self.org_info_id = organization.new_organization_info( + self.org_info_id = organization.new_organization_information( self.cursor, self.name, self.website, self.phone, self.location_id )