From 3c57fa737891674980884596f69ce7452b8d3822 Mon Sep 17 00:00:00 2001 From: Josh Adam Date: Tue, 7 Jan 2025 14:18:21 -0600 Subject: [PATCH] feat(metadata): Add Logidze support to MetadataTemplates for change tracking --- ...94839_add_logidze_to_metadata_templates.rb | 20 ++++++++++++++++ db/schema.rb | 6 ++++- .../logidze_on_metadata_templates_v01.sql | 6 +++++ test/models/metadata_template_test.rb | 24 ++++++++++++------- 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20250107194839_add_logidze_to_metadata_templates.rb create mode 100644 db/triggers/logidze_on_metadata_templates_v01.sql diff --git a/db/migrate/20250107194839_add_logidze_to_metadata_templates.rb b/db/migrate/20250107194839_add_logidze_to_metadata_templates.rb new file mode 100644 index 0000000000..576c9d8174 --- /dev/null +++ b/db/migrate/20250107194839_add_logidze_to_metadata_templates.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# Migration to add Logidze to MetadataTemplates table +class AddLogidzeToMetadataTemplates < ActiveRecord::Migration[7.2] + def change + add_column :metadata_templates, :log_data, :jsonb + + reversible do |dir| + dir.up do + create_trigger :logidze_on_metadata_templates, on: :metadata_templates + end + + dir.down do + execute <<~SQL.squish + DROP TRIGGER IF EXISTS "logidze_on_metadata_template" on "metadata_templates"; + SQL + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fd60af004f..348653eed3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_01_06_153442) do +ActiveRecord::Schema[7.2].define(version: 2025_01_07_194839) do # These are extensions that must be enabled in order to support this database enable_extension "hstore" enable_extension "plpgsql" @@ -140,6 +140,7 @@ t.datetime "deleted_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.jsonb "log_data" t.index ["created_by_id"], name: "index_metadata_templates_on_created_by_id" t.index ["namespace_id", "name"], name: "index_template_name_with_namespace", unique: true, where: "(deleted_at IS NULL)" t.index ["namespace_id"], name: "index_metadata_templates_on_namespace_id" @@ -760,4 +761,7 @@ create_trigger :logidze_on_automated_workflow_executions, sql_definition: <<-SQL CREATE TRIGGER logidze_on_automated_workflow_executions BEFORE INSERT OR UPDATE ON public.automated_workflow_executions FOR EACH ROW WHEN ((COALESCE(current_setting('logidze.disabled'::text, true), ''::text) <> 'on'::text)) EXECUTE FUNCTION logidze_logger('null', 'updated_at') SQL + create_trigger :logidze_on_metadata_templates, sql_definition: <<-SQL + CREATE TRIGGER logidze_on_metadata_templates BEFORE INSERT OR UPDATE ON public.metadata_templates FOR EACH ROW WHEN ((COALESCE(current_setting('logidze.disabled'::text, true), ''::text) <> 'on'::text)) EXECUTE FUNCTION logidze_logger('null', 'updated_at') + SQL end diff --git a/db/triggers/logidze_on_metadata_templates_v01.sql b/db/triggers/logidze_on_metadata_templates_v01.sql new file mode 100644 index 0000000000..39b8af679e --- /dev/null +++ b/db/triggers/logidze_on_metadata_templates_v01.sql @@ -0,0 +1,6 @@ +CREATE TRIGGER "logidze_on_metadata_templates" +BEFORE UPDATE OR INSERT ON "metadata_templates" FOR EACH ROW +WHEN (coalesce(current_setting('logidze.disabled', true), '') <> 'on') +-- Parameters: history_size_limit (integer), timestamp_column (text), filtered_columns (text[]), +-- include_columns (boolean), debounce_time_ms (integer) +EXECUTE PROCEDURE logidze_logger(null, 'updated_at'); diff --git a/test/models/metadata_template_test.rb b/test/models/metadata_template_test.rb index dfa82200ad..4ceec535c0 100644 --- a/test/models/metadata_template_test.rb +++ b/test/models/metadata_template_test.rb @@ -74,13 +74,19 @@ class MetadataTemplateTest < ActiveSupport::TestCase # end # end - # test 'tracks history changes' do - # original_name = @valid_metadata_template.name - # travel 1.minute do - # assert_difference -> { @valid_metadata_template.reload.log_size } do - # @valid_metadata_template.update!(name: 'Updated Name') - # end - # end - # assert_equal original_name, @valid_metadata_template.reload.log_data.versions.first['changes']['name'] - # end + test 'tracks history changes' do + @valid_metadata_template.create_logidze_snapshot! + + assert_equal 1, @valid_metadata_template.log_data.version + assert_equal 1, @valid_metadata_template.log_data.size + + assert_changes -> { @valid_metadata_template.name }, to: 'Updated Name' do + @valid_metadata_template.update(name: 'Updated Name') + end + + @valid_metadata_template.create_logidze_snapshot! + + assert_equal 2, @valid_metadata_template.log_data.version + assert_equal 2, @valid_metadata_template.log_data.size + end end