Skip to content

Commit

Permalink
Issue #212: Fix SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Werbrouck committed Nov 18, 2024
1 parent 5ea3ac9 commit 9a00ef9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BEGIN
))
INTO result_json
FROM organization_information as org
WHERE org.label_id = label_id
WHERE org.label_id = label_id;
RETURN result_json;
END;
$function$;
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_label_information(
guaranteed_title_en TEXT,
guaranteed_title_fr TEXT,
is_minimal boolean,
company_id UUID DEFAULT Null,
manufacturer_id UUID DEFAULT Null,
record_keeping BOOLEAN DEFAULT NULL
)
RETURNS uuid
Expand All @@ -23,7 +21,7 @@ BEGIN
SET SEARCH_PATH TO "fertiscan_0.0.17";
-- LABEL INFORMATION
INSERT INTO label_information (
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
product_name,lot_number, npk, n, p, k, guaranteed_title_en, guaranteed_title_fr, title_is_minimal, record_keeping
) VALUES (
name,
lot_number,
Expand All @@ -34,8 +32,6 @@ BEGIN
guaranteed_title_en,
guaranteed_title_fr,
is_minimal,
company_id,
manufacturer_id,
record_keeping
)
RETURNING id INTO label_id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

DROP FUNCTION IF EXISTS "fertiscan_0.0.17".new_organization_located(TEXT, TEXT, TEXT, TEXT, BOOLEAN, UUID);
CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".new_organization_info_located(
name TEXT,
address_str TEXT,
website TEXT,
phone_number TEXT,
edited BOOLEAN = FALSE
edited BOOLEAN = FALSE,
label_id UUID = NULL
)
RETURNS uuid
LANGUAGE plpgsql
Expand All @@ -17,6 +19,9 @@ BEGIN
-- CHECK IF ANY OF THE INPUTS ARE NOT NULL
IF COALESCE(name, address_str, website, phone_number,'') = '' THEN
RAISE EXCEPTION 'ALL of the input parameters are null';
END IF;
IF label_id IS NULL THEN
RAISE EXCEPTION 'Label_id cannot be null when creating an organization_information';
END IF;
-- CHECK IF ADRESS IS NULL
IF address_str IS NULL THEN
Expand All @@ -36,13 +41,14 @@ END IF;
RETURNING id INTO location_id;
END IF;
END IF;
INSERT INTO organization_information (name,website,phone_number,location_id,edited)
INSERT INTO organization_information ("name","website","phone_number","location_id","edited","label_id")
VALUES (
name,
website,
phone_number,
location_id,
edited
edited,
label_id
)
RETURNING id INTO organization_id;

Expand Down
13 changes: 5 additions & 8 deletions fertiscan/db/bytebase/new_inspection_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ DECLARE
en_values jsonb;
record jsonb;
inspection_id_value uuid;
company_id uuid;
organization_id uuid;
location_id uuid;
weight_id uuid;
density_id uuid;
volume_id uuid;
micronutrient_id uuid;
manufacturer_location_id uuid;
manufacturer_id uuid;
ingredient_id uuid;
guaranteed_analysis_id uuid;
registration_number_id uuid;
Expand Down Expand Up @@ -103,12 +101,10 @@ BEGIN
input_json->'guaranteed_analysis'->'title'->>'en',
input_json->'guaranteed_analysis'->'title'->>'fr',
(input_json->'guaranteed_analysis'->>'is_minimal')::boolean,
company_id,
manufacturer_id,
Null -- record_keeping not handled yet
);

-- Update input_json with company_id
-- Update input_json with label_id
input_json := jsonb_set(input_json, '{product,label_id}', to_jsonb(label_info_id));

--LABEL END
Expand Down Expand Up @@ -369,7 +365,8 @@ BEGIN
record->>'address',
record->>'website',
record->>'phone_number',
FALSE
FALSE,
label_info_id
);
END IF;
END LOOP;
Expand All @@ -387,7 +384,7 @@ BEGIN
)
RETURNING id INTO inspection_id_value;

-- Update input_json with company_id
-- Update input_json with inspection data
input_json := jsonb_set(input_json, '{inspection_id}', to_jsonb(inspection_id_value));
input_json := jsonb_set(input_json, '{inspection_comment}', to_jsonb(''::text));

Expand Down
46 changes: 22 additions & 24 deletions fertiscan/db/bytebase/schema_0.0.17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,6 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti
"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,
"label_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id),
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(),
Expand Down Expand Up @@ -156,6 +132,28 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti
"original_dataset" json
);

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,
"label_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id),
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 TYPE "fertiscan_0.0.17".metric_type as ENUM ('volume', 'weight','density');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@

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

-- 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
new_organization_info_located(
PERFORM new_organization_info_located(
record->>'name',
record->>'address',
record->>'website',
record->>'phone_number',
TRUE
TRUE,
label_info_id
);
else
-- UPDATE THE LOCATION
Expand Down

0 comments on commit 9a00ef9

Please sign in to comment.