diff --git a/internal/db/schema/migrations/oss/postgres/91/04_insert_session_delete_on_statement.up.sql b/internal/db/schema/migrations/oss/postgres/91/04_insert_session_delete_on_statement.up.sql new file mode 100644 index 0000000000..99e8eebad2 --- /dev/null +++ b/internal/db/schema/migrations/oss/postgres/91/04_insert_session_delete_on_statement.up.sql @@ -0,0 +1,25 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: BUSL-1.1 + +begin; + create function bulk_insert_deleted_ids() returns trigger + as $$ + begin + execute format('insert into %I (public_id, delete_time) + select o.public_id, now() + from old_table o;', + tg_argv[0]); + return null; + end; + $$ language plpgsql; + comment on function bulk_insert_deleted_ids is + 'bulk_insert_deleted_ids is a function that inserts records into the table ' + 'specified by the first trigger argument. It takes the public IDs from the ' + 'set of rows that where deleted and the current timestamp.'; + + drop trigger insert_deleted_id on session; + create trigger bulk_insert_deleted_ids + after delete on session + referencing old table as old_table + for each statement execute function bulk_insert_deleted_ids('session_deleted'); +commit;