Skip to content

Commit

Permalink
db: Improve debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
jhf committed May 22, 2024
1 parent f80e2a9 commit 4578924
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions dbseed/create-db-structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5932,7 +5932,7 @@ DECLARE
BEGIN
new_data := to_jsonb(NEW);
-- Loop through each conflicting row
RAISE DEBUG 'NEW row %', new_data;
RAISE DEBUG 'UPSERT row %', new_data;
-- Remove fields that are generated by the database,
-- since we don't wish to override them with NULL
-- and get a constraint error.
Expand Down Expand Up @@ -6003,7 +6003,7 @@ BEGIN
FOR existing IN EXECUTE existing_query USING NEW, existing_id
LOOP
existing_data := to_jsonb(existing);
RAISE DEBUG 'Existing row %', existing_data;
RAISE DEBUG 'EXISTING row %', existing_data;

delete_existing_sql := format($$
DELETE FROM %1$I.%2$I
Expand All @@ -6016,26 +6016,29 @@ BEGIN
WHEN 'existing_adjacent_valid_from' THEN
IF existing.equivalent THEN
RAISE DEBUG 'Upsert Case: existing_adjacent_valid_from AND equivalent';
RAISE DEBUG 'DELETE EXISTING';
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
NEW.valid_from := existing.valid_from;
END IF;
WHEN 'existing_adjacent_valid_to' THEN
IF existing.equivalent THEN
RAISE DEBUG 'Upsert Case: existing_adjacent_valid_to AND equivalent';
RAISE DEBUG 'DELETE EXISTING';
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
NEW.valid_to := existing.valid_to;
END IF;
WHEN 'existing_overlaps_valid_from' THEN
IF existing.equivalent THEN
RAISE DEBUG 'Upsert Case: existing_overlaps_valid_from AND equivalent';
RAISE DEBUG 'DELETE EXISTING';
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
NEW.valid_from := existing.valid_from;
ELSE
RAISE DEBUG 'Upsert Case: existing_overlaps_valid_from AND different';
adjusted_valid_to := NEW.valid_from - interval '1 day';
RAISE DEBUG 'adjusted_valid_to = %', adjusted_valid_to;
IF adjusted_valid_to <= existing.valid_from THEN
RAISE DEBUG 'Deleting existing with zero valid duration %.%(id=%)', schema_name, table_name, existing.id;
RAISE DEBUG 'DELETE EXISTING with zero valid duration %.%(id=%)', schema_name, table_name, existing.id;
EXECUTE EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
ELSE
RAISE DEBUG 'Adjusting existing row %.%(id=%)', schema_name, table_name, existing.id;
Expand All @@ -6052,6 +6055,7 @@ BEGIN
WHEN 'inside_existing' THEN
IF existing.equivalent THEN
RAISE DEBUG 'Upsert Case: inside_existing AND equivalent';
RAISE DEBUG 'DELETE EXISTING';
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
NEW.valid_from := existing.valid_from;
NEW.valid_to := existing.valid_to;
Expand All @@ -6063,9 +6067,10 @@ BEGIN
RAISE DEBUG 'adjusted_valid_to = %', adjusted_valid_to;
IF adjusted_valid_to <= existing.valid_from THEN
RAISE DEBUG 'Deleting existing with zero valid duration %.%(id=%)', schema_name, table_name, existing.id;
RAISE DEBUG 'DELETE EXISTING';
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
ELSE
RAISE DEBUG 'Adjusting existing row %.%(id=%)', schema_name, table_name, existing.id;
RAISE DEBUG 'ADJUSTING EXISTING row %.%(id=%)', schema_name, table_name, existing.id;
EXECUTE format($$
UPDATE %1$I.%2$I
SET valid_to = $1
Expand All @@ -6089,19 +6094,20 @@ BEGIN
END IF;
WHEN 'contains_existing' THEN
RAISE DEBUG 'Upsert Case: contains_existing';
RAISE DEBUG 'Deleting existing contained by NEW %.%(id=%)', schema_name, table_name, existing.id;
RAISE DEBUG 'DELETE EXISTING contained by NEW %.%(id=%)', schema_name, table_name, existing.id;
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
WHEN 'existing_overlaps_valid_to' THEN
IF existing.equivalent THEN
RAISE DEBUG 'Upsert Case: existing_overlaps_valid_to AND equivalent';
RAISE DEBUG 'DELETE EXISTING';
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
NEW.valid_to := existing.valid_to;
ELSE
RAISE DEBUG 'Upsert Case: existing_overlaps_valid_to AND different';
adjusted_valid_from := NEW.valid_to + interval '1 day';
RAISE DEBUG 'adjusted_valid_from = %', adjusted_valid_from;
IF existing.valid_to < adjusted_valid_from THEN
RAISE DEBUG 'Deleting existing with zero valid duration %.%(id=%)', schema_name, table_name, existing.id;
RAISE DEBUG 'DELETE EXISTING with zero valid duration %.%(id=%)', schema_name, table_name, existing.id;
EXECUTE delete_existing_sql USING existing.id, existing.valid_from, existing.valid_to;
ELSE
RAISE DEBUG 'Adjusting existing row %.%(id=%)', schema_name, table_name, existing.id;
Expand All @@ -6128,7 +6134,7 @@ BEGIN
new_base_data := jsonb_set(new_base_data, '{id}', existing_id::text::jsonb, true);
END IF;

RAISE DEBUG 'NEW %.%(%)', schema_name, table_name, new_base_data;
RAISE DEBUG 'INSERT %.%(%)', schema_name, table_name, new_base_data;
EXECUTE format('INSERT INTO %1$I.%2$I(%3$s) VALUES (%4$s) RETURNING *', schema_name, table_name,
(SELECT string_agg(quote_ident(key), ', ' ORDER BY key) FROM jsonb_each_text(new_base_data)),
(SELECT string_agg(quote_nullable(value), ', ' ORDER BY key) FROM jsonb_each_text(new_base_data)))
Expand Down

0 comments on commit 4578924

Please sign in to comment.