basis.randomInRange, basis.jsonb_changes_delta, asis.bigIntHash, basis.tableColumnNames, basis.raiseException, basis.assertTrue

This commit is contained in:
Michael Hoennig 2024-09-13 08:13:09 +02:00
parent 47926d0d84
commit 984cf0ef3b
8 changed files with 18 additions and 18 deletions

View File

@ -10,9 +10,9 @@
to be used for test data generation. to be used for test data generation.
Example: Example:
randomInRange(0, 4) might return any of 0, 1, 2, 3, 4 basis.randomInRange(0, 4) might return any of 0, 1, 2, 3, 4
*/ */
create or replace function randomInRange(min integer, max integer) create or replace function basis.randomInRange(min integer, max integer)
returns integer returns integer
returns null on null input returns null on null input
language 'plpgsql' as $$ language 'plpgsql' as $$

View File

@ -9,7 +9,7 @@
This is a kind of right sided json diff. This is a kind of right sided json diff.
*/ */
create or replace function jsonb_changes_delta(oldJson jsonb, newJson jsonb) create or replace function basis.jsonb_changes_delta(oldJson jsonb, newJson jsonb)
returns jsonb returns jsonb
called on null input called on null input
language plpgsql as $$ language plpgsql as $$
@ -31,7 +31,7 @@ begin
if jsonb_typeof(newJson -> (oldJsonElement.key)) = 'object' then if jsonb_typeof(newJson -> (oldJsonElement.key)) = 'object' then
diffJson = diffJson || diffJson = diffJson ||
jsonb_build_object(oldJsonElement.key, jsonb_build_object(oldJsonElement.key,
jsonb_changes_delta(oldJsonElement.value, newJson -> (oldJsonElement.key))); basis.jsonb_changes_delta(oldJsonElement.value, newJson -> (oldJsonElement.key)));
end if; end if;
else else
diffJson = diffJson || jsonb_build_object(oldJsonElement.key, null); diffJson = diffJson || jsonb_build_object(oldJsonElement.key, null);
@ -49,30 +49,30 @@ do language plpgsql $$
actual text; actual text;
begin begin
select jsonb_changes_delta(null::jsonb, null::jsonb) into actual; select basis.jsonb_changes_delta(null::jsonb, null::jsonb) into actual;
if actual is not null then if actual is not null then
raise exception 'jsonb_diff #1 failed:% expected: %,% actually: %', E'\n', expected, E'\n', actual; raise exception 'jsonb_diff #1 failed:% expected: %,% actually: %', E'\n', expected, E'\n', actual;
end if; end if;
select jsonb_changes_delta(null::jsonb, '{"a": "new"}'::jsonb) into actual; select basis.jsonb_changes_delta(null::jsonb, '{"a": "new"}'::jsonb) into actual;
expected := '{"a": "new"}'::jsonb; expected := '{"a": "new"}'::jsonb;
if actual <> expected then if actual <> expected then
raise exception 'jsonb_diff #2 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual; raise exception 'jsonb_diff #2 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual;
end if; end if;
select jsonb_changes_delta('{"a": "old"}'::jsonb, '{"a": "new"}'::jsonb) into actual; select basis.jsonb_changes_delta('{"a": "old"}'::jsonb, '{"a": "new"}'::jsonb) into actual;
expected := '{"a": "new"}'::jsonb; expected := '{"a": "new"}'::jsonb;
if actual <> expected then if actual <> expected then
raise exception 'jsonb_diff #3 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual; raise exception 'jsonb_diff #3 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual;
end if; end if;
select jsonb_changes_delta('{"a": "old"}'::jsonb, '{"a": "old"}'::jsonb) into actual; select basis.jsonb_changes_delta('{"a": "old"}'::jsonb, '{"a": "old"}'::jsonb) into actual;
expected := '{}'::jsonb; expected := '{}'::jsonb;
if actual <> expected then if actual <> expected then
raise exception 'jsonb_diff #4 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual; raise exception 'jsonb_diff #4 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual;
end if; end if;
select jsonb_changes_delta( select basis.jsonb_changes_delta(
$json${ $json${
"a": "same", "a": "same",
"b": "old", "b": "old",

View File

@ -6,7 +6,7 @@
--changeset numeric-hash-functions:1 endDelimiter:--// --changeset numeric-hash-functions:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
create function bigIntHash(text) returns bigint as $$ create function basis.bigIntHash(text) returns bigint as $$
select ('x'||substr(md5($1),1,16))::bit(64)::bigint; select ('x'||substr(md5($1),1,16))::bit(64)::bigint;
$$ language sql; $$ language sql;
--// --//

View File

@ -6,7 +6,7 @@
--changeset table-columns-function:1 endDelimiter:--// --changeset table-columns-function:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
create or replace function columnsNames( tableName text ) create or replace function basis.tableColumnNames( tableName text )
returns text returns text
stable stable
language 'plpgsql' as $$ language 'plpgsql' as $$

View File

@ -6,7 +6,7 @@
/* /*
Like `RAISE EXCEPTION` ... just as an expression instead of a statement. Like `RAISE EXCEPTION` ... just as an expression instead of a statement.
*/ */
create or replace function raiseException(msg text) create or replace function basis.raiseException(msg text)
returns varchar returns varchar
language plpgsql as $$ language plpgsql as $$
begin begin
@ -21,7 +21,7 @@ end; $$;
/* /*
Like `ASSERT` but as an expression instead of a statement. Like `ASSERT` but as an expression instead of a statement.
*/ */
create or replace function assertTrue(expectedTrue boolean, msg text) create or replace function basis.assertTrue(expectedTrue boolean, msg text)
returns boolean returns boolean
language plpgsql as $$ language plpgsql as $$
begin begin

View File

@ -97,7 +97,7 @@ begin
into tx_journal into tx_journal
values (curTxId, values (curTxId,
tg_table_name, old.uuid, tg_op::operation, tg_table_name, old.uuid, tg_op::operation,
jsonb_changes_delta(to_jsonb(old), to_jsonb(new))); basis.jsonb_changes_delta(to_jsonb(old), to_jsonb(new)));
when 'DELETE' then insert when 'DELETE' then insert
into tx_journal into tx_journal
values (curTxId, values (curTxId,

View File

@ -168,7 +168,7 @@ declare
begin begin
targetTable := lower(targetTable); targetTable := lower(targetTable);
if columnNames = '*' then if columnNames = '*' then
columnNames := columnsNames(targetTable); columnNames := basis.tableColumnNames(targetTable);
end if; end if;
/* /*
@ -190,14 +190,14 @@ begin
select distinct g.descendantuuid, select distinct g.descendantuuid,
g.ascendantuuid, g.ascendantuuid,
grants.level + 1 as level, grants.level + 1 as level,
assertTrue(grants.level < 22, 'too many grant-levels: ' || grants.level) basis.assertTrue(grants.level < 22, 'too many grant-levels: ' || grants.level)
from rbacgrants g from rbacgrants g
join recursive_grants grants on grants.descendantuuid = g.ascendantuuid join recursive_grants grants on grants.descendantuuid = g.ascendantuuid
where g.assumed), where g.assumed),
grant_count AS ( grant_count AS (
SELECT COUNT(*) AS grant_count FROM recursive_grants SELECT COUNT(*) AS grant_count FROM recursive_grants
), ),
count_check as (select assertTrue((select count(*) as grant_count from recursive_grants) < 400000, count_check as (select basis.assertTrue((select count(*) as grant_count from recursive_grants) < 400000,
'too many grants for current subjects: ' || (select count(*) as grant_count from recursive_grants)) 'too many grants for current subjects: ' || (select count(*) as grant_count from recursive_grants))
as valid) as valid)
select distinct perm.objectuuid select distinct perm.objectuuid

View File

@ -91,7 +91,7 @@ begin
when 'IPV4_NUMBER' then null when 'IPV4_NUMBER' then null
when 'IPV6_NUMBER' then null when 'IPV6_NUMBER' then null
else raiseException(format('[400] unknown asset type %s', NEW.type::text)) else basis.raiseException(format('[400] unknown asset type %s', NEW.type::text))
end); end);
if expectedParentType is not null and actualParentType is null then if expectedParentType is not null and actualParentType is null then