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.
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 null on null input
language 'plpgsql' as $$

View File

@ -9,7 +9,7 @@
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
called on null input
language plpgsql as $$
@ -31,7 +31,7 @@ begin
if jsonb_typeof(newJson -> (oldJsonElement.key)) = 'object' then
diffJson = diffJson ||
jsonb_build_object(oldJsonElement.key,
jsonb_changes_delta(oldJsonElement.value, newJson -> (oldJsonElement.key)));
basis.jsonb_changes_delta(oldJsonElement.value, newJson -> (oldJsonElement.key)));
end if;
else
diffJson = diffJson || jsonb_build_object(oldJsonElement.key, null);
@ -49,30 +49,30 @@ do language plpgsql $$
actual text;
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
raise exception 'jsonb_diff #1 failed:% expected: %,% actually: %', E'\n', expected, E'\n', actual;
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;
if actual <> expected then
raise exception 'jsonb_diff #2 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual;
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;
if actual <> expected then
raise exception 'jsonb_diff #3 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual;
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;
if actual <> expected then
raise exception 'jsonb_diff #4 failed:% expected: %,% actual: %', E'\n', expected, E'\n', actual;
end if;
select jsonb_changes_delta(
select basis.jsonb_changes_delta(
$json${
"a": "same",
"b": "old",

View File

@ -6,7 +6,7 @@
--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;
$$ language sql;
--//

View File

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

View File

@ -6,7 +6,7 @@
/*
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
language plpgsql as $$
begin
@ -21,7 +21,7 @@ end; $$;
/*
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
language plpgsql as $$
begin

View File

@ -97,7 +97,7 @@ begin
into tx_journal
values (curTxId,
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
into tx_journal
values (curTxId,

View File

@ -168,7 +168,7 @@ declare
begin
targetTable := lower(targetTable);
if columnNames = '*' then
columnNames := columnsNames(targetTable);
columnNames := basis.tableColumnNames(targetTable);
end if;
/*
@ -190,14 +190,14 @@ begin
select distinct g.descendantuuid,
g.ascendantuuid,
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
join recursive_grants grants on grants.descendantuuid = g.ascendantuuid
where g.assumed),
grant_count AS (
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))
as valid)
select distinct perm.objectuuid

View File

@ -91,7 +91,7 @@ begin
when 'IPV4_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);
if expectedParentType is not null and actualParentType is null then