Files
threshold/internal/db/schema/migrations/oss/postgres/0/30_keys.up.sql
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

27 lines
887 B
PL/PgSQL
Raw Normal View History

2020-08-19 16:47:18 -04:00
-- Copyright (c) HashiCorp, Inc.
2023-02-02 17:15:17 -08:00
-- SPDX-License-Identifier: BUSL-1.1
2020-08-19 16:47:18 -04:00
begin;
-- kms_version_column() will increment the version column whenever row data
-- is inserted and should only be used in an before insert trigger. This
-- function will overwrite any explicit values to the version column.
2022-08-08 18:17:57 -04:00
create or replace function kms_version_column() returns trigger
2020-08-19 16:47:18 -04:00
as $$
declare
_key_id text;
_max bigint;
begin
execute format('SELECT $1.%I', tg_argv[0]) into _key_id using new;
execute format('select max(version) + 1 from %I where %I = $1', tg_relid::regclass, tg_argv[0]) using _key_id into _max;
if _max is null then
_max = 1;
end if;
new.version = _max;
return new;
end;
$$ language plpgsql;
2022-08-08 18:17:57 -04:00
comment on function kms_version_column() is
2020-08-19 16:47:18 -04:00
'function used in before insert triggers to properly set version columns for kms_* tables with a version column';
2022-08-08 18:17:57 -04:00
commit;