From e9b90d9ce16b40a3173ff1feef6e2a334c35963f Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Thu, 14 Dec 2023 17:49:27 -0800 Subject: [PATCH] internal/db: add schema changes for host catalog pagination --- .../oss/postgres/44/02_hosts.up.sql | 1 + .../12_host_catalog_base_table_updates.up.sql | 71 +++++++++++++++++++ .../sqltest/tests/pagination/host_catalog.sql | 32 +++++++++ 3 files changed, 104 insertions(+) create mode 100644 internal/db/schema/migrations/oss/postgres/80/12_host_catalog_base_table_updates.up.sql create mode 100644 internal/db/sqltest/tests/pagination/host_catalog.sql diff --git a/internal/db/schema/migrations/oss/postgres/44/02_hosts.up.sql b/internal/db/schema/migrations/oss/postgres/44/02_hosts.up.sql index 2b541a5abe..c9d8e821cb 100644 --- a/internal/db/schema/migrations/oss/postgres/44/02_hosts.up.sql +++ b/internal/db/schema/migrations/oss/postgres/44/02_hosts.up.sql @@ -17,6 +17,7 @@ begin; -- insert_host_catalog_subtype() is a before insert trigger function for -- subtypes of host_catalog. -- Replaces the insert_host_catalog_subtype function defined in 20/04_host.up.sql + -- Replaced in 80/10_host_catalog_base_table_updates.up.sql create or replace function insert_host_catalog_subtype() returns trigger as $$ begin diff --git a/internal/db/schema/migrations/oss/postgres/80/12_host_catalog_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/12_host_catalog_base_table_updates.up.sql new file mode 100644 index 0000000000..ad8323f85c --- /dev/null +++ b/internal/db/schema/migrations/oss/postgres/80/12_host_catalog_base_table_updates.up.sql @@ -0,0 +1,71 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: BUSL-1.1 + +begin; + + -- Add create_time and update_time to host_catalog table. + alter table host_catalog add column create_time wt_timestamp; + alter table host_catalog add column update_time wt_timestamp; + + -- Update rows with current values from the subtype host catalog tables. + with sub_host_catalog as ( + select public_id, + create_time, + update_time + from static_host_catalog + union + select public_id, + create_time, + update_time + from host_plugin_catalog + ) + update host_catalog + set create_time = sub_host_catalog.create_time, + update_time = sub_host_catalog.update_time + from sub_host_catalog + where host_catalog.public_id = sub_host_catalog.public_id; + + alter table host_catalog + alter column create_time set not null, + alter column update_time set not null; + + -- Replace the insert trigger to also set the create_time + -- Replaces the insert_host_catalog_subtype function defined in 44/02_hosts.up.sql + create or replace function insert_host_catalog_subtype() returns trigger + as $$ + begin + insert into host_catalog + (public_id, project_id, name, create_time) + values + (new.public_id, new.project_id, new.name, new.create_time); + return new; + end; + $$ language plpgsql; + + -- Add trigger to update the new update_time column on every subtype update. + create function update_host_catalog_table_update_time() returns trigger + as $$ + begin + update host_catalog set update_time = now() where public_id = new.public_id; + return new; + end; + $$ language plpgsql; + comment on function update_host_catalog_table_update_time() is + 'update_host_catalog_table_update_time is used to automatically update the update_time ' + 'of the base table whenever one of the subtype tables are updated'; + + -- Add triggers to subtype tables + create trigger update_host_catalog_table_update_time before update on static_host_catalog + for each row execute procedure update_host_catalog_table_update_time(); + create trigger update_host_catalog_table_update_time before update on host_plugin_catalog + for each row execute procedure update_host_catalog_table_update_time(); + + -- Add new indexes for the create and update time queries. + create index host_catalog_create_time_public_id_idx + on host_catalog (create_time desc, public_id desc); + create index host_catalog_update_time_public_id_idx + on host_catalog (update_time desc, public_id desc); + + analyze host_catalog; + +commit; diff --git a/internal/db/sqltest/tests/pagination/host_catalog.sql b/internal/db/sqltest/tests/pagination/host_catalog.sql new file mode 100644 index 0000000000..0551c48e5d --- /dev/null +++ b/internal/db/sqltest/tests/pagination/host_catalog.sql @@ -0,0 +1,32 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: BUSL-1.1 + +begin; + select plan(9); + + -- Verify the trigger functions exist and are declared properly + select has_function('update_host_catalog_table_update_time'); + select volatility_is('update_host_catalog_table_update_time', 'volatile'); + select isnt_strict('update_host_catalog_table_update_time'); + select has_trigger('static_host_catalog', 'update_host_catalog_table_update_time'); + select has_trigger('host_plugin_catalog', 'update_host_catalog_table_update_time'); + select has_index('host_catalog', 'host_catalog_create_time_public_id_idx', array['create_time', 'public_id']); + select has_index('host_catalog', 'host_catalog_update_time_public_id_idx', array['update_time', 'public_id']); + + -- To test that the trigger that sets the create_time of the base table + -- when an insert into a subtype table happens works, we check that the + -- create time of the entries in both tables match + prepare host_catalog_create_time as select create_time from host_catalog where public_id='hc__st_____b'; + prepare static_host_catalog_create_time as select create_time from static_host_catalog where public_id='hc__st_____b'; + select results_eq('host_catalog_create_time','static_host_catalog_create_time'); + -- To test the trigger that updates the update_time of the base table, + -- we update one of the existing catalog and check that the base table + -- entry has the same update_time as the subtype one. + update static_host_catalog set name='blue black static host catalog' where public_id='hc__st_____b'; + prepare host_catalog_update_time as select update_time from host_catalog where public_id='hc__st_____b'; + prepare static_host_catalog_update_time as select update_time from static_host_catalog where public_id='hc__st_____b'; + select results_eq('host_catalog_update_time','static_host_catalog_update_time'); + + select * from finish(); + +rollback;