From 7459b1e948ae50f22bc7f570925fba2ce6a5e9cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 6 Nov 2024 19:04:31 +0000 Subject: [PATCH 01/15] Issue #154: new schema --- fertiscan/db/bytebase/schema_0.0.17.sql | 345 ++++++++++++++++++++++++ fertiscan/doc/fertiScan-architecture.md | 8 +- 2 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 fertiscan/db/bytebase/schema_0.0.17.sql diff --git a/fertiscan/db/bytebase/schema_0.0.17.sql b/fertiscan/db/bytebase/schema_0.0.17.sql new file mode 100644 index 00000000..b018d0bd --- /dev/null +++ b/fertiscan/db/bytebase/schema_0.0.17.sql @@ -0,0 +1,345 @@ +--Schema creation "fertiscan_0.0.17" +DO +$do$ +BEGIN +IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'fertiscan_0.0.17')) THEN + + + CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + + CREATE TABLE "fertiscan_0.0.17"."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.17".LANGUAGE AS ENUM ('fr', 'en'); + + CREATE TABLE "fertiscan_0.0.17"."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.17".users(id), + "upload_date" date NOT NULL DEFAULT current_timestamp, + "name" text + ); + + alter table "fertiscan_0.0.17".users ADD "default_set_id" uuid REFERENCES "fertiscan_0.0.17".picture_set(id); + + CREATE TABLE "fertiscan_0.0.17"."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.17".picture_set(id), + "verified" boolean NOT NULL DEFAULT false, + "upload_date" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE "fertiscan_0.0.17"."province" ( + "id" SERIAL PRIMARY KEY, + "name" text UNIQUE NOT NULL + ); + + CREATE TABLE "fertiscan_0.0.17"."region" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "province_id" int REFERENCES "fertiscan_0.0.17".province(id), + "name" text NOT NULL + ); + + CREATE TABLE "fertiscan_0.0.17"."location" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" text, + "address" text NOT NULL, + "region_id" uuid REFERENCES "fertiscan_0.0.17".region(id) + ); + + CREATE TABLE "fertiscan_0.0.17"."organization_information" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" text, + "website" text, + "phone_number" text, + "location_id" uuid REFERENCES "fertiscan_0.0.17".location(id), + "edited" boolean DEFAULT false, + CONSTRAINT check_not_all_null CHECK ( + (name IS NOT NULL)::integer + + (website IS NOT NULL)::integer + + (phone_number IS NOT NULL)::integer + + (location_id is not null)::integer >= 1) + ); + + CREATE TABLE "fertiscan_0.0.17"."organization" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "information_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id), + "main_location_id" uuid REFERENCES "fertiscan_0.0.17".location(id) + ); + + + Alter table "fertiscan_0.0.17".location ADD "owner_id" uuid REFERENCES "fertiscan_0.0.17".organization(id); + + CREATE TABLE "fertiscan_0.0.17"."sample" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "number" uuid, + "collection_date" date, + "location" uuid REFERENCES "fertiscan_0.0.17".location(id) + ); + + CREATE TABLE "fertiscan_0.0.17"."unit" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "unit" text NOT NULL, + "to_si_unit" float + ); + + CREATE TABLE "fertiscan_0.0.17"."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.17"."label_information" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "product_name" tefertiscan/db/bytebase/schema_0.0.17.sqlxt, + "lot_number" text, + "npk" text, + "n" float, + "p" float, + "k" float, + "guaranteed_title_en" text, + "guaranteed_title_fr" text, + "title_is_minimal" boolean, + "company_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id), + "manufacturer_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id) + ); + + CREATE TABLE "fertiscan_0.0.17"."label_dimension" ( + "label_id" uuid PRIMARY KEY, + "company_info_id" uuid , + "company_location_id" uuid , + "manufacturer_info_id" uuid, + "manufacturer_location_id" uuid , + "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.17"."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.17"."inspection_factual" ( + "inspection_id" uuid PRIMARY KEY, + "inspector_id" uuid , + "label_info_id" uuid , + "time_id" uuid REFERENCES "fertiscan_0.0.17".time_dimension(id), + "sample_id" uuid, + "company_id" uuid, + "manufacturer_id" uuid, + "picture_set_id" uuid, + "inspection_date" timestamp DEFAULT CURRENT_TIMESTAMP, + "original_dataset" json + ); + + + CREATE TYPE "fertiscan_0.0.17".metric_type as ENUM ('volume', 'weight','density'); + + CREATE TABLE "fertiscan_0.0.17"."metric" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "value" float, + "edited" boolean, + "unit_id" uuid REFERENCES "fertiscan_0.0.17".unit(id), + "metric_type" "fertiscan_0.0.17".metric_type, + "label_id" uuid REFERENCES "fertiscan_0.0.17".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.17"."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.17"."registration_number_information" ( + "id" UUID PRIMARY KEY uuid_generate_v4(), + "identifier" text NOT NULL, + "name" text, + "is_an_ingredient" BOOLEAN + ); + + CREATE TABLE "fertiscan_0.0.17"."specification" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "humidity" float, + "ph" float, + "solubility" float, + "edited" boolean, + "label_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id) ON DELETE CASCADE, + "language" "fertiscan_0.0.17".LANGUAGE + ); + + CREATE TABLE "fertiscan_0.0.17"."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.17"."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.17"."sub_type" ("id"), + CONSTRAINT check_not_all_null CHECK ( + (COALESCE(sub_label.text_content_en, '') <> '') AND + (COALESCE(sub_label.text_content_fr, '') <> '') + ) + ); + + CREATE TABLE "fertiscan_0.0.17"."micronutrient" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "read_name" text, + "value" float, + "unit" text , + "element_id" int REFERENCES "fertiscan_0.0.17".element_compound(id), + "label_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id) ON DELETE CASCADE, + "edited" boolean, + "language" "fertiscan_0.0.17".LANGUAGE + ); + + CREATE TABLE "fertiscan_0.0.17"."guaranteed" ( + "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + "read_name" text, + "value" float , + "unit" text , + "language" "fertiscan_0.0.17".LANGUAGE, + "element_id" int REFERENCES "fertiscan_0.0.17".element_compound(id), + "label_id" uuid REFERENCES "fertiscan_0.0.17".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.17"."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.17".label_information(id) ON DELETE CASCADE, + "language" "fertiscan_0.0.17".LANGUAGE + ); + + CREATE TABLE "fertiscan_0.0.17"."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.17".users(id), + "label_info_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id) ON DELETE CASCADE, + "sample_id" uuid REFERENCES "fertiscan_0.0.17".sample(id), + "picture_set_id" uuid REFERENCES "fertiscan_0.0.17".picture_set(id), + "inspection_comment" text + ); + + CREATE TABLE "fertiscan_0.0.17"."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.17".inspection(id) ON DELETE CASCADE, + "owner_id" uuid REFERENCES "fertiscan_0.0.17".organization(id) + ); + + Alter table "fertiscan_0.0.17".inspection ADD "fertilizer_id" uuid REFERENCES "fertiscan_0.0.17".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.17".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.17".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.17".fertilizer + FOR EACH ROW + EXECUTE FUNCTION update_fertilizer_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.17".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.17".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/doc/fertiScan-architecture.md b/fertiscan/doc/fertiScan-architecture.md index e1ae747c..acb52bdc 100644 --- a/fertiscan/doc/fertiScan-architecture.md +++ b/fertiscan/doc/fertiScan-architecture.md @@ -115,13 +115,18 @@ erDiagram uuid id PK string lot_number string npk - string registration_number float n float p float k uuid company_info_id FK uuid manufacturer_info_id FK } + registration_number_information{ + uuid id PK + string identifier + string name + boolean is_an_ingredient + } sub_label { uuid id PK text text_content_fr @@ -205,6 +210,7 @@ erDiagram label_information ||--|{ micronutrient: has label_information ||--|{ specification: has label_information ||--|{ sub_label: has + label_information ||--o{ registration_number_information: has label_information ||--o| organization_information: company label_information ||--o| organization_information: manufacturer organization_information ||--|| organization: defines From 63988a6b93eda260c79ca117f470a17ceab3d76c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 6 Nov 2024 21:16:59 +0000 Subject: [PATCH 02/15] Issue #154: add registration_number entity in DB architecture --- .../OLAP/registration_number_triggers.sql | 55 ++++++++++++++++ .../bytebase/delete_inspection_function.sql | 42 ++++++------ .../get_registration_numbers_json.sql | 25 ++++++++ .../new_registration_number.sql | 33 ++++++++++ .../db/bytebase/new_inspection_function.sql | 64 +++++++++++++------ fertiscan/db/bytebase/schema_0.0.17.sql | 8 ++- .../update_registration_number.sql | 31 +++++++++ .../bytebase/update_inspection_function.sql | 41 ++++++------ 8 files changed, 235 insertions(+), 64 deletions(-) create mode 100644 fertiscan/db/bytebase/OLAP/registration_number_triggers.sql create mode 100644 fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql create mode 100644 fertiscan/db/bytebase/new_inspection/new_registration_number.sql create mode 100644 fertiscan/db/bytebase/update_inspection/update_registration_number.sql diff --git a/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql b/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql new file mode 100644 index 00000000..6613ecca --- /dev/null +++ b/fertiscan/db/bytebase/OLAP/registration_number_triggers.sql @@ -0,0 +1,55 @@ +-- Trigger function +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_registration_number_creation() +RETURNS TRIGGER AS $$ +BEGIN + -- Check if the operation is an INSERT + IF (TG_OP = 'INSERT') THEN + -- 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" + SET registration_number_ids = array_append(registration_number_ids, NEW.id) + WHERE label_dimension.label_id = NEW.label_id; + ELSE + -- Raise a warning if the condition is not met + RAISE WARNING 'The OLAP dimension of this registration_number is not updated because the condition is not met'; + END IF; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +-- Trigger +DROP TRIGGER IF EXISTS registration_number_creation ON "fertiscan_0.0.17".registration_number_information; +CREATE TRIGGER registration_number_creation +AFTER INSERT ON "fertiscan_0.0.17".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() +RETURNS TRIGGER AS $$ +BEGIN + -- Check if the operation is an INSERT + IF (TG_OP = 'DELETE') THEN + -- 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" + SET registration_number_ids = array_remove(registration_number_ids, OLD.id) + WHERE label_dimension.label_id = OLD.label_id; + ELSE + -- Raise a warning if the condition is not met + RAISE WARNING 'The OLAP dimension of this registration_number is not updated because the condition is not met'; + END IF; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +-- Trigger +DROP TRIGGER IF EXISTS registration_number_deletion ON "fertiscan_0.0.17".registration_number_information; +CREATE TRIGGER registration_number_deletion +AFTER INSERT ON "fertiscan_0.0.17".registration_number_information +FOR EACH ROW +EXECUTE FUNCTION olap_registration_number_deletion(); diff --git a/fertiscan/db/bytebase/delete_inspection_function.sql b/fertiscan/db/bytebase/delete_inspection_function.sql index c1f9c651..3afaf6c3 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.16"; +SET search_path TO "fertiscan_0.0.17"; -- Trigger function to handle after organization_information delete for location deletion -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".after_org_info_delete_location_trig() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".location + DELETE FROM "fertiscan_0.0.17".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.16".organization_information; +DROP TRIGGER IF EXISTS after_organization_information_delete_location ON "fertiscan_0.0.17".organization_information; CREATE TRIGGER after_organization_information_delete_location -AFTER DELETE ON "fertiscan_0.0.16".organization_information +AFTER DELETE ON "fertiscan_0.0.17".organization_information FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.16".after_org_info_delete_location_trig(); +EXECUTE FUNCTION "fertiscan_0.0.17".after_org_info_delete_location_trig(); -- Function to delete an inspection and related data -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".delete_inspection( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".inspection + FROM "fertiscan_0.0.17".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.16".inspection + DELETE FROM "fertiscan_0.0.17".inspection WHERE id = p_inspection_id RETURNING * ) @@ -63,18 +63,18 @@ END; $$; -- Combined trigger function to handle after inspection delete for sample and label_information deletion -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".after_insp_delete_cleanup_trig() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".after_insp_delete_cleanup_trig() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN IF OLD.sample_id IS NOT NULL THEN - DELETE FROM "fertiscan_0.0.16".sample + DELETE FROM "fertiscan_0.0.17".sample WHERE id = OLD.sample_id; END IF; IF OLD.label_info_id IS NOT NULL THEN - DELETE FROM "fertiscan_0.0.16".label_information + DELETE FROM "fertiscan_0.0.17".label_information WHERE id = OLD.label_info_id; END IF; @@ -83,27 +83,27 @@ 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.16".inspection; +DROP TRIGGER IF EXISTS after_inspection_delete_cleanup ON "fertiscan_0.0.17".inspection; CREATE TRIGGER after_inspection_delete_cleanup -AFTER DELETE ON "fertiscan_0.0.16".inspection +AFTER DELETE ON "fertiscan_0.0.17".inspection FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.16".after_insp_delete_cleanup_trig(); +EXECUTE FUNCTION "fertiscan_0.0.17".after_insp_delete_cleanup_trig(); -- Trigger function to handle after label_information delete for organization_information deletion -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".after_label_info_delete_org_info_trig() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".after_label_info_delete_org_info_trig() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN BEGIN - DELETE FROM "fertiscan_0.0.16".organization_information + DELETE FROM "fertiscan_0.0.17".organization_information WHERE id = OLD.company_info_id; EXCEPTION WHEN foreign_key_violation THEN RAISE NOTICE 'Company organization_information with ID % could not be deleted due to foreign key constraints.', OLD.company_info_id; END; BEGIN - DELETE FROM "fertiscan_0.0.16".organization_information + DELETE FROM "fertiscan_0.0.17".organization_information WHERE id = OLD.manufacturer_info_id; EXCEPTION WHEN foreign_key_violation THEN RAISE NOTICE 'Manufacturer organization_information with ID % could not be deleted due to foreign key constraints.', OLD.manufacturer_info_id; @@ -114,8 +114,8 @@ END; $$; -- Trigger definition on label_information table for organization_information deletion -DROP TRIGGER IF EXISTS after_label_information_delete_organization_information ON "fertiscan_0.0.16".label_information; +DROP TRIGGER IF EXISTS after_label_information_delete_organization_information ON "fertiscan_0.0.17".label_information; CREATE TRIGGER after_label_information_delete_organization_information -AFTER DELETE ON "fertiscan_0.0.16".label_information +AFTER DELETE ON "fertiscan_0.0.17".label_information FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.16".after_label_info_delete_org_info_trig(); +EXECUTE FUNCTION "fertiscan_0.0.17".after_label_info_delete_org_info_trig(); diff --git a/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql b/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql new file mode 100644 index 00000000..3e3da80c --- /dev/null +++ b/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql @@ -0,0 +1,25 @@ + +CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".get_registration_numbers_json( +label_info_id uuid) +RETURNS jsonb +LANGUAGE plpgsql +AS $function$ +DECLARE + result_json jsonb; +BEGIN + SELECT jsonb_build_object( + 'registration_numbers', + COALESCE(jsonb_agg( + jsonb_build_object( + 'identifier', COALESCE(registration_number_information.identifier,Null), + 'is_an_ingredient', COALESCE(registration_number_information.is_an_ingredient,Null), + 'edited', COALESCE(registration_number_information.edited,Null) + ) + ), '[]'::jsonb) + ) + INTO result_json + FROM registration_number_information + WHERE registration_number_information.label_id = label_info_id; + RETURN result_json; +END; +$function$; diff --git a/fertiscan/db/bytebase/new_inspection/new_registration_number.sql b/fertiscan/db/bytebase/new_inspection/new_registration_number.sql new file mode 100644 index 00000000..9bcfb551 --- /dev/null +++ b/fertiscan/db/bytebase/new_inspection/new_registration_number.sql @@ -0,0 +1,33 @@ + +DROP FUNCTION IF EXISTS "fertiscan_0.0.17".new_registration_number(TEXT, BOOLEAN, UUID, TEXT, BOOLEAN); +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_registration_number( + identifier TEXT, + label_info_id UUID, + is_an_ingredient BOOLEAN DEFAULT NULL, + read_name TEXT DEFAULT NULL, + edited BOOLEAN DEFAULT FALSE +) +RETURNS uuid +LANGUAGE plpgsql +AS $function$ +DECLARE + reg_num_id uuid; +BEGIN + INSERT INTO "fertiscan_0.0.17".registration_number_information ( + identifier, + is_an_ingredient, + name, + label_id, + edited + ) + VALUES ( + reg_num, + is_an_ingredient_val, + read_name, + label_info_id, + FALSE + ) + RETURNING id INTO reg_num_id; + RETURN reg_num_id; +END; +$function$; diff --git a/fertiscan/db/bytebase/new_inspection_function.sql b/fertiscan/db/bytebase/new_inspection_function.sql index 3ee32403..b58e79ec 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.16".new_inspection(user_id uuid, picture_set_id uuid, input_json jsonb) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_inspection(user_id uuid, picture_set_id uuid, input_json jsonb) RETURNS jsonb LANGUAGE plpgsql AS $function$ @@ -20,6 +20,7 @@ DECLARE manufacturer_id uuid; ingredient_id uuid; guaranteed_analysis_id uuid; + registration_number_id uuid; time_id uuid; unit_id uuid; specification_id uuid; @@ -54,7 +55,7 @@ BEGIN phone_number_string, '') <> '' THEN - company_id := "fertiscan_0.0.16".new_organization_info_located( + company_id := "fertiscan_0.0.17".new_organization_info_located( input_json->'company'->>'name', input_json->'company'->>'address', input_json->'company'->>'website', @@ -79,7 +80,7 @@ BEGIN phone_number_string, '') <> '' THEN - manufacturer_id := "fertiscan_0.0.16".new_organization_info_located( + manufacturer_id := "fertiscan_0.0.17".new_organization_info_located( input_json->'manufacturer'->>'name', input_json->'manufacturer'->>'address', input_json->'manufacturer'->>'website', @@ -92,11 +93,10 @@ BEGIN -- Manufacturer end -- LABEL INFORMATION - label_info_id := "fertiscan_0.0.16".new_label_information( + label_info_id := "fertiscan_0.0.17".new_label_information( input_json->'product'->>'name', input_json->'product'->>'lot_number', input_json->'product'->>'npk', - input_json->'product'->>'registration_number', (input_json->'product'->>'n')::float, (input_json->'product'->>'p')::float, (input_json->'product'->>'k')::float, @@ -125,11 +125,11 @@ BEGIN '') <> '' THEN -- Insert the new weight - weight_id = "fertiscan_0.0.16".new_metric_unit( + weight_id = "fertiscan_0.0.17".new_metric_unit( read_value::float, record->>'unit', label_info_id, - 'weight'::"fertiscan_0.0.16".metric_type, + 'weight'::"fertiscan_0.0.17".metric_type, FALSE ); END IF; @@ -146,11 +146,11 @@ BEGIN read_unit, '') <> '' THEN - density_id := "fertiscan_0.0.16".new_metric_unit( + density_id := "fertiscan_0.0.17".new_metric_unit( read_value::float, read_unit, label_info_id, - 'density'::"fertiscan_0.0.16".metric_type, + 'density'::"fertiscan_0.0.17".metric_type, FALSE ); END IF; @@ -169,11 +169,11 @@ BEGIN '') <> '' THEN -- Insert the new volume - volume_id := "fertiscan_0.0.16".new_metric_unit( + volume_id := "fertiscan_0.0.17".new_metric_unit( value_float, read_unit, label_info_id, - 'volume'::"fertiscan_0.0.16".metric_type, + 'volume'::"fertiscan_0.0.17".metric_type, FALSE ); END IF; @@ -192,11 +192,11 @@ BEGIN -- '') <> '' -- THEN -- -- Insert the new specification --- specification_id := "fertiscan_0.0.16".new_specification( +-- specification_id := "fertiscan_0.0.17".new_specification( -- (record->>'humidity')::float, -- (record->>'ph')::float, -- (record->>'solubility')::float, --- ingredient_language::"fertiscan_0.0.16".language, +-- ingredient_language::"fertiscan_0.0.17".language, -- label_info_id, -- FALSE -- ); @@ -266,7 +266,7 @@ BEGIN en_value := en_values->>i; -- Insert sub-label without deleting existing data - sub_label_id := "fertiscan_0.0.16".new_sub_label( + sub_label_id := "fertiscan_0.0.17".new_sub_label( fr_value, en_value, label_info_id, @@ -290,12 +290,12 @@ BEGIN -- '') <> '' -- THEN -- -- Insert the new Micronutrient --- micronutrient_id := "fertiscan_0.0.16".new_micronutrient( +-- micronutrient_id := "fertiscan_0.0.17".new_micronutrient( -- record->> 'name', -- (record->> 'value')::float, -- record->> 'unit', -- label_info_id, --- micronutrient_language::"fertiscan_0.0.16".language +-- micronutrient_language::"fertiscan_0.0.17".language -- ); -- END IF; -- END LOOP; @@ -305,7 +305,7 @@ BEGIN -- GUARANTEED -- Loop through each language ('en' and 'fr') - FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.16".LANGUAGE)) + FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.17".LANGUAGE)) LOOP FOR record IN SELECT * FROM jsonb_array_elements(input_json->'guaranteed_analysis'->guaranteed_analysis_language) LOOP @@ -316,12 +316,12 @@ BEGIN '') <> '' THEN -- Insert the new guaranteed_analysis - guaranteed_analysis_id := "fertiscan_0.0.16".new_guaranteed_analysis( + guaranteed_analysis_id := "fertiscan_0.0.17".new_guaranteed_analysis( record->>'name', (record->>'value')::float, record->>'unit', label_info_id, - guaranteed_analysis_language::"fertiscan_0.0.16".language, + guaranteed_analysis_language::"fertiscan_0.0.17".language, FALSE, NULL -- We arent handeling element_id yet ); @@ -330,8 +330,30 @@ BEGIN END LOOP; -- GUARANTEED END +-- REGISTRATION NUMBER + + -- Loop through each registration number in the JSON array + FOR record IN SELECT * FROM jsonb_array_elements(input_json->'registration_number') + LOOP + -- Check make sure we dont create an empty registration number + IF COALESCE(record->>'identifier', + '') <> '' + THEN + -- Insert the new registration number + registration_number_id := "fertiscan_0.0.17".new_registration_number( + record->>'identifier', + label_info_id, + (record->>'type' = 'Ingredient')::BOOLEAN, + null, + FALSE + ); + END IF; + END LOOP; + +-- REGISTRATION NUMBER END + -- INSPECTION - INSERT INTO "fertiscan_0.0.16".inspection ( + INSERT INTO "fertiscan_0.0.17".inspection ( inspector_id, label_info_id, sample_id, picture_set_id, inspection_comment ) VALUES ( user_id, -- Assuming inspector_id is handled separately @@ -348,7 +370,7 @@ BEGIN -- TODO: remove olap transactions from Operational transactions -- Update the Inspection_factual entry with the json - UPDATE "fertiscan_0.0.16".inspection_factual + UPDATE "fertiscan_0.0.17".inspection_factual SET original_dataset = input_json WHERE inspection_factual."inspection_id" = inspection_id_value; diff --git a/fertiscan/db/bytebase/schema_0.0.17.sql b/fertiscan/db/bytebase/schema_0.0.17.sql index b018d0bd..c6e3e519 100644 --- a/fertiscan/db/bytebase/schema_0.0.17.sql +++ b/fertiscan/db/bytebase/schema_0.0.17.sql @@ -100,7 +100,7 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti CREATE TABLE "fertiscan_0.0.17"."label_information" ( "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4(), - "product_name" tefertiscan/db/bytebase/schema_0.0.17.sqlxt, + "product_name" text, "lot_number" text, "npk" text, "n" float, @@ -178,10 +178,12 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti ); CREATE TABLE "fertiscan_0.0.17"."registration_number_information" ( - "id" UUID PRIMARY KEY uuid_generate_v4(), + "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), "identifier" text NOT NULL, "name" text, - "is_an_ingredient" BOOLEAN + "is_an_ingredient" BOOLEAN, + "label_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id) ON DELETE CASCADE, + "edited" BOOLEAN DEFAULT FALSE ); CREATE TABLE "fertiscan_0.0.17"."specification" ( diff --git a/fertiscan/db/bytebase/update_inspection/update_registration_number.sql b/fertiscan/db/bytebase/update_inspection/update_registration_number.sql new file mode 100644 index 00000000..32f82782 --- /dev/null +++ b/fertiscan/db/bytebase/update_inspection/update_registration_number.sql @@ -0,0 +1,31 @@ + +drop FUNCTION IF EXISTS "fertiscan_0.0.17".update_registration_number; +-- Function to update guaranteed analysis: delete old and insert new +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_registration_number( + p_label_id uuid, + new_registration_numbers jsonb +) +RETURNS void AS $$ +DECLARE + reg_num_record jsonb; +BEGIN + -- Delete existing guaranteed analysis for the given label_id + DELETE FROM registration_number_information WHERE label_id = p_label_id; + + FOR reg_num_record IN SELECT * FROM jsonb_array_elements(new_registration_numbers) + LOOP + INSERT INTO registration_number_information ( + identifier, + is_an_ingredient, + label_id, + edited + ) + VALUES ( + reg_num_record->>'identifier', + reg_num_record->>'is_an_ingredient', + p_label_id, + reg_num_record->>'edited' + ); + END LOOP; +END; +$$ LANGUAGE plpgsql; diff --git a/fertiscan/db/bytebase/update_inspection_function.sql b/fertiscan/db/bytebase/update_inspection_function.sql index c7d6a139..2a3035ec 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.16"; +SET search_path TO "fertiscan_0.0.17"; -- Function to upsert location information based on location_id and address -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".upsert_location(location_id uuid, address text) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_location(location_id uuid, address text) RETURNS uuid AS $$ DECLARE new_location_id uuid; @@ -23,7 +23,7 @@ $$ LANGUAGE plpgsql; -- Function to upsert organization information -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".upsert_organization_info(input_org_info jsonb) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_organization_info(input_org_info jsonb) RETURNS uuid AS $$ DECLARE organization_info_id uuid; @@ -82,7 +82,7 @@ $$ LANGUAGE plpgsql; -- Function to update metrics: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_metrics( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_metrics( p_label_id uuid, metrics jsonb ) @@ -157,7 +157,7 @@ $$ LANGUAGE plpgsql; -- Function to update specifications: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_specifications( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_specifications( p_label_id uuid, new_specifications jsonb ) @@ -203,7 +203,7 @@ $$ LANGUAGE plpgsql; -- Function to update ingredients: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_ingredients( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_ingredients( p_label_id uuid, new_ingredients jsonb ) @@ -251,7 +251,7 @@ $$ LANGUAGE plpgsql; -- Function to update micronutrients: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_micronutrients( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_micronutrients( p_label_id uuid, new_micronutrients jsonb ) @@ -295,9 +295,9 @@ BEGIN END; $$ LANGUAGE plpgsql; -drop FUNCTION IF EXISTS "fertiscan_0.0.16".update_guaranteed; +drop FUNCTION IF EXISTS "fertiscan_0.0.17".update_guaranteed; -- Function to update guaranteed analysis: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_guaranteed( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_guaranteed( p_label_id uuid, new_guaranteed jsonb ) @@ -311,7 +311,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.16".LANGUAGE)) + FOR guaranteed_analysis_language IN SELECT unnest(enum_range(NULL::"fertiscan_0.0.17".LANGUAGE)) LOOP FOR guaranteed_record IN SELECT * FROM jsonb_array_elements(new_guaranteed->guaranteed_analysis_language) LOOP @@ -344,7 +344,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.16".check_null_or_empty_sub_label() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".check_null_or_empty_sub_label() RETURNS TRIGGER AS $$ BEGIN -- Check if both text_content_fr and text_content_en are NULL or empty @@ -371,15 +371,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.16".sub_label; +DROP TRIGGER IF EXISTS before_insert_sub_label ON "fertiscan_0.0.17".sub_label; CREATE TRIGGER before_insert_sub_label -BEFORE INSERT ON "fertiscan_0.0.16".sub_label +BEFORE INSERT ON "fertiscan_0.0.17".sub_label FOR EACH ROW -EXECUTE FUNCTION "fertiscan_0.0.16".check_null_or_empty_sub_label(); +EXECUTE FUNCTION "fertiscan_0.0.17".check_null_or_empty_sub_label(); -- Function to update sub labels: delete old and insert new -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_sub_labels( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_sub_labels( p_label_id uuid, new_sub_labels jsonb ) @@ -438,9 +438,9 @@ BEGIN END; $$ LANGUAGE plpgsql; -Drop FUNCTION IF EXISTS "fertiscan_0.0.16".upsert_inspection; +Drop FUNCTION IF EXISTS "fertiscan_0.0.17".upsert_inspection; -- Function to upsert inspection information -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".upsert_inspection( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_inspection( p_inspection_id uuid, p_label_info_id uuid, p_inspector_id uuid, @@ -490,7 +490,7 @@ $$ LANGUAGE plpgsql; -- Function to upsert fertilizer information based on unique fertilizer name -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".upsert_fertilizer( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_fertilizer( p_name text, p_registration_number text, p_owner_id uuid, @@ -526,7 +526,7 @@ $$ LANGUAGE plpgsql; -- Function to update inspection data and related entities, returning an updated JSON -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".update_inspection( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_inspection( p_inspection_id uuid, p_inspector_id uuid, p_input_json jsonb @@ -619,6 +619,9 @@ BEGIN -- Update sub labels related to the label PERFORM update_sub_labels(label_info_id_value, p_input_json); + -- Update the registration number information related to the label + PERFORM update_registration_number(label_info_id_value, p_input_json->'product'->'registration_numbers'); + -- Update the inspection record verified_bool := (p_input_json->>'verified')::boolean; From 8228544669d63550cdbc95de950d6332f8b066bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 6 Nov 2024 21:17:20 +0000 Subject: [PATCH 03/15] Issue #154: new schema 0.0.17 --- db-creation.py | 3 +++ .../db/bytebase/OLAP/guaranteed_triggers.sql | 16 ++++++------ .../db/bytebase/OLAP/ingredient_trigger.sql | 16 ++++++------ .../db/bytebase/OLAP/inspection_triggers.sql | 26 +++++++++---------- .../OLAP/label_information_triggers.sql | 24 ++++++++--------- .../db/bytebase/OLAP/metrics_triggers.sql | 16 ++++++------ .../bytebase/OLAP/micronutrient_triggers.sql | 16 ++++++------ .../bytebase/OLAP/specification_triggers.sql | 16 ++++++------ .../db/bytebase/OLAP/sub_label_triggers.sql | 20 +++++++------- .../new_guaranteed_analysis.sql | 4 +-- .../new_inspection/new_ingredient.sql | 4 +-- .../new_inspection/new_label_information.sql | 4 +-- .../new_inspection/new_metric_unit.sql | 4 +-- .../new_inspection/new_micronutrient.sql | 4 +-- .../new_organization_located.sql | 2 +- .../new_inspection/new_specification.sql | 4 +-- .../bytebase/new_inspection/new_sub_label.sql | 15 +++++------ 17 files changed, 98 insertions(+), 96 deletions(-) diff --git a/db-creation.py b/db-creation.py index 4dd8fa80..a23b7c76 100644 --- a/db-creation.py +++ b/db-creation.py @@ -32,6 +32,9 @@ def create_db(DB_URL, SCHEMA : str): path = "fertiscan/db/bytebase/update_inspection_function.sql" execute_sql_file(cur, path) + path = "fertiscan/db/bytebase/update_inspection" + loop_for_sql_files(cur, path) + path = "fertiscan/db/bytebase/delete_inspection_function.sql" execute_sql_file(cur, path) diff --git a/fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql b/fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql index 691d7dc5..d574591e 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.16".olap_guaranteed_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".guaranteed; +DROP TRIGGER IF EXISTS guaranteed_creation ON "fertiscan_0.0.17".guaranteed; CREATE TRIGGER guaranteed_creation -AFTER INSERT ON "fertiscan_0.0.16".guaranteed +AFTER INSERT ON "fertiscan_0.0.17".guaranteed FOR EACH ROW EXECUTE FUNCTION olap_guaranteed_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_guaranteed_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".guaranteed; +DROP TRIGGER IF EXISTS guaranteed_deletion ON "fertiscan_0.0.17".guaranteed; CREATE TRIGGER guaranteed_deletion -AFTER DELETE ON "fertiscan_0.0.16".guaranteed +AFTER DELETE ON "fertiscan_0.0.17".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 4d96b883..faa33063 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.16".olap_ingredient_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".ingredient; +DROP TRIGGER IF EXISTS ingredient_creation ON "fertiscan_0.0.17".ingredient; CREATE TRIGGER ingredient_creation -AFTER INSERT ON "fertiscan_0.0.16".ingredient +AFTER INSERT ON "fertiscan_0.0.17".ingredient FOR EACH ROW EXECUTE FUNCTION olap_ingredient_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_ingredient_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".ingredient; +DROP TRIGGER IF EXISTS ingredient_deletion ON "fertiscan_0.0.17".ingredient; CREATE TRIGGER ingredient_deletion -AFTER DELETE ON "fertiscan_0.0.16".ingredient +AFTER DELETE ON "fertiscan_0.0.17".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 72375773..f89b857a 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.16".olap_inspection_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".time_dimension ( + INSERT INTO "fertiscan_0.0.17".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.16".inspection_factual ( + INSERT INTO "fertiscan_0.0.17".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.16".inspection; +DROP TRIGGER IF EXISTS inspection_creation ON "fertiscan_0.0.17".inspection; CREATE TRIGGER inspection_creation -AFTER INSERT ON "fertiscan_0.0.16".inspection +AFTER INSERT ON "fertiscan_0.0.17".inspection FOR EACH ROW EXECUTE FUNCTION olap_inspection_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_inspection_update() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".inspection_factual + UPDATE "fertiscan_0.0.17".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.16".inspection; +DROP TRIGGER IF EXISTS inspection_update ON "fertiscan_0.0.17".inspection; CREATE TRIGGER inspection_update -BEFORE UPDATE ON "fertiscan_0.0.16".inspection +BEFORE UPDATE ON "fertiscan_0.0.17".inspection FOR EACH ROW EXECUTE FUNCTION olap_inspection_update(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_inspection_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".olap_inspection_deletion() RETURNS TRIGGER AS $$ BEGIN IF (TG_OP = 'DELETE') THEN IF (OLD.id IS NOT NULL) THEN - DELETE FROM "fertiscan_0.0.16".inspection_factual + DELETE FROM "fertiscan_0.0.17".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.16".inspection; +DROP TRIGGER IF EXISTS inspection_deletion ON "fertiscan_0.0.17".inspection; CREATE TRIGGER inspection_deletion -AFTER DELETE ON "fertiscan_0.0.16".inspection +AFTER DELETE ON "fertiscan_0.0.17".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 a70ec271..fda09992 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.16".olap_label_information_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" ( + INSERT INTO "fertiscan_0.0.17"."label_dimension" ( label_id, company_info_id, manufacturer_info_id ) VALUES ( NEW.id, NEW.company_info_id, NEW.manufacturer_info_id @@ -18,19 +18,19 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS label_information_creation ON "fertiscan_0.0.16".label_information; +DROP TRIGGER IF EXISTS label_information_creation ON "fertiscan_0.0.17".label_information; CREATE TRIGGER label_information_creation -AFTER INSERT ON "fertiscan_0.0.16".label_information +AFTER INSERT ON "fertiscan_0.0.17".label_information FOR EACH ROW EXECUTE FUNCTION olap_label_information_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_label_information_update() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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 @@ BEGIN END; $$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS label_information_update ON "fertiscan_0.0.16".label_information; +DROP TRIGGER IF EXISTS label_information_update ON "fertiscan_0.0.17".label_information; CREATE TRIGGER label_information_update -BEFORE UPDATE ON "fertiscan_0.0.16".label_information +BEFORE UPDATE ON "fertiscan_0.0.17".label_information FOR EACH ROW EXECUTE FUNCTION olap_label_information_update(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_label_information_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + DELETE FROM "fertiscan_0.0.17"."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.16".label_information; +DROP TRIGGER IF EXISTS label_information_deletion ON "fertiscan_0.0.17".label_information; CREATE TRIGGER label_information_deletion -AFTER DELETE ON "fertiscan_0.0.16".label_information +AFTER DELETE ON "fertiscan_0.0.17".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 31339266..e8a95cb4 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.16".olap_metrics_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + EXECUTE format('UPDATE "fertiscan_0.0.17"."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.16".metric; +DROP TRIGGER IF EXISTS metrics_creation ON "fertiscan_0.0.17".metric; CREATE TRIGGER metrics_creation -AFTER INSERT ON "fertiscan_0.0.16".metric +AFTER INSERT ON "fertiscan_0.0.17".metric FOR EACH ROW EXECUTE FUNCTION olap_metrics_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_metrics_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + EXECUTE format('UPDATE "fertiscan_0.0.17"."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.16".metric; +DROP TRIGGER IF EXISTS metrics_deletion ON "fertiscan_0.0.17".metric; CREATE TRIGGER metrics_deletion -AFTER DELETE ON "fertiscan_0.0.16".metric +AFTER DELETE ON "fertiscan_0.0.17".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 13bef86e..e1587a57 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.16".olap_micronutrient_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".micronutrient; +DROP TRIGGER IF EXISTS micronutrient_creation ON "fertiscan_0.0.17".micronutrient; CREATE TRIGGER micronutrient_creation -AFTER INSERT ON "fertiscan_0.0.16".micronutrient +AFTER INSERT ON "fertiscan_0.0.17".micronutrient FOR EACH ROW EXECUTE FUNCTION olap_micronutrient_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_micronutrient_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".micronutrient; +DROP TRIGGER IF EXISTS micronutrient_deletion ON "fertiscan_0.0.17".micronutrient; CREATE TRIGGER micronutrient_deletion -AFTER DELETE ON "fertiscan_0.0.16".micronutrient +AFTER DELETE ON "fertiscan_0.0.17".micronutrient FOR EACH ROW EXECUTE FUNCTION olap_micronutrient_deletion(); diff --git a/fertiscan/db/bytebase/OLAP/specification_triggers.sql b/fertiscan/db/bytebase/OLAP/specification_triggers.sql index 35ff77f1..6e37cea3 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.16".olap_specification_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".specification; +DROP TRIGGER IF EXISTS specification_creation ON "fertiscan_0.0.17".specification; CREATE TRIGGER specification_creation -AFTER INSERT ON "fertiscan_0.0.16".specification +AFTER INSERT ON "fertiscan_0.0.17".specification FOR EACH ROW EXECUTE FUNCTION olap_specification_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_specification_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16"."label_dimension" + UPDATE "fertiscan_0.0.17"."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.16".specification; +DROP TRIGGER IF EXISTS specification_deletion ON "fertiscan_0.0.17".specification; CREATE TRIGGER specification_deletion -AFTER DELETE ON "fertiscan_0.0.16".specification +AFTER DELETE ON "fertiscan_0.0.17".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 d72329d7..8fc8bba1 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.16".olap_sub_label_creation() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".sub_type WHERE sub_type.id = NEW.sub_type_id; + SELECT sub_type.type_en INTO type_str FROM "fertiscan_0.0.17".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.16".label_dimension + EXECUTE format('UPDATE "fertiscan_0.0.17".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.16".sub_label; +DROP TRIGGER IF EXISTS sub_label_creation ON "fertiscan_0.0.17".sub_label; CREATE TRIGGER sub_label_creation -AFTER INSERT ON "fertiscan_0.0.16".sub_label +AFTER INSERT ON "fertiscan_0.0.17".sub_label FOR EACH ROW EXECUTE FUNCTION olap_sub_label_creation(); -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_sub_label_deletion() +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".sub_type WHERE sub_type.id = OLD.sub_type_id; + SELECT sub_type.type_en INTO type_str FROM "fertiscan_0.0.17".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.16".label_dimension + EXECUTE format('UPDATE "fertiscan_0.0.17".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.16".sub_label; +DROP TRIGGER IF EXISTS sub_label_deletion ON "fertiscan_0.0.17".sub_label; CREATE TRIGGER sub_label_deletion -AFTER DELETE ON "fertiscan_0.0.16".sub_label +AFTER DELETE ON "fertiscan_0.0.17".sub_label FOR EACH ROW EXECUTE FUNCTION olap_sub_label_deletion(); diff --git a/fertiscan/db/bytebase/new_inspection/new_guaranteed_analysis.sql b/fertiscan/db/bytebase/new_inspection/new_guaranteed_analysis.sql index 7fc058cc..3282b899 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.16".new_guaranteed_analysis( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_guaranteed_analysis( name TEXT, value FLOAT, unit TEXT, label_id UUID, -language "fertiscan_0.0.16".language, +language "fertiscan_0.0.17".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 391529e7..c8b8e613 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.16".new_ingredient( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_ingredient( name TEXT, value FLOAt, read_unit TEXT, label_id UUID, -language "fertiscan_0.0.16".language, +language "fertiscan_0.0.17".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 ceeef800..a7463cba 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 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".new_label_information( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_label_information( name TEXT, lot_number TEXT, npk TEXT, @@ -21,7 +21,7 @@ DECLARE label_id uuid; record RECORD; BEGIN - SET SEARCH_PATH TO "fertiscan_0.0.16"; + SET SEARCH_PATH TO "fertiscan_0.0.17"; -- LABEL INFORMATION INSERT INTO label_information ( product_name,lot_number, npk, registration_number, n, p, k, guaranteed_title_en, guaranteed_title_fr, title_is_minimal, company_info_id, manufacturer_info_id, 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 bfda764b..04ab341c 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.16".new_metric_unit( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_metric_unit( value FLOAT, read_unit TEXT, label_id UUID, - metric_type "fertiscan_0.0.16".metric_type, + metric_type "fertiscan_0.0.17".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 179d5c6b..6e11300a 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.16".new_micronutrient( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_micronutrient( name TEXT, value FLOAT, unit TEXT, label_id UUID, -language "fertiscan_0.0.16".language, +language "fertiscan_0.0.17".language, edited BOOLEAN = FALSE, element_id int = NULL ) diff --git a/fertiscan/db/bytebase/new_inspection/new_organization_located.sql b/fertiscan/db/bytebase/new_inspection/new_organization_located.sql index 4e04b900..754df4b0 100644 --- a/fertiscan/db/bytebase/new_inspection/new_organization_located.sql +++ b/fertiscan/db/bytebase/new_inspection/new_organization_located.sql @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".new_organization_info_located( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_organization_info_located( name TEXT, address_str TEXT, website TEXT, diff --git a/fertiscan/db/bytebase/new_inspection/new_specification.sql b/fertiscan/db/bytebase/new_inspection/new_specification.sql index 25cfaeac..2c98e107 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.16".new_specification( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_specification( humidity FLOAT, ph FLOAT, solubility FLOAT, -language "fertiscan_0.0.16".language, +language "fertiscan_0.0.17".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 2b519c0d..00af29be 100644 --- a/fertiscan/db/bytebase/new_inspection/new_sub_label.sql +++ b/fertiscan/db/bytebase/new_inspection/new_sub_label.sql @@ -1,12 +1,11 @@ CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".new_sub_label( -content_fr TEXT default null, -content_en TEXT default null, -label_id UUID default null, -sub_type_id UUID default null, -edited BOOLEAN = FALSE -) -RETURNS uuid -LANGUAGE plpgsql + content_fr text DEFAULT NULL::text, + content_en text DEFAULT NULL::text, + label_id uuid DEFAULT NULL::uuid, + sub_type_id uuid DEFAULT NULL::uuid, + edited boolean DEFAULT false) + RETURNS uuid + LANGUAGE plpgsql AS $function$ DECLARE sub_label_id uuid; From 6a7af1dc559d8b1e44efdfbeb7dfc6805d543ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:01:52 +0000 Subject: [PATCH 04/15] Issue #154: add querie module for registration_number --- .../queries/registration_number/__init__.py | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 fertiscan/db/queries/registration_number/__init__.py diff --git a/fertiscan/db/queries/registration_number/__init__.py b/fertiscan/db/queries/registration_number/__init__.py new file mode 100644 index 00000000..c126843c --- /dev/null +++ b/fertiscan/db/queries/registration_number/__init__.py @@ -0,0 +1,74 @@ +""" +This module represent the function for the registration_number_information table + +""" + +from psycopg import Cursor, sql + +from uuid import UUID + +from fertiscan.db.queries.errors import ( + RegistrationNumberCreationError, + RegistrationNumberNotFoundError, + RegistrationNumberQueryError, + RegistrationNumberRetrievalError, + handle_query_errors, +) + + +@handle_query_errors(RegistrationNumberCreationError) +def new_registration_number( + cursor: Cursor, + registration_number, + label_id: UUID, + is_an_ingredient: bool, + read_name: str = None, + edited=False, +): + """ + This function creates a new registration_number in the database. + Parameters: + - cursor (cursor): The cursor of the database. + - registration_number (str): The registration number of the product. + - label_id (uuid): The UUID of the label_information. + - is_an_ingredient (bool): The status of the registration number. + - edited (bool): The edited status of the registration number. + Returns: + - The UUID of the new registration number. + """ + query = sql.SQL( + """ + SELECT new_registration_number(%s, %s, %s, %s, %s); + """ + ) + cursor.execute( + query, (registration_number, label_id, is_an_ingredient, read_name, edited) + ) + if result := cursor.fetchone(): + return result[0] + raise RegistrationNumberCreationError( + "Failed to create Registration Number. No data returned." + ) + + +@handle_query_errors(RegistrationNumberRetrievalError) +def get_registration_numbers_json(cursor: Cursor, label_id: UUID): + """ + This function gets the registration numbers from the database. + Parameters: + - cursor (cursor): The cursor of the database. + - label_id (uuid): The UUID of the label_information. + Returns: + - The registration numbers of the label. + """ + query = sql.SQL( + """ + SELECT get_registration_numbers_json(%s); + """ + ) + cursor.execute(query, (label_id,)) + if result := cursor.fetchone(): + return result[0] + raise RegistrationNumberRetrievalError( + "Failed to get Registration Numbers with the given label_id. No data returned." + ) From 277986b98be3b6eb7708fc879bcf66ed121cd432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:05:13 +0000 Subject: [PATCH 05/15] Issue #154: update registration numbers in metadata module --- .../db/bytebase/new_inspection_function.sql | 2 +- fertiscan/db/metadata/inspection/__init__.py | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fertiscan/db/bytebase/new_inspection_function.sql b/fertiscan/db/bytebase/new_inspection_function.sql index b58e79ec..c6e56ae1 100644 --- a/fertiscan/db/bytebase/new_inspection_function.sql +++ b/fertiscan/db/bytebase/new_inspection_function.sql @@ -343,7 +343,7 @@ BEGIN registration_number_id := "fertiscan_0.0.17".new_registration_number( record->>'identifier', label_info_id, - (record->>'type' = 'Ingredient')::BOOLEAN, + (record->>'is_an_ingredient')::BOOLEAN, null, FALSE ); diff --git a/fertiscan/db/metadata/inspection/__init__.py b/fertiscan/db/metadata/inspection/__init__.py index ac124823..7d5638df 100644 --- a/fertiscan/db/metadata/inspection/__init__.py +++ b/fertiscan/db/metadata/inspection/__init__.py @@ -22,6 +22,7 @@ nutrients, organization, sub_label, + registration_number, ingredient, ) from fertiscan.db.queries.errors import QueryError @@ -87,7 +88,6 @@ class Metrics(ValidatedModel): class ProductInformation(ValidatedModel): name: str | None = None label_id: str | None = None - registration_number: str | None = None lot_number: str | None = None metrics: Metrics | None = Metrics() npk: str | None = None @@ -97,6 +97,10 @@ class ProductInformation(ValidatedModel): k: float | None = None record_keeping: Optional[bool] = None +class RegistrationNumber(ValidatedModel): + registration_number: str | None = None + is_an_ingredient: bool | None = None + edited: Optional[bool] = False class Specification(ValidatedModel): humidity: float | None = None @@ -135,6 +139,7 @@ class Inspection(ValidatedModel): cautions: SubLabel instructions: SubLabel guaranteed_analysis: GuaranteedAnalysis + registration_numbers: Optional[List[RegistrationNumber]] = [] ingredients: ValuesObjects @@ -218,7 +223,6 @@ def build_inspection_import(analysis_form: dict,user_id) -> str: # record keeping is set as null since the pipeline cant output a value yet product = ProductInformation( name=analysis_form.get("fertiliser_name"), - registration_number=analysis_form.get("registration_number"), lot_number=analysis_form.get("lot_number"), metrics=metrics, npk=analysis_form.get("npk"), @@ -329,6 +333,14 @@ def build_inspection_import(analysis_form: dict,user_id) -> str: fr=guaranteed_fr, ) + reg_numbers = [] + for reg_number in analysis_form.get("registration_number", []): + reg_numbers.append(RegistrationNumber( + registration_number=reg_number.get("identifier"), + is_an_ingredient= (reg_number.get("type") == "Ingredient") + ) + ) + inspection_formatted = Inspection( inspector_id=str(user_id), company=company, @@ -337,6 +349,7 @@ def build_inspection_import(analysis_form: dict,user_id) -> str: cautions=cautions, instructions=instructions, guaranteed_analysis=guaranteed, + registration_numbers=reg_numbers, ingredients=ingredients, ) Inspection(**inspection_formatted.model_dump()) @@ -382,6 +395,11 @@ def build_inspection_export(cursor, inspection_id) -> str: ) guaranteed_analysis = GuaranteedAnalysis.model_validate(guaranteed_analysis) + # Retrieve the registration numbers + reg_numbers = registration_number.get_registration_numbers_json(cursor, label_info_id) + reg_numbers = RegistrationNumber.model_validate(reg_numbers) + + # Get the ingredients but if the fertilizer is record keeping, the ingredients are not displayed if not product_info.record_keeping: ingredients = ingredient.get_ingredient_json(cursor, label_info_id) @@ -403,6 +421,7 @@ def build_inspection_export(cursor, inspection_id) -> str: manufacturer=manufacturer, product=product_info, verified=db_inspection.verified, + registration_numbers=reg_numbers, ingredients=ingredients, ) From e6b5a194a7c5c8639fb99081d3c036ba8fa855b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:39:32 +0000 Subject: [PATCH 06/15] Issue #154 reposition based on feedback --- .../get_registration_numbers_json.sql | 2 +- .../db/bytebase/new_inspection_function.sql | 2 +- fertiscan/db/metadata/inspection/__init__.py | 39 +++++++++++-------- tests/fertiscan/analyse.json | 7 +++- tests/fertiscan/inspection.json | 7 +++- tests/fertiscan/inspection_export.json | 6 ++- 6 files changed, 40 insertions(+), 23 deletions(-) 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 3e3da80c..aeacc39d 100644 --- a/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_registration_numbers_json.sql @@ -11,7 +11,7 @@ BEGIN 'registration_numbers', COALESCE(jsonb_agg( jsonb_build_object( - 'identifier', COALESCE(registration_number_information.identifier,Null), + 'registration_number', COALESCE(registration_number_information.identifier,Null), 'is_an_ingredient', COALESCE(registration_number_information.is_an_ingredient,Null), 'edited', COALESCE(registration_number_information.edited,Null) ) diff --git a/fertiscan/db/bytebase/new_inspection_function.sql b/fertiscan/db/bytebase/new_inspection_function.sql index c6e56ae1..7c558586 100644 --- a/fertiscan/db/bytebase/new_inspection_function.sql +++ b/fertiscan/db/bytebase/new_inspection_function.sql @@ -333,7 +333,7 @@ BEGIN -- REGISTRATION NUMBER -- Loop through each registration number in the JSON array - FOR record IN SELECT * FROM jsonb_array_elements(input_json->'registration_number') + FOR record IN SELECT * FROM jsonb_array_elements(input_json-> 'product'-> 'registration_number') LOOP -- Check make sure we dont create an empty registration number IF COALESCE(record->>'identifier', diff --git a/fertiscan/db/metadata/inspection/__init__.py b/fertiscan/db/metadata/inspection/__init__.py index 7d5638df..705849ca 100644 --- a/fertiscan/db/metadata/inspection/__init__.py +++ b/fertiscan/db/metadata/inspection/__init__.py @@ -85,6 +85,11 @@ class Metrics(ValidatedModel): density: Optional[Metric] = Metric() +class RegistrationNumber(ValidatedModel): + registration_number: str | None = None + is_an_ingredient: bool | None = None + edited: Optional[bool] = False + class ProductInformation(ValidatedModel): name: str | None = None label_id: str | None = None @@ -95,12 +100,10 @@ class ProductInformation(ValidatedModel): n: float | None = None p: float | None = None k: float | None = None + verified: Optional[bool] = False + registration_numbers: List[RegistrationNumber] | None = [] record_keeping: Optional[bool] = None -class RegistrationNumber(ValidatedModel): - registration_number: str | None = None - is_an_ingredient: bool | None = None - edited: Optional[bool] = False class Specification(ValidatedModel): humidity: float | None = None @@ -220,11 +223,21 @@ def build_inspection_import(analysis_form: dict,user_id) -> str: metrics = Metrics(weight=weights, volume=volume_obj, density=density_obj) + + reg_numbers = [] + for reg_number in analysis_form.get("registration_number", []): + reg_numbers.append(RegistrationNumber( + registration_number=reg_number.get("identifier"), + is_an_ingredient= (reg_number.get("type") == "Ingredient") + ) + ) + # record keeping is set as null since the pipeline cant output a value yet product = ProductInformation( name=analysis_form.get("fertiliser_name"), lot_number=analysis_form.get("lot_number"), metrics=metrics, + registration_numbers=reg_numbers, npk=analysis_form.get("npk"), warranty=analysis_form.get("warranty"), n=npk[0], @@ -333,14 +346,6 @@ def build_inspection_import(analysis_form: dict,user_id) -> str: fr=guaranteed_fr, ) - reg_numbers = [] - for reg_number in analysis_form.get("registration_number", []): - reg_numbers.append(RegistrationNumber( - registration_number=reg_number.get("identifier"), - is_an_ingredient= (reg_number.get("type") == "Ingredient") - ) - ) - inspection_formatted = Inspection( inspector_id=str(user_id), company=company, @@ -378,6 +383,11 @@ def build_inspection_export(cursor, inspection_id) -> str: metrics.volume = metrics.volume or Metric() metrics.density = metrics.density or Metric() product_info.metrics = metrics + + # Retrieve the registration numbers + reg_numbers = registration_number.get_registration_numbers_json(cursor, label_info_id) + reg_numbers = RegistrationNumber.model_validate(reg_numbers) + product_info.registration_numbers = reg_numbers # get the organizations information (Company and Manufacturer) org = organization.get_organizations_info_json(cursor, label_info_id) @@ -395,11 +405,6 @@ def build_inspection_export(cursor, inspection_id) -> str: ) guaranteed_analysis = GuaranteedAnalysis.model_validate(guaranteed_analysis) - # Retrieve the registration numbers - reg_numbers = registration_number.get_registration_numbers_json(cursor, label_info_id) - reg_numbers = RegistrationNumber.model_validate(reg_numbers) - - # Get the ingredients but if the fertilizer is record keeping, the ingredients are not displayed if not product_info.record_keeping: ingredients = ingredient.get_ingredient_json(cursor, label_info_id) diff --git a/tests/fertiscan/analyse.json b/tests/fertiscan/analyse.json index 69214583..4023cdab 100644 --- a/tests/fertiscan/analyse.json +++ b/tests/fertiscan/analyse.json @@ -8,7 +8,12 @@ "manufacturer_website":"www.agrotechindustries.com", "manufacturer_phone_number":"+1 416 555 0123", "fertiliser_name":"SuperGrow 20-20-20", - "registration_number":"F12345678", + "registration_number":[ + { + "identifier":"F12345678", + "type":"fertilizer_productf" + } + ], "lot_number":"L987654321", "weight":[ { diff --git a/tests/fertiscan/inspection.json b/tests/fertiscan/inspection.json index 9f180317..ec0f6b14 100644 --- a/tests/fertiscan/inspection.json +++ b/tests/fertiscan/inspection.json @@ -39,8 +39,11 @@ "label_id": "cdca3638-a3c0-45df-8c72-74e28a8740c6", "record_keeping": null, "lot_number": "L987654321", - "registration_number": "F12345678" - }, + "registration_numbers": [{ + "registration_number": "F12345678", + "is_an_ingredient": false, + "edited": false + }], "cautions": { "en": [ "Keep out of reach of children.", diff --git a/tests/fertiscan/inspection_export.json b/tests/fertiscan/inspection_export.json index f4756477..94500466 100644 --- a/tests/fertiscan/inspection_export.json +++ b/tests/fertiscan/inspection_export.json @@ -19,7 +19,11 @@ "product": { "name": "SuperGrow 20-20-20", "label_id": "cdca3638-a3c0-45df-8c72-74e28a8740c6", - "registration_number": "F12345678", + "registration_numbers": [{ + "registration_number": "F12345678", + "is_an_ingredient": false, + "edited": false + }], "lot_number": "L987654321", "metrics": { "weight": [ From 9727db63453ab353a35ae59659531c51caa87d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:27:33 +0000 Subject: [PATCH 07/15] Issue #154: added tests --- fertiscan/db/queries/errors.py | 19 +++ .../queries/registration_number/__init__.py | 38 +++++- .../fertiscan/db/test_registration_number.py | 111 ++++++++++++++++++ tests/fertiscan/metadata/test_inspection.py | 24 ++++ 4 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 tests/fertiscan/db/test_registration_number.py diff --git a/fertiscan/db/queries/errors.py b/fertiscan/db/queries/errors.py index 7d53b120..392438a0 100644 --- a/fertiscan/db/queries/errors.py +++ b/fertiscan/db/queries/errors.py @@ -585,6 +585,25 @@ class SubTypeDeleteError(SubTypeQueryError): pass +class RegistrationNumberQueryError(QueryError): + """Base exception for all registration number-related query errors.""" + + pass + +class RegistrationNumberCreationError(RegistrationNumberQueryError): + """Raised when an error occurs during the creation of a registration number.""" + + pass + +class RegistrationNumberRetrievalError(RegistrationNumberQueryError): + """Raised when an error occurs during the retrieval of a registration number.""" + + pass + +class RegistrationNumberNotFoundError(RegistrationNumberQueryError): + """Raised when a registration number is not found.""" + + pass def handle_query_errors(error_cls=QueryError): """Decorator for handling query errors.""" diff --git a/fertiscan/db/queries/registration_number/__init__.py b/fertiscan/db/queries/registration_number/__init__.py index c126843c..1a51c8c1 100644 --- a/fertiscan/db/queries/registration_number/__init__.py +++ b/fertiscan/db/queries/registration_number/__init__.py @@ -4,7 +4,7 @@ """ from psycopg import Cursor, sql - +from fertiscan.db.metadata.inspection import RegistrationNumber from uuid import UUID from fertiscan.db.queries.errors import ( @@ -72,3 +72,39 @@ def get_registration_numbers_json(cursor: Cursor, label_id: UUID): raise RegistrationNumberRetrievalError( "Failed to get Registration Numbers with the given label_id. No data returned." ) + + +@handle_query_errors(RegistrationNumberQueryError) +def update_registration_number( + cursor: Cursor, + registration_numbers: RegistrationNumber, + label_id: UUID, +): + """ + This function updates the registration number in the database. + Parameters: + - cursor (cursor): The cursor of the database. + - registration_number_id (uuid): The UUID of the registration number. + - registration_number (str): The registration number of the product. + - is_an_ingredient (bool): The status of the registration number. + - edited (bool): The edited status of the registration number. + Returns: + - The UUID of the updated registration number. + """ + query = sql.SQL( + """ + SELECT update_registration_number(%s, %s); + """ + ) + cursor.execute( + query, + ( + label_id, + registration_numbers.model_dump_json(), + ), + ) + if result := cursor.fetchone(): + return result[0] + raise RegistrationNumberQueryError( + "Failed to update Registration Number. No data returned." + ) \ No newline at end of file diff --git a/tests/fertiscan/db/test_registration_number.py b/tests/fertiscan/db/test_registration_number.py new file mode 100644 index 00000000..220e3493 --- /dev/null +++ b/tests/fertiscan/db/test_registration_number.py @@ -0,0 +1,111 @@ +""" +This is a test script for the database packages. +It tests the functions in the registration number modules. +""" + +import os +import unittest + +import datastore.db as db +from datastore.db.metadata import validator +from fertiscan.db.metadata.inspection import RegistrationNumber +from fertiscan.db.queries import label, registration_number + +DB_CONNECTION_STRING = os.environ.get("FERTISCAN_DB_URL_TESTING") +if DB_CONNECTION_STRING is None or DB_CONNECTION_STRING == "": + raise ValueError("FERTISCAN_DB_URL is not set") + +DB_SCHEMA = os.environ.get("FERTISCAN_SCHEMA_TESTING") +if DB_SCHEMA is None or DB_SCHEMA == "": + raise ValueError("FERTISCAN_SCHEMA_TESTING is not set") + +class test_registration_number(unittest.TestCase): + 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.registration_number = "123456" + + self.lot_number = "lot_number" + self.product_name = "product_name" + self.npk = "npk" + self.registration_number = "registration_number" + self.n = 10.0 + self.p = 20.0 + self.k = 30.0 + self.weight = None + self.density = None + self.volume = None + self.warranty = "warranty" + self.label_id = label.new_label_information( + self.cursor, + self.product_name, + self.lot_number, + self.npk, + self.registration_number, + self.n, + self.p, + self.k, + None, + None, + False, + None, + None, + ) + + self.is_an_ingredient = False + self.read_name = "read_name" + self.edited = False + + def tearDown(self): + self.con.rollback() + db.end_query(self.con, self.cursor) + + def test_new_registration_number(self): + registration_number_id = registration_number.new_registration_number( + self.cursor, + self.registration_number, + self.label_id, + self.is_an_ingredient, + self.read_name, + self.edited, + ) + self.assertTrue(validator.is_valid_uuid(registration_number_id)) + + def test_get_registration_numbers_json(self): + registration_number.new_registration_number( + self.cursor, + self.registration_number, + self.label_id, + self.is_an_ingredient, + self.read_name, + self.edited, + ) + registration_numbers = registration_number.get_registration_numbers_json( + self.cursor, self.label_id + ) + RegistrationNumber.model_validate(registration_numbers) + self.assertEqual(len(registration_numbers), 1) + + def test_get_registration_numbers_json_empty(self): + registration_numbers = registration_number.get_registration_numbers_json( + self.cursor, self.label_id + ) + RegistrationNumber.model_validate(registration_numbers) + self.assertEqual(len(registration_numbers), 0) + + def test_update_registration_number(self): + olg_data = RegistrationNumber.model_validate(registration_number.get_registration_numbers_json(self.cursor, self.label_id)) + new_reg_number = "654321" + self.sample_registration_number.registration_number = new_reg_numbe + registration_number.update_registration_number( + self.cursor, + self.sample_registration_number, + self.label_id, + ) + new_data = RegistrationNumber.model_validate(registration_number.get_registration_numbers_json(self.cursor, self.label_id)) + + self.assertEqual(olg_data.registration_number, self.registration_number) + self.assertEqual(new_data.registration_number, new_reg_number) + self.assertNotEqual(olg_data.registration_number, new_data.registration_number) + diff --git a/tests/fertiscan/metadata/test_inspection.py b/tests/fertiscan/metadata/test_inspection.py index f65e5397..4952ace4 100644 --- a/tests/fertiscan/metadata/test_inspection.py +++ b/tests/fertiscan/metadata/test_inspection.py @@ -196,6 +196,30 @@ def test_missing_sub_label(self): self.assertListEqual(data["instructions"]["fr"], []) self.assertListEqual(data["instructions"]["en"], []) + def test_empty_registration_number(self): + self.analyse["registration_number"] = [] + formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id) + + # print(formatted_analysis) + inspection_dict = inspection.new_inspection_with_label_info( + self.cursor, self.user_id, self.picture_set_id, formatted_analysis + ) + inspection_id = inspection_dict["inspection_id"] + + # label_information_id = inspection_dict["product"]["label_id"] + + if inspection_id is None: + self.fail("Inspection not created") + data = metadata.build_inspection_export( + self.cursor, str(inspection_id) + ) + data = json.loads(data) + self.assertIsNotNone(data["inspection_id"]) + self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"]) + self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"]) + self.assertEqual(inspection_dict["verified"], data["verified"]) + self.assertEqual(data["product"]["registration_numbers"], []) + def test_unequal_sub_label_lengths_and_order(self): expected_instructions_en = ["one", ""] expected_instructions_fr = ["un", "deux"] From c15de86f1ec8c9d7d82c7e6d33244680cd429496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:06:47 +0000 Subject: [PATCH 08/15] Issue #154: tests are passing --- .../get_guaranteed_analysis.sql | 6 ++-- .../get_inspection/get_ingredients_json.sql | 2 +- .../get_inspection/get_label_info_json.sql | 3 +- .../get_inspection/get_metrics_json.sql | 2 +- .../get_micronutrients_json.sql | 2 +- .../get_inspection/get_organizations_json.sql | 2 +- .../get_registration_numbers_json.sql | 2 +- .../get_inspection/get_specification_json.sql | 2 +- .../bytebase/get_inspection/get_sub_label.sql | 2 +- .../new_inspection/new_label_information.sql | 4 +-- .../new_registration_number.sql | 14 +++----- .../bytebase/new_inspection/new_sub_label.sql | 2 +- .../db/bytebase/new_inspection_function.sql | 6 ++-- fertiscan/db/bytebase/schema_0.0.17.sql | 2 +- .../update_registration_number.sql | 6 ++-- .../bytebase/update_inspection_function.sql | 11 +++++-- fertiscan/db/metadata/inspection/__init__.py | 28 ++++++++++------ fertiscan/db/queries/label/__init__.py | 6 +--- .../queries/registration_number/__init__.py | 33 ++++++++++++++++--- .../archive/archived_test_micronutrients.py | 1 - .../db/archive/archived_test_specification.py | 1 - .../fertiscan/db/test_guaranteed_analysis.py | 1 - tests/fertiscan/db/test_ingredient.py | 1 - tests/fertiscan/db/test_label.py | 20 ++++------- tests/fertiscan/db/test_metric.py | 1 - tests/fertiscan/db/test_organization.py | 2 -- .../fertiscan/db/test_registration_number.py | 29 +++++++++++----- tests/fertiscan/db/test_sub_label.py | 1 - .../archived_test_update_micronutrients.py | 1 - .../archived/archived_test_update_specs.py | 1 - .../test_update_guaranteed.py | 1 - .../test_update_inspection.py | 14 ++++---- .../test_update_inspection_python.py | 1 + .../update_inspection/test_update_metrics.py | 1 - .../test_update_sub_labels.py | 1 - .../test_upsert_fertilizer.py | 6 ++-- tests/fertiscan/inspection.json | 13 +++++--- tests/fertiscan/metadata/test_inspection.py | 4 +++ tests/fertiscan/test_datastore.py | 10 +++--- 39 files changed, 136 insertions(+), 109 deletions(-) diff --git a/fertiscan/db/bytebase/get_inspection/get_guaranteed_analysis.sql b/fertiscan/db/bytebase/get_inspection/get_guaranteed_analysis.sql index f92f29a6..e97540b0 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.16".get_guaranteed_analysis_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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.16".guaranteed + FROM "fertiscan_0.0.17".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.16".label_information + FROM "fertiscan_0.0.17".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 0ecb5d94..fa8ea2ed 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.16".get_ingredients_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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 a0fb32d4..ff9804c1 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.16".get_label_info_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_label_info_json( label_id uuid) RETURNS jsonb LANGUAGE plpgsql @@ -12,7 +12,6 @@ BEGIN product_name, lot_number, npk, - registration_number, n, p, k, diff --git a/fertiscan/db/bytebase/get_inspection/get_metrics_json.sql b/fertiscan/db/bytebase/get_inspection/get_metrics_json.sql index 8a5dd976..f369b211 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.16".get_metrics_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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 272d71b7..86b0f3a0 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.16".get_micronutrient_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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 ea930546..c6ee0849 100644 --- a/fertiscan/db/bytebase/get_inspection/get_organizations_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_organizations_json.sql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".get_organizations_information_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_organizations_information_json( label_id uuid) RETURNS jsonb LANGUAGE plpgsql 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 aeacc39d..cf599ae7 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.16".get_registration_numbers_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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 369cb76c..1b3ad288 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.16".get_specification_json( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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 c8230235..8ff354be 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.16".get_sub_label_json(label_info_id uuid) +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".get_sub_label_json(label_info_id uuid) RETURNS jsonb LANGUAGE plpgsql AS $function$ diff --git a/fertiscan/db/bytebase/new_inspection/new_label_information.sql b/fertiscan/db/bytebase/new_inspection/new_label_information.sql index a7463cba..c354ffcb 100644 --- a/fertiscan/db/bytebase/new_inspection/new_label_information.sql +++ b/fertiscan/db/bytebase/new_inspection/new_label_information.sql @@ -1,9 +1,8 @@ - +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( name TEXT, lot_number TEXT, npk TEXT, - registration_number TEXT, n FLOAt, p FLOAT, k FLOAT, @@ -29,7 +28,6 @@ BEGIN name, lot_number, npk, - registration_number, n, p, k, diff --git a/fertiscan/db/bytebase/new_inspection/new_registration_number.sql b/fertiscan/db/bytebase/new_inspection/new_registration_number.sql index 9bcfb551..9c8bbb6c 100644 --- a/fertiscan/db/bytebase/new_inspection/new_registration_number.sql +++ b/fertiscan/db/bytebase/new_inspection/new_registration_number.sql @@ -1,9 +1,9 @@ -DROP FUNCTION IF EXISTS "fertiscan_0.0.17".new_registration_number(TEXT, BOOLEAN, UUID, TEXT, BOOLEAN); +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( identifier TEXT, label_info_id UUID, - is_an_ingredient BOOLEAN DEFAULT NULL, + is_an_ingredient_val BOOLEAN DEFAULT NULL, read_name TEXT DEFAULT NULL, edited BOOLEAN DEFAULT FALSE ) @@ -13,15 +13,9 @@ AS $function$ DECLARE reg_num_id uuid; BEGIN - INSERT INTO "fertiscan_0.0.17".registration_number_information ( - identifier, - is_an_ingredient, - name, - label_id, - edited - ) + INSERT INTO registration_number_information ("identifier", "is_an_ingredient", "name","label_id","edited") VALUES ( - reg_num, + identifier, is_an_ingredient_val, read_name, label_info_id, diff --git a/fertiscan/db/bytebase/new_inspection/new_sub_label.sql b/fertiscan/db/bytebase/new_inspection/new_sub_label.sql index 00af29be..99da6536 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.16".new_sub_label( +CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".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 7c558586..8d2f3341 100644 --- a/fertiscan/db/bytebase/new_inspection_function.sql +++ b/fertiscan/db/bytebase/new_inspection_function.sql @@ -333,15 +333,15 @@ BEGIN -- REGISTRATION NUMBER -- Loop through each registration number in the JSON array - FOR record IN SELECT * FROM jsonb_array_elements(input_json-> 'product'-> 'registration_number') + FOR record IN SELECT * FROM jsonb_array_elements(input_json-> 'product'-> 'registration_numbers') LOOP -- Check make sure we dont create an empty registration number - IF COALESCE(record->>'identifier', + IF COALESCE(record->>'registration_number', '') <> '' THEN -- Insert the new registration number registration_number_id := "fertiscan_0.0.17".new_registration_number( - record->>'identifier', + record->>'registration_number', label_info_id, (record->>'is_an_ingredient')::BOOLEAN, null, diff --git a/fertiscan/db/bytebase/schema_0.0.17.sql b/fertiscan/db/bytebase/schema_0.0.17.sql index c6e3e519..7fe4dbf2 100644 --- a/fertiscan/db/bytebase/schema_0.0.17.sql +++ b/fertiscan/db/bytebase/schema_0.0.17.sql @@ -204,7 +204,7 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti "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.17"."sub_type" ("id"), CONSTRAINT check_not_all_null CHECK ( - (COALESCE(sub_label.text_content_en, '') <> '') AND + (COALESCE(sub_label.text_content_en, '') <> '') OR (COALESCE(sub_label.text_content_fr, '') <> '') ) ); diff --git a/fertiscan/db/bytebase/update_inspection/update_registration_number.sql b/fertiscan/db/bytebase/update_inspection/update_registration_number.sql index 32f82782..db1c15c7 100644 --- a/fertiscan/db/bytebase/update_inspection/update_registration_number.sql +++ b/fertiscan/db/bytebase/update_inspection/update_registration_number.sql @@ -21,10 +21,10 @@ BEGIN edited ) VALUES ( - reg_num_record->>'identifier', - reg_num_record->>'is_an_ingredient', + reg_num_record->>'registration_number', + (reg_num_record->>'is_an_ingredient')::boolean, p_label_id, - reg_num_record->>'edited' + (reg_num_record->>'edited')::boolean ); END LOOP; END; diff --git a/fertiscan/db/bytebase/update_inspection_function.sql b/fertiscan/db/bytebase/update_inspection_function.sql index 2a3035ec..cc271640 100644 --- a/fertiscan/db/bytebase/update_inspection_function.sql +++ b/fertiscan/db/bytebase/update_inspection_function.sql @@ -545,6 +545,7 @@ DECLARE registration_number text; verified_bool boolean; existing_inspector_id uuid; + registration_number_json jsonb; BEGIN -- Check if the provided inspection_id matches the one in the input JSON json_inspection_id := (p_input_json->>'inspection_id')::uuid; @@ -587,7 +588,6 @@ BEGIN product_name = p_input_json->'product'->>'name', lot_number = p_input_json->'product'->>'lot_number', npk = p_input_json->'product'->>'npk', - registration_number = p_input_json->'product'->>'registration_number', n = (NULLIF(p_input_json->'product'->>'n', '')::float), p = (NULLIF(p_input_json->'product'->>'p', '')::float), k = (NULLIF(p_input_json->'product'->>'k', '')::float), @@ -645,7 +645,14 @@ BEGIN -- Check if the verified field is true, and upsert fertilizer if so IF verified_bool THEN fertilizer_name := p_input_json->'product'->>'name'; - registration_number := p_input_json->'product'->>'registration_number'; + registration_number := NULL; + -- loop through registration numbers to find the one to use + for registration_number_json in SELECT jsonb_array_elements_text(p_input_json->'product'->'registration_numbers') + LOOP + if (registration_number_json->>'is_an_ingredient')::boolean THEN + registration_number := registration_number_json->>'registration_number'; + END IF; + END LOOP; -- Insert organization and get the organization_id IF manufacturer_info_id IS NOT NULL THEN diff --git a/fertiscan/db/metadata/inspection/__init__.py b/fertiscan/db/metadata/inspection/__init__.py index 705849ca..6c6df139 100644 --- a/fertiscan/db/metadata/inspection/__init__.py +++ b/fertiscan/db/metadata/inspection/__init__.py @@ -90,6 +90,7 @@ class RegistrationNumber(ValidatedModel): is_an_ingredient: bool | None = None edited: Optional[bool] = False + class ProductInformation(ValidatedModel): name: str | None = None label_id: str | None = None @@ -146,7 +147,7 @@ class Inspection(ValidatedModel): ingredients: ValuesObjects -def build_inspection_import(analysis_form: dict,user_id) -> str: +def build_inspection_import(analysis_form: dict, user_id) -> str: """ This funtion build an inspection json object from the pipeline of digitalization analysis. This serves as the metadata for the inspection object in the database. @@ -223,12 +224,12 @@ def build_inspection_import(analysis_form: dict,user_id) -> str: metrics = Metrics(weight=weights, volume=volume_obj, density=density_obj) - reg_numbers = [] for reg_number in analysis_form.get("registration_number", []): - reg_numbers.append(RegistrationNumber( - registration_number=reg_number.get("identifier"), - is_an_ingredient= (reg_number.get("type") == "Ingredient") + reg_numbers.append( + RegistrationNumber( + registration_number=reg_number.get("identifier"), + is_an_ingredient=(reg_number.get("type") == "Ingredient"), ) ) @@ -372,7 +373,9 @@ def build_inspection_export(cursor, inspection_id) -> str: This funtion build an inspection json object from the database. """ try: - label_info_id = inspection.get_inspection(cursor,inspection_id)[inspection.LABEL_INFO_ID] + label_info_id = inspection.get_inspection(cursor, inspection_id)[ + inspection.LABEL_INFO_ID + ] # get the label information product_info = label.get_label_information_json(cursor, label_info_id) product_info = ProductInformation(**product_info) @@ -383,11 +386,16 @@ def build_inspection_export(cursor, inspection_id) -> str: metrics.volume = metrics.volume or Metric() metrics.density = metrics.density or Metric() product_info.metrics = metrics - + # Retrieve the registration numbers - reg_numbers = registration_number.get_registration_numbers_json(cursor, label_info_id) - reg_numbers = RegistrationNumber.model_validate(reg_numbers) - product_info.registration_numbers = reg_numbers + reg_numbers = registration_number.get_registration_numbers_json( + cursor, label_info_id + ) + reg_number_model_list = [] + for reg_number in reg_numbers["registration_numbers"]: + + reg_number_model_list.append(RegistrationNumber.model_validate(reg_number)) + product_info.registration_numbers = reg_number_model_list # get the organizations information (Company and Manufacturer) org = organization.get_organizations_info_json(cursor, label_info_id) diff --git a/fertiscan/db/queries/label/__init__.py b/fertiscan/db/queries/label/__init__.py index ae337780..7294a649 100644 --- a/fertiscan/db/queries/label/__init__.py +++ b/fertiscan/db/queries/label/__init__.py @@ -20,7 +20,6 @@ def new_label_information( name: str, lot_number: str, npk: str, - registration_number: str, n: float, p: float, k: float, @@ -38,7 +37,6 @@ def new_label_information( - cursor (cursor): The cursor of the database. - lot_number (str): The lot number of the label_information. - npk (str): The npk of the label_information. - - registration_number (str): The registration number of the label_information. - n (float): The n of the label_information. - p (float): The p of the label_information. - k (float): The k of the label_information. @@ -52,7 +50,7 @@ def new_label_information( - str: The UUID of the label_information """ query = """ - SELECT new_label_information(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s); + SELECT new_label_information(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s); """ cursor.execute( query, @@ -60,7 +58,6 @@ def new_label_information( name, lot_number, npk, - registration_number, n, p, k, @@ -107,7 +104,6 @@ def get_label_information(cursor: Cursor, label_information_id: str) -> dict: product_name, lot_number, npk, - registration_number, n, p, k, diff --git a/fertiscan/db/queries/registration_number/__init__.py b/fertiscan/db/queries/registration_number/__init__.py index 1a51c8c1..06e6f0bf 100644 --- a/fertiscan/db/queries/registration_number/__init__.py +++ b/fertiscan/db/queries/registration_number/__init__.py @@ -4,7 +4,6 @@ """ from psycopg import Cursor, sql -from fertiscan.db.metadata.inspection import RegistrationNumber from uuid import UUID from fertiscan.db.queries.errors import ( @@ -77,7 +76,7 @@ def get_registration_numbers_json(cursor: Cursor, label_id: UUID): @handle_query_errors(RegistrationNumberQueryError) def update_registration_number( cursor: Cursor, - registration_numbers: RegistrationNumber, + registration_numbers, label_id: UUID, ): """ @@ -100,11 +99,37 @@ def update_registration_number( query, ( label_id, - registration_numbers.model_dump_json(), + registration_numbers, ), ) if result := cursor.fetchone(): return result[0] raise RegistrationNumberQueryError( "Failed to update Registration Number. No data returned." - ) \ No newline at end of file + ) + +def get_registration_numbers_from_label(cursor: Cursor, label_id: UUID): + """ + This function gets the registration numbers from the database. + Parameters: + - cursor (cursor): The cursor of the database. + - label_id (uuid): The UUID of the label_information. + Returns: + - The registration numbers of the label. + """ + query = sql.SQL( + """ + SELECT + identifier, + is_an_ingredient, + name, + edited + FROM registration_number_information + WHERE label_id = %s; + """ + ) + cursor.execute(query, (label_id,)) + result = cursor.fetchall() + if result: + return result + \ No newline at end of file diff --git a/tests/fertiscan/db/archive/archived_test_micronutrients.py b/tests/fertiscan/db/archive/archived_test_micronutrients.py index ce2bd9c3..484330c0 100644 --- a/tests/fertiscan/db/archive/archived_test_micronutrients.py +++ b/tests/fertiscan/db/archive/archived_test_micronutrients.py @@ -52,7 +52,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/archive/archived_test_specification.py b/tests/fertiscan/db/archive/archived_test_specification.py index 4bcd7798..0abf364a 100644 --- a/tests/fertiscan/db/archive/archived_test_specification.py +++ b/tests/fertiscan/db/archive/archived_test_specification.py @@ -42,7 +42,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/test_guaranteed_analysis.py b/tests/fertiscan/db/test_guaranteed_analysis.py index 104e2572..f5c8857c 100644 --- a/tests/fertiscan/db/test_guaranteed_analysis.py +++ b/tests/fertiscan/db/test_guaranteed_analysis.py @@ -141,7 +141,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/test_ingredient.py b/tests/fertiscan/db/test_ingredient.py index 607850e2..f73cafd0 100644 --- a/tests/fertiscan/db/test_ingredient.py +++ b/tests/fertiscan/db/test_ingredient.py @@ -37,7 +37,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/test_label.py b/tests/fertiscan/db/test_label.py index 56433ad0..cd7f6c33 100644 --- a/tests/fertiscan/db/test_label.py +++ b/tests/fertiscan/db/test_label.py @@ -25,7 +25,6 @@ def setUp(self): self.product_name = "product_name" self.lot_number = "lot_number" self.npk = "npk" - self.registration_number = "registration_number" self.n = 10.0 self.p = 20.0 self.k = 30.0 @@ -45,7 +44,6 @@ def test_new_label_information(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, @@ -64,7 +62,6 @@ def test_get_label_information(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, @@ -81,15 +78,14 @@ def test_get_label_information(self): self.assertEqual(label_data[1], self.product_name) self.assertEqual(label_data[2], self.lot_number) self.assertEqual(label_data[3], self.npk) - self.assertEqual(label_data[4], self.registration_number) - self.assertEqual(label_data[5], self.n) - self.assertEqual(label_data[6], self.p) - self.assertEqual(label_data[7], self.k) - self.assertEqual(label_data[8], self.guaranteed_analysis_title_en) - self.assertEqual(label_data[9], self.guaranteed_analysis_title_fr) - self.assertEqual(label_data[10], self.guaranteed_is_minimal) + self.assertEqual(label_data[4], self.n) + self.assertEqual(label_data[5], self.p) + self.assertEqual(label_data[6], self.k) + self.assertEqual(label_data[7], self.guaranteed_analysis_title_en) + self.assertEqual(label_data[8], self.guaranteed_analysis_title_fr) + self.assertEqual(label_data[9], self.guaranteed_is_minimal) + self.assertIsNone(label_data[10]) self.assertIsNone(label_data[11]) - self.assertIsNone(label_data[12]) def test_get_label_information_json(self): label_information_id = label.new_label_information( @@ -97,7 +93,6 @@ def test_get_label_information_json(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, @@ -113,7 +108,6 @@ def test_get_label_information_json(self): self.assertEqual(label_data["name"], self.product_name) self.assertEqual(label_data["lot_number"], self.lot_number) self.assertEqual(label_data["npk"], self.npk) - self.assertEqual(label_data["registration_number"], self.registration_number) self.assertEqual(label_data["n"], self.n) self.assertEqual(label_data["p"], self.p) self.assertEqual(label_data["k"], self.k) diff --git a/tests/fertiscan/db/test_metric.py b/tests/fertiscan/db/test_metric.py index b8d96c91..8bf9181f 100644 --- a/tests/fertiscan/db/test_metric.py +++ b/tests/fertiscan/db/test_metric.py @@ -76,7 +76,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/test_organization.py b/tests/fertiscan/db/test_organization.py index a704b035..57b4ccae 100644 --- a/tests/fertiscan/db/test_organization.py +++ b/tests/fertiscan/db/test_organization.py @@ -287,7 +287,6 @@ def test_get_organizations_info_json(self): "label_name", "lot_number", "10-10-10", - "registration_number", 10, 10, 10, @@ -308,7 +307,6 @@ def test_get_organizations_info_json_not_found(self): "label_name", "lot_number", "10-10-10", - "registration_number", 10, 10, 10, diff --git a/tests/fertiscan/db/test_registration_number.py b/tests/fertiscan/db/test_registration_number.py index 220e3493..04c447e1 100644 --- a/tests/fertiscan/db/test_registration_number.py +++ b/tests/fertiscan/db/test_registration_number.py @@ -5,6 +5,7 @@ import os import unittest +import json import datastore.db as db from datastore.db.metadata import validator @@ -42,7 +43,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, @@ -92,20 +92,31 @@ def test_get_registration_numbers_json_empty(self): self.cursor, self.label_id ) RegistrationNumber.model_validate(registration_numbers) - self.assertEqual(len(registration_numbers), 0) + print(registration_numbers) + self.assertEqual(len(registration_numbers["registration_numbers"]), 0) def test_update_registration_number(self): - olg_data = RegistrationNumber.model_validate(registration_number.get_registration_numbers_json(self.cursor, self.label_id)) + registration_number.new_registration_number( + self.cursor, + self.registration_number, + self.label_id, + self.is_an_ingredient, + self.read_name, + self.edited, + ) + old_data = registration_number.get_registration_numbers_json(self.cursor, self.label_id) + self.assertEqual(old_data["registration_numbers"][0]["registration_number"], self.registration_number) new_reg_number = "654321" - self.sample_registration_number.registration_number = new_reg_numbe + new_dict = old_data["registration_numbers"] + new_dict[0]["registration_number"] = new_reg_number + registration_number.update_registration_number( self.cursor, - self.sample_registration_number, + json.dumps(new_dict), self.label_id, ) - new_data = RegistrationNumber.model_validate(registration_number.get_registration_numbers_json(self.cursor, self.label_id)) + new_data = registration_number.get_registration_numbers_json(self.cursor, self.label_id) - self.assertEqual(olg_data.registration_number, self.registration_number) - self.assertEqual(new_data.registration_number, new_reg_number) - self.assertNotEqual(olg_data.registration_number, new_data.registration_number) + self.assertEqual(new_data["registration_numbers"][0]["registration_number"], new_reg_number) + self.assertNotEqual(new_data["registration_numbers"][0]["registration_number"], self.registration_number) diff --git a/tests/fertiscan/db/test_sub_label.py b/tests/fertiscan/db/test_sub_label.py index 6685ffe2..b9fcf9cf 100644 --- a/tests/fertiscan/db/test_sub_label.py +++ b/tests/fertiscan/db/test_sub_label.py @@ -76,7 +76,6 @@ def setUp(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/update_inspection/archived/archived_test_update_micronutrients.py b/tests/fertiscan/db/update_inspection/archived/archived_test_update_micronutrients.py index 688a15d5..668fb7ba 100644 --- a/tests/fertiscan/db/update_inspection/archived/archived_test_update_micronutrients.py +++ b/tests/fertiscan/db/update_inspection/archived/archived_test_update_micronutrients.py @@ -75,7 +75,6 @@ def setUp(self): None, None, None, - None, "test-warranty", self.company_info_id, self.company_info_id, diff --git a/tests/fertiscan/db/update_inspection/archived/archived_test_update_specs.py b/tests/fertiscan/db/update_inspection/archived/archived_test_update_specs.py index e2d1643e..04fd12f8 100644 --- a/tests/fertiscan/db/update_inspection/archived/archived_test_update_specs.py +++ b/tests/fertiscan/db/update_inspection/archived/archived_test_update_specs.py @@ -63,7 +63,6 @@ def setUp(self): None, None, None, - None, "test-warranty", self.company_info_id, self.company_info_id, diff --git a/tests/fertiscan/db/update_inspection/test_update_guaranteed.py b/tests/fertiscan/db/update_inspection/test_update_guaranteed.py index 732623b1..382d0108 100644 --- a/tests/fertiscan/db/update_inspection/test_update_guaranteed.py +++ b/tests/fertiscan/db/update_inspection/test_update_guaranteed.py @@ -85,7 +85,6 @@ def setUp(self): None, None, None, - None, self.company_info_id, self.company_info_id, None, diff --git a/tests/fertiscan/db/update_inspection/test_update_inspection.py b/tests/fertiscan/db/update_inspection/test_update_inspection.py index 2ccc9690..bb28c490 100644 --- a/tests/fertiscan/db/update_inspection/test_update_inspection.py +++ b/tests/fertiscan/db/update_inspection/test_update_inspection.py @@ -232,7 +232,7 @@ def test_update_inspection_with_verified_true(self): # Verify the fertilizer details are correct self.cursor.execute( - "SELECT name, registration_number, owner_id FROM fertilizer WHERE id = %s;", + "SELECT name, owner_id FROM fertilizer WHERE id = %s;", (fertilizer_id,), ) fertilizer_data = self.cursor.fetchone() @@ -241,16 +241,16 @@ def test_update_inspection_with_verified_true(self): altered_inspection.product.name, "The fertilizer name should match the product name in the input model.", ) - self.assertEqual( - fertilizer_data[1], - altered_inspection.product.registration_number, - "The registration number should match the input model.", - ) + # self.assertEqual( + # fertilizer_data[1], + # altered_inspection.product.registration_number, + # "The registration number should match the input model.", + # ) # Check if the owner_id matches the organization information created for the manufacturer self.cursor.execute( "SELECT information_id FROM organization WHERE id = %s;", - (fertilizer_data[2],), + (fertilizer_data[1],), ) organization_information_id = self.cursor.fetchone()[0] diff --git a/tests/fertiscan/db/update_inspection/test_update_inspection_python.py b/tests/fertiscan/db/update_inspection/test_update_inspection_python.py index ccb5f496..5914f6ac 100644 --- a/tests/fertiscan/db/update_inspection/test_update_inspection_python.py +++ b/tests/fertiscan/db/update_inspection/test_update_inspection_python.py @@ -94,6 +94,7 @@ def test_python_function_update_inspection_with_verified_false(self): task = get_full_inspection_json(self.cursor, self.inspection_id) updated_inspection = asyncio.run(task) updated_inspection = json.loads(updated_inspection) + print(updated_inspection) updated_inspection = Inspection.model_validate(updated_inspection) # Assertions using the Inspection model diff --git a/tests/fertiscan/db/update_inspection/test_update_metrics.py b/tests/fertiscan/db/update_inspection/test_update_metrics.py index 001a150d..20428449 100644 --- a/tests/fertiscan/db/update_inspection/test_update_metrics.py +++ b/tests/fertiscan/db/update_inspection/test_update_metrics.py @@ -64,7 +64,6 @@ def setUp(self): None, None, None, - None, self.company_info_id, self.company_info_id, None, diff --git a/tests/fertiscan/db/update_inspection/test_update_sub_labels.py b/tests/fertiscan/db/update_inspection/test_update_sub_labels.py index 22162741..ba18cb9f 100644 --- a/tests/fertiscan/db/update_inspection/test_update_sub_labels.py +++ b/tests/fertiscan/db/update_inspection/test_update_sub_labels.py @@ -114,7 +114,6 @@ def setUp(self): None, None, None, - None, self.company_info_id, self.company_info_id, None, diff --git a/tests/fertiscan/db/update_inspection/test_upsert_fertilizer.py b/tests/fertiscan/db/update_inspection/test_upsert_fertilizer.py index 23e47d1c..515655c6 100644 --- a/tests/fertiscan/db/update_inspection/test_upsert_fertilizer.py +++ b/tests/fertiscan/db/update_inspection/test_upsert_fertilizer.py @@ -63,9 +63,9 @@ def setUp(self): # Insert a label information record to link with inspection self.cursor.execute( - "INSERT INTO label_information (lot_number, npk, registration_number, n, p, k) " - "VALUES (%s, %s, %s, %s, %s, %s) RETURNING id;", - ("L123456789", "10-20-30", "R123456", 10.0, 20.0, 30.0), + "INSERT INTO label_information (lot_number, npk, n, p, k) " + "VALUES ( %s, %s, %s, %s, %s) RETURNING id;", + ("L123456789", "10-20-30", 10.0, 20.0, 30.0), ) self.label_info_id = self.cursor.fetchone()[0] diff --git a/tests/fertiscan/inspection.json b/tests/fertiscan/inspection.json index ec0f6b14..4ababcbe 100644 --- a/tests/fertiscan/inspection.json +++ b/tests/fertiscan/inspection.json @@ -39,11 +39,14 @@ "label_id": "cdca3638-a3c0-45df-8c72-74e28a8740c6", "record_keeping": null, "lot_number": "L987654321", - "registration_numbers": [{ - "registration_number": "F12345678", - "is_an_ingredient": false, - "edited": false - }], + "registration_numbers": [ + { + "registration_number": "F12345678", + "is_an_ingredient": false, + "edited": false + } + ] + }, "cautions": { "en": [ "Keep out of reach of children.", diff --git a/tests/fertiscan/metadata/test_inspection.py b/tests/fertiscan/metadata/test_inspection.py index 4952ace4..85544038 100644 --- a/tests/fertiscan/metadata/test_inspection.py +++ b/tests/fertiscan/metadata/test_inspection.py @@ -60,10 +60,14 @@ def test_perfect_inspection(self): self.cursor, str(inspection_id) ) data = json.loads(data) + self.maxDiff = None self.assertIsNotNone(data["inspection_id"]) self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"]) self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"]) self.assertEqual(inspection_dict["verified"], data["verified"]) + print(data["product"]) + + self.assertListEqual(inspection_dict["product"]["registration_numbers"], data["product"]["registration_numbers"]) self.assertDictEqual(inspection_dict["product"], data["product"]) self.assertDictEqual(inspection_dict["manufacturer"], data["manufacturer"]) self.assertDictEqual(inspection_dict["company"], data["company"]) diff --git a/tests/fertiscan/test_datastore.py b/tests/fertiscan/test_datastore.py index 774a7282..ea41849f 100644 --- a/tests/fertiscan/test_datastore.py +++ b/tests/fertiscan/test_datastore.py @@ -257,7 +257,7 @@ def test_register_analysis_empty(self): "manufacturer_website": "test-website-to-trigger-adress-not-null-test-case", "manufacturer_phone_number": None, "fertiliser_name": None, - "registration_number": None, + "registration_number": [], "lot_number": None, "weight": [], "density": None, @@ -287,7 +287,7 @@ def test_register_analysis_empty(self): # Make sure the manufacturer is created label_data = label.get_label_information(self.cursor, label_id) self.assertIsNotNone(label_data) - self.assertIsNotNone(label_data[12]) + self.assertIsNotNone(label_data[11]) # Verify getters inspection_data = metadata.build_inspection_export( @@ -532,9 +532,9 @@ def test_update_inspection(self): label_info_data = label.get_label_information(self.cursor, label_id) self.assertEqual(label_info_data[3], new_npk) self.assertNotEqual(label_info_data[3], old_npk) - self.assertEqual(label_info_data[8], new_title) - self.assertNotEqual(label_info_data[8], old_title) - self.assertEqual(label_info_data[9], old_title) + self.assertEqual(label_info_data[7], new_title) + self.assertNotEqual(label_info_data[7], old_title) + self.assertEqual(label_info_data[8], old_title) self.assertEqual(label_info_data[13], new_record_keeping) guaranteed_data = nutrients.get_all_guaranteeds(self.cursor, label_id) From 6d4e8ac7a8a9d1cc8337a2096a756ca18a9a5d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:09:27 +0000 Subject: [PATCH 09/15] Issue #154: add error --- fertiscan/db/queries/registration_number/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fertiscan/db/queries/registration_number/__init__.py b/fertiscan/db/queries/registration_number/__init__.py index 06e6f0bf..05dcce1f 100644 --- a/fertiscan/db/queries/registration_number/__init__.py +++ b/fertiscan/db/queries/registration_number/__init__.py @@ -132,4 +132,5 @@ def get_registration_numbers_from_label(cursor: Cursor, label_id: UUID): result = cursor.fetchall() if result: return result + raise RegistrationNumberNotFoundError(f"Failed to get Registration Numbers with the given label_id {label_id}. No data returned.") \ No newline at end of file From b9ee8869f34591c69d23177e33b5f556f6d7235b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:13:36 +0000 Subject: [PATCH 10/15] Issue #154: fix lint --- .../queries/registration_number/__init__.py | 6 ++-- .../fertiscan/db/test_registration_number.py | 30 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/fertiscan/db/queries/registration_number/__init__.py b/fertiscan/db/queries/registration_number/__init__.py index 05dcce1f..28da77ae 100644 --- a/fertiscan/db/queries/registration_number/__init__.py +++ b/fertiscan/db/queries/registration_number/__init__.py @@ -108,6 +108,7 @@ def update_registration_number( "Failed to update Registration Number. No data returned." ) + def get_registration_numbers_from_label(cursor: Cursor, label_id: UUID): """ This function gets the registration numbers from the database. @@ -132,5 +133,6 @@ def get_registration_numbers_from_label(cursor: Cursor, label_id: UUID): result = cursor.fetchall() if result: return result - raise RegistrationNumberNotFoundError(f"Failed to get Registration Numbers with the given label_id {label_id}. No data returned.") - \ No newline at end of file + raise RegistrationNumberNotFoundError( + f"Failed to get Registration Numbers with the given label_id {label_id}. No data returned." + ) diff --git a/tests/fertiscan/db/test_registration_number.py b/tests/fertiscan/db/test_registration_number.py index 04c447e1..aa3e8d5d 100644 --- a/tests/fertiscan/db/test_registration_number.py +++ b/tests/fertiscan/db/test_registration_number.py @@ -20,13 +20,14 @@ if DB_SCHEMA is None or DB_SCHEMA == "": raise ValueError("FERTISCAN_SCHEMA_TESTING is not set") + class test_registration_number(unittest.TestCase): 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.registration_number = "123456" - + self.lot_number = "lot_number" self.product_name = "product_name" self.npk = "npk" @@ -93,7 +94,7 @@ def test_get_registration_numbers_json_empty(self): ) RegistrationNumber.model_validate(registration_numbers) print(registration_numbers) - self.assertEqual(len(registration_numbers["registration_numbers"]), 0) + self.assertEqual(len(registration_numbers["registration_numbers"]), 0) def test_update_registration_number(self): registration_number.new_registration_number( @@ -104,19 +105,30 @@ def test_update_registration_number(self): self.read_name, self.edited, ) - old_data = registration_number.get_registration_numbers_json(self.cursor, self.label_id) - self.assertEqual(old_data["registration_numbers"][0]["registration_number"], self.registration_number) + old_data = registration_number.get_registration_numbers_json( + self.cursor, self.label_id + ) + self.assertEqual( + old_data["registration_numbers"][0]["registration_number"], + self.registration_number, + ) new_reg_number = "654321" new_dict = old_data["registration_numbers"] new_dict[0]["registration_number"] = new_reg_number - + registration_number.update_registration_number( self.cursor, json.dumps(new_dict), self.label_id, ) - new_data = registration_number.get_registration_numbers_json(self.cursor, self.label_id) - - self.assertEqual(new_data["registration_numbers"][0]["registration_number"], new_reg_number) - self.assertNotEqual(new_data["registration_numbers"][0]["registration_number"], self.registration_number) + new_data = registration_number.get_registration_numbers_json( + self.cursor, self.label_id + ) + self.assertEqual( + new_data["registration_numbers"][0]["registration_number"], new_reg_number + ) + self.assertNotEqual( + new_data["registration_numbers"][0]["registration_number"], + self.registration_number, + ) From 84f32f8fea9217b7191c5b4a365f3c7fe2a26238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:16:28 +0000 Subject: [PATCH 11/15] Issue #154: fix rebase errors --- fertiscan/db/bytebase/new_inspection/new_label_information.sql | 2 +- fertiscan/db/queries/label/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fertiscan/db/bytebase/new_inspection/new_label_information.sql b/fertiscan/db/bytebase/new_inspection/new_label_information.sql index c354ffcb..9e0fa580 100644 --- a/fertiscan/db/bytebase/new_inspection/new_label_information.sql +++ b/fertiscan/db/bytebase/new_inspection/new_label_information.sql @@ -23,7 +23,7 @@ BEGIN SET SEARCH_PATH TO "fertiscan_0.0.17"; -- LABEL INFORMATION INSERT INTO label_information ( - product_name,lot_number, npk, registration_number, n, p, k, guaranteed_title_en, guaranteed_title_fr, title_is_minimal, company_info_id, manufacturer_info_id, record_keeping + product_name,lot_number, npk, n, p, k, guaranteed_title_en, guaranteed_title_fr, title_is_minimal, company_info_id, manufacturer_info_id, record_keeping ) VALUES ( name, lot_number, diff --git a/fertiscan/db/queries/label/__init__.py b/fertiscan/db/queries/label/__init__.py index 7294a649..acf1a46d 100644 --- a/fertiscan/db/queries/label/__init__.py +++ b/fertiscan/db/queries/label/__init__.py @@ -50,7 +50,7 @@ def new_label_information( - str: The UUID of the label_information """ query = """ - SELECT new_label_information(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s); + SELECT new_label_information(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); """ cursor.execute( query, From 2e5e33f99c788ad17061c597996f7fce3c6b191e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:17:29 +0000 Subject: [PATCH 12/15] Issue #154: bump Fertiscan --- fertiscan_pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fertiscan_pyproject.toml b/fertiscan_pyproject.toml index 5009f022..806c7c9b 100644 --- a/fertiscan_pyproject.toml +++ b/fertiscan_pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fertiscan_datastore" -version = "1.0.10" +version = "1.0.11" authors = [ { name="Francois Werbrouck", email="francois.werbrouck@inspection.gc.ca" }, { name="Kotchikpa Guy-Landry Allagbe" , email = "kotchikpaguy-landry.allagbe@inspection.gc.ca"} From 7ab76153bd9ee8da1d43448f9fda2f94e1d5b172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:22:22 +0000 Subject: [PATCH 13/15] Issue #154: update schema 17 with rebase update --- fertiscan/db/bytebase/schema_0.0.17.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fertiscan/db/bytebase/schema_0.0.17.sql b/fertiscan/db/bytebase/schema_0.0.17.sql index 7fe4dbf2..a61b7a13 100644 --- a/fertiscan/db/bytebase/schema_0.0.17.sql +++ b/fertiscan/db/bytebase/schema_0.0.17.sql @@ -110,7 +110,8 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti "guaranteed_title_fr" text, "title_is_minimal" boolean, "company_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id), - "manufacturer_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id) + "manufacturer_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id), + "record_keeping" boolean default null ); CREATE TABLE "fertiscan_0.0.17"."label_dimension" ( From c8b4a15afef0c6ec2c018a92ba2e7cb799e29d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:30:07 +0000 Subject: [PATCH 14/15] Issue #154: Fix rebase issue --- fertiscan/db/bytebase/new_inspection_function.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fertiscan/db/bytebase/new_inspection_function.sql b/fertiscan/db/bytebase/new_inspection_function.sql index 8d2f3341..47c8637d 100644 --- a/fertiscan/db/bytebase/new_inspection_function.sql +++ b/fertiscan/db/bytebase/new_inspection_function.sql @@ -222,12 +222,12 @@ BEGIN '') <> '' THEN -- Insert the new ingredient - ingredient_id := "fertiscan_0.0.16".new_ingredient( + ingredient_id := "fertiscan_0.0.17".new_ingredient( record->>'name', read_value::float, read_unit, label_info_id, - ingredient_language::"fertiscan_0.0.16".language, + ingredient_language::"fertiscan_0.0.17".language, NULL, --We cant tell atm NULL, --We cant tell atm FALSE --preset From 0edb01a043bf32e598fa26d5a4194689cf209d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Werbrouck?= <122839953+Francois-Werbrouck@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:39:19 +0000 Subject: [PATCH 15/15] Issue #154: fix rebase broken tests --- fertiscan/db/bytebase/get_inspection/get_label_info_json.sql | 1 - fertiscan/db/metadata/inspection/__init__.py | 2 +- tests/fertiscan/db/test_ingredient.py | 1 - tests/fertiscan/db/test_registration_number.py | 1 + tests/fertiscan/db/update_inspection/test_update_ingredients.py | 1 - tests/fertiscan/test_datastore.py | 2 +- 6 files changed, 3 insertions(+), 5 deletions(-) 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 ff9804c1..efa420e7 100644 --- a/fertiscan/db/bytebase/get_inspection/get_label_info_json.sql +++ b/fertiscan/db/bytebase/get_inspection/get_label_info_json.sql @@ -46,7 +46,6 @@ BEGIN 'p', record.p, 'npk', record.npk, 'lot_number', record.lot_number, - 'registration_number', record.registration_number, 'verified', verified_bool, 'record_keeping', record.record_keeping ); diff --git a/fertiscan/db/metadata/inspection/__init__.py b/fertiscan/db/metadata/inspection/__init__.py index 6c6df139..87b03bde 100644 --- a/fertiscan/db/metadata/inspection/__init__.py +++ b/fertiscan/db/metadata/inspection/__init__.py @@ -434,7 +434,7 @@ def build_inspection_export(cursor, inspection_id) -> str: manufacturer=manufacturer, product=product_info, verified=db_inspection.verified, - registration_numbers=reg_numbers, + registration_numbers=reg_number_model_list, ingredients=ingredients, ) diff --git a/tests/fertiscan/db/test_ingredient.py b/tests/fertiscan/db/test_ingredient.py index f73cafd0..e493cf22 100644 --- a/tests/fertiscan/db/test_ingredient.py +++ b/tests/fertiscan/db/test_ingredient.py @@ -129,7 +129,6 @@ def test_get_ingredient_json_record_keeping(self): self.product_name, self.lot_number, self.npk, - self.registration_number, self.n, self.p, self.k, diff --git a/tests/fertiscan/db/test_registration_number.py b/tests/fertiscan/db/test_registration_number.py index aa3e8d5d..151d33bc 100644 --- a/tests/fertiscan/db/test_registration_number.py +++ b/tests/fertiscan/db/test_registration_number.py @@ -52,6 +52,7 @@ def setUp(self): False, None, None, + None, ) self.is_an_ingredient = False diff --git a/tests/fertiscan/db/update_inspection/test_update_ingredients.py b/tests/fertiscan/db/update_inspection/test_update_ingredients.py index 725291a5..474edcbf 100644 --- a/tests/fertiscan/db/update_inspection/test_update_ingredients.py +++ b/tests/fertiscan/db/update_inspection/test_update_ingredients.py @@ -77,7 +77,6 @@ def setUp(self): None, None, None, - None, False, self.company_info_id, self.company_info_id, diff --git a/tests/fertiscan/test_datastore.py b/tests/fertiscan/test_datastore.py index ea41849f..14792598 100644 --- a/tests/fertiscan/test_datastore.py +++ b/tests/fertiscan/test_datastore.py @@ -535,7 +535,7 @@ def test_update_inspection(self): self.assertEqual(label_info_data[7], new_title) self.assertNotEqual(label_info_data[7], old_title) self.assertEqual(label_info_data[8], old_title) - self.assertEqual(label_info_data[13], new_record_keeping) + self.assertEqual(label_info_data[12], new_record_keeping) guaranteed_data = nutrients.get_all_guaranteeds(self.cursor, label_id) for guaranteed in guaranteed_data: