From 7ebe393aee0a1c1fdd3e1cdb63d63cc4b1f9e191 Mon Sep 17 00:00:00 2001 From: Michael Gaffney Date: Thu, 4 May 2023 14:10:42 -0400 Subject: [PATCH] feat(sql): Add history table for target_ssh This adds a history table for `target_ssh`. This also fixes a bug in the history table test suite. Prior to this change, the operational table name was calculated from the history table name using the SQL trim() function. The SQL trim function as used returned 'targe' for 'target_ssh_hst' instead of the expected value 'target_ssh'. --- .../oss/postgres/70/09_target_history.up.sql | 50 +++++++++++++++++++ .../tests/history/history_table_tests.sql | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 internal/db/schema/migrations/oss/postgres/70/09_target_history.up.sql diff --git a/internal/db/schema/migrations/oss/postgres/70/09_target_history.up.sql b/internal/db/schema/migrations/oss/postgres/70/09_target_history.up.sql new file mode 100644 index 0000000000..a7fd03e4e1 --- /dev/null +++ b/internal/db/schema/migrations/oss/postgres/70/09_target_history.up.sql @@ -0,0 +1,50 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: MPL-2.0 + +begin; + + create table target_ssh_hst ( + public_id wt_public_id not null, + project_id wt_scope_id not null, + name text not null, + description text null, + default_port integer null, + session_max_seconds integer not null, + session_connection_limit integer not null, + worker_filter wt_bexprfilter null, + egress_worker_filter wt_bexprfilter null, + ingress_worker_filter wt_bexprfilter null, + default_client_port integer null, + enable_session_recording boolean not null, + storage_bucket_id wt_public_id null, + history_id wt_url_safe_id default wt_url_safe_id() primary key, + valid_range tstzrange not null default tstzrange(current_timestamp, null), + constraint target_ssh_hst_valid_range_excl + exclude using gist (public_id with =, valid_range with &&) + ); + comment on table target_ssh_hst is + 'target_ssh_hst is a history table where each row contains the values from a row ' + 'in the target_ssh table during the time range in the valid_range column.'; + + create trigger hst_on_insert after insert on target_ssh + for each row execute function hst_on_insert(); + create trigger hst_on_update after update on target_ssh + for each row execute function hst_on_update(); + create trigger hst_on_delete after delete on target_ssh + for each row execute function hst_on_delete(); + + insert into target_ssh_hst ( + public_id, + project_id, name, description, + default_port, session_max_seconds, session_connection_limit, + worker_filter, egress_worker_filter, ingress_worker_filter, + default_client_port, enable_session_recording, storage_bucket_id + ) + select public_id, + project_id, name, description, + default_port, session_max_seconds, session_connection_limit, + worker_filter, egress_worker_filter, ingress_worker_filter, + default_client_port, enable_session_recording, storage_bucket_id + from target_ssh; + +commit; diff --git a/internal/db/sqltest/tests/history/history_table_tests.sql b/internal/db/sqltest/tests/history/history_table_tests.sql index 256b6fc101..6dcbd57b04 100644 --- a/internal/db/sqltest/tests/history/history_table_tests.sql +++ b/internal/db/sqltest/tests/history/history_table_tests.sql @@ -14,7 +14,7 @@ begin; create function op_table(history_table_name name) returns text as $$ - select trim(trailing '_hst' from history_table_name); + select left(history_table_name, -4); $$ language sql; create function has_operational_table(history_table_name name) returns text