Skip to content

Commit

Permalink
Issue #218: All tests are passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Werbrouck committed Nov 29, 2024
1 parent c6d60c1 commit b29424a
Show file tree
Hide file tree
Showing 38 changed files with 580 additions and 669 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ ml_structure.json
.DS_Store
.venv
venv
.vscode/extensions.json
3 changes: 2 additions & 1 deletion db-creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datastore.db as db
from psycopg import sql

DB_URL = os.environ.get("FERTISCAN_DB_URL_TESTING")
DB_URL = os.environ.get("FERTISCAN_DB_URL")
SCHEMA = os.environ.get("FERTISCAN_SCHEMA_TESTING")

def create_db(DB_URL, SCHEMA : str):
Expand Down Expand Up @@ -63,4 +63,5 @@ def execute_sql_file(cursor,sql_file):

if __name__ == "__main__":
print("Creating the database for schema: ", SCHEMA)
print("Database URL: ", DB_URL)
create_db(DB_URL=DB_URL, SCHEMA=SCHEMA)
4 changes: 1 addition & 3 deletions fertiscan/db/bytebase/OLAP/inspection_triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ BEGIN
) RETURNING id INTO time_id;
-- Create the Inspection_factual entry
INSERT INTO "fertiscan_0.0.18".inspection_factual (
inspection_id, inspector_id, label_info_id, time_id, sample_id, company_id, manufacturer_id, picture_set_id, original_dataset
inspection_id, inspector_id, label_info_id, time_id, sample_id, picture_set_id, original_dataset
) VALUES (
NEW.id,
NEW.inspector_id,
NEW.label_info_id,
time_id,
NULL, -- NOT handled yet
NULL, -- IS not defined yet
NULL, -- IS not defined yet
NEW.picture_set_id,
NULL
);
Expand Down
18 changes: 10 additions & 8 deletions fertiscan/db/bytebase/delete_inspection_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
SET search_path TO "fertiscan_0.0.18";

-- Trigger function to handle after organization_information delete for location deletion
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".after_org_info_delete_location_trig()
DROP TRIGGER IF EXISTS after_organization_delete_main_location ON "fertiscan_0.0.18".organization;
drop FUNCTION IF EXISTS "fertiscan_0.0.18".after_org_delete_location_trig();
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".after_org_delete_location_trig()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
IF OLD.location_id IS NOT NULL THEN
IF OLD.main_location_id IS NOT NULL THEN
BEGIN
DELETE FROM "fertiscan_0.0.18".location
WHERE id = OLD.location_id;
WHERE id = OLD.main_location_id;
EXCEPTION WHEN foreign_key_violation THEN
RAISE NOTICE 'Location % is still referenced by another record and cannot be deleted.', OLD.location_id;
RAISE NOTICE 'Location % is still referenced by another record and cannot be deleted.', OLD.main_location_id;
END;
END IF;

Expand All @@ -21,11 +23,10 @@ END;
$$;

-- Trigger definition on organization_information table for location deletion
DROP TRIGGER IF EXISTS after_organization_information_delete_location ON "fertiscan_0.0.18".organization_information;
CREATE TRIGGER after_organization_information_delete_location
AFTER DELETE ON "fertiscan_0.0.18".organization_information
CREATE TRIGGER after_organization_delete_main_location
AFTER DELETE ON "fertiscan_0.0.18".organization
FOR EACH ROW
EXECUTE FUNCTION "fertiscan_0.0.18".after_org_info_delete_location_trig();
EXECUTE FUNCTION "fertiscan_0.0.18".after_org_delete_location_trig();

-- Function to delete an inspection and related data
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".delete_inspection(
Expand Down Expand Up @@ -96,6 +97,7 @@ BEGIN
--This was the only inspection for the fertilizer so we delete it
DELETE FROM "fertiscan_0.0.18".fertilizer
WHERE id = OLD.fertilizer_id;
end if;
END IF;

RETURN NULL;
Expand Down
40 changes: 22 additions & 18 deletions fertiscan/db/bytebase/get_inspection/get_organizations_json.sql
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@

--Unverified organization data
DROP IF EXISTS FUNCTION "fertiscan_0.0.18".get_organizations_information_json(label_id uuid);
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".get_organizations_information_json(label_id_value uuid);
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_organizations_information_json(
label_id uuid)
label_id_value uuid)
RETURNS jsonb
LANGUAGE plpgsql
AS $function$
DECLARE
result_json jsonb;
BEGIN
SELECT jsonb_agg(jsonb_build_object(
SELECT jsonb_build_object(
'organizations',
jsonb_build_object(
'id', COALESCE(org.id, Null),
'name', COALESCE(org.name, Null),
'address', COALESCE(org.address, Null),
'phone_number', COALESCE(org.phone_number, Null),
'website', COALESCE(org.website, Null),
'edited', COALESCE(org.edited, Null),
'is_main_contact', COALESCE(org.is_main_contact, Null)
)
))
COALESCE(jsonb_agg(
jsonb_build_object(
'id', COALESCE(org.id, Null),
'name', COALESCE(org.name, Null),
'address', COALESCE(org.address, Null),
'phone_number', COALESCE(org.phone_number, Null),
'website', COALESCE(org.website, Null),
'edited', COALESCE(org.edited, Null),
'is_main_contact', COALESCE(org.is_main_contact, Null)
)
), '[]'::jsonb)
)
INTO result_json
FROM organization_information as org
WHERE org.label_id = label_id;
WHERE org.label_id = label_id_value;
RETURN result_json;
END;
$function$;

-- verified organization
DROP IF EXISTS FUNCTION "fertiscan_0.0.18".get_organizations_json();
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".get_organizations_json();
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_organizations_json()
RETURNS jsonb
LANGUAGE plpgsql
AS $function$
DECLARE
result_json jsonb;
BEGIN
SELECT jsonb_agg(jsonb_build_object(
SELECT jsonb_build_object(
'organizations',
jsonb_build_object(
jsonb_agg(jsonb_build_object(
'id', COALESCE(org.id, Null),
'name', COALESCE(org.name, Null),
'address', COALESCE(org.address, Null),
'phone_number', COALESCE(org.phone_number, Null),
'website', COALESCE(org.website, Null),
'updated_at', COALESCE(org.updated_at, Null),
'updated_at', COALESCE(org.updated_at, Null)
)
))
INTO result_json
FROM organization_information as org;
RETURN result_json;
END;
$function$;

10 changes: 10 additions & 0 deletions fertiscan/db/bytebase/new_inspection_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ DECLARE
fr_value text;
en_value text;
flag boolean;
counter int;
orgs_ids uuid[];
BEGIN

-- COMPANY
Expand Down Expand Up @@ -351,6 +353,7 @@ BEGIN

-- ORGANIZATIONS INFO
flag := TRUE;
counter := 0;
FOR record in SELECT * FROM jsonb_array_elements(input_json->'organizations')
LOOP
-- Check if any of the fields are not null
Expand All @@ -372,8 +375,15 @@ BEGIN
);
-- The flag is used to mark the first Org as the main contact
if flag THEN
-- Update the first organization as the main contact in the form
input_json := jsonb_set(input_json,ARRAY['organizations',counter::text,'is_main_contact'],to_jsonb(TRUE));
flag := FALSE;
END IF;
--array_append(orgs_ids,organization_id)
if organization_id is not null then
input_json := jsonb_set(input_json,ARRAY['organizations',counter::text,'id'],to_jsonb(organization_id));
end if;
counter := counter + 1;
END IF;
END LOOP;
-- ORGANIZATIONS INFO END
Expand Down
6 changes: 3 additions & 3 deletions fertiscan/db/bytebase/schema_0.0.18.sql
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ create schema "fertiscan_0.0.18";
"name" text,
"address" text NOT NULL,
"address_number" text,
"city" text NOT NULL,
"postal_code" text NOT NULL,
"city" text,
"postal_code" text,
"region_id" uuid REFERENCES "fertiscan_0.0.18".region(id)
);

Expand Down Expand Up @@ -271,7 +271,7 @@ create schema "fertiscan_0.0.18";
"upload_date" timestamp DEFAULT CURRENT_TIMESTAMP,
"update_at" timestamp DEFAULT CURRENT_TIMESTAMP,
"latest_inspection_id" uuid REFERENCES "fertiscan_0.0.18".inspection(id) ON DELETE SET NULL,
"main_contact" uuid REFERENCES "fertiscan_0.0.18".organization(id) ON DELETE SET NULL
"main_contact_id" uuid REFERENCES "fertiscan_0.0.18".organization(id) ON DELETE SET NULL
);-- It should actually try to seek if there are any other organization that can be found under the latest_inspection organisation_information instead of setting it to null

Alter table "fertiscan_0.0.18".inspection ADD "fertilizer_id" uuid REFERENCES "fertiscan_0.0.18".fertilizer(id);
Expand Down
61 changes: 61 additions & 0 deletions fertiscan/db/bytebase/update_inspection/update_sub_label.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

-- Function to update sub labels: delete old and insert new
Drop FUNCTION IF EXISTS "fertiscan_0.0.18".update_sub_labels(uuid, jsonb);
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_sub_labels(
p_label_id uuid,
new_sub_labels jsonb
)
RETURNS void AS $$
DECLARE
sub_type_rec RECORD;
fr_values jsonb;
en_values jsonb;
i int;
max_length int;
fr_value text;
en_value text;
BEGIN
-- Delete existing sub labels for the given label_id
DELETE FROM sub_label WHERE label_id = p_label_id;

-- Loop through each sub_type
FOR sub_type_rec IN SELECT id, type_en FROM sub_type
LOOP
-- Extract the French and English arrays for the current sub_type
fr_values := COALESCE(new_sub_labels->sub_type_rec.type_en->'fr', '[]'::jsonb);
en_values := COALESCE(new_sub_labels->sub_type_rec.type_en->'en', '[]'::jsonb);

-- Determine the maximum length of the arrays
max_length := GREATEST(
jsonb_array_length(fr_values),
jsonb_array_length(en_values)
);

-- Check if lengths are not equal, and raise a notice
IF jsonb_array_length(en_values) != jsonb_array_length(fr_values) THEN
RAISE NOTICE 'Array length mismatch for sub_type: %, EN length: %, FR length: %',
sub_type_rec.type_en, jsonb_array_length(en_values), jsonb_array_length(fr_values);
END IF;

-- Loop through the indices up to the maximum length
FOR i IN 0..(max_length - 1)
LOOP
-- Extract values or set to empty string if not present
fr_value := fr_values->>i;
en_value := en_values->>i;

-- Insert sub label record
INSERT INTO sub_label (
text_content_fr, text_content_en, label_id, edited, sub_type_id
)
VALUES (
fr_value,
en_value,
p_label_id,
NULL, -- not handled
sub_type_rec.id
);
END LOOP;
END LOOP;
END;
$$ LANGUAGE plpgsql;
20 changes: 12 additions & 8 deletions fertiscan/db/bytebase/update_inspection/upsert_organization.sql
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@

DROP IF EXISTS FUNCTION "fertiscan_0.0.18".upsert_organization();
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".upsert_organization();
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_organization(org_info_id uuid)
RETURNS id AS $$
RETURNS uuid
LANGUAGE plpgsql
AS $function$
DECLARE
record record;
address_str TEXT;
org_id uuid;
BEGIN
-- Select the row from the organization_information table
SELECT * INTO record FROM organization_information WHERE id = org_info_id;
SELECT * INTO record FROM organization_information WHERE "id" = org_info_id;

-- Check if the organization exists
Select id INTO org_id FROM organization WHERE name ILIKE record.name;
Select "id" INTO org_id FROM organization WHERE name ILIKE record.name;

-- UPSERT DATA INTO THE ORGANIZATION TABLE
IF org_id IS NULL THEN
INSERT INTO organization ("website","phone_number","address","main_location_id")
INSERT INTO organization ("name","website","phone_number","address","main_location_id")
VALUES (
record.name,
record.website,
record.phone_number,
record.address,
Null -- Not re-implemented yet
)
RETURNING id INTO org_id;
RETURNING "id" INTO org_id;
ELSE
UPDATE organization SET
"website" = record.website,
"phone_number" = record.phone_number,
"address" = record.address,
"main_location_id" = Null -- Not re-implemented yet
WHERE id = org_id
RETURNING id INTO org_id;
WHERE id = org_id;
END IF;
RETURN org_id;
END;
$function$;

Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

-- Function to upsert organization information
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".upsert_organization_info(jsonb, uuid);
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_organization_info(input_org_info jsonb, label_info_id uuid)
RETURNS void AS $$
RETURNS jsonb AS $$
DECLARE
record jsonb;
address_str TEXT;
location_id uuid;
new_id uuid;
index INT;
result jsonb;
BEGIN

index := 0;
result := input_org_info;
-- loop each orgs in the input_org_info
for record in SELECT * FROM jsonb_array_elements(input_org_info)
loop
if record->>'id' IS NULL THEN
PERFORM new_organization_info_located(
new_id := new_organization_information(
record->>'name',
record->>'address',
record->>'website',
Expand All @@ -22,6 +25,8 @@ BEGIN
label_info_id,
(record->>'is_main_contact')::boolean
);
-- add ids to the jsonb
result := jsonb_set(result, array[index::text,'id'], to_jsonb(new_id));
else
-- UPDATE THE ORGANIZATION INFORMATION
UPDATE organization_information SET
Expand All @@ -31,8 +36,10 @@ BEGIN
"address" = address_str,
"edited" = (record->>'edited')::boolean,
"is_main_contact" = (record->>'is_main_contact')::boolean
WHERE "id" = record->>'id';
WHERE id = (record->>'id')::UUID;
end if;
index := index + 1;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
Loading

0 comments on commit b29424a

Please sign in to comment.