Skip to content

Commit

Permalink
feat(metadata): Add Logidze support to MetadataTemplates for change t…
Browse files Browse the repository at this point in the history
…racking
  • Loading branch information
joshsadam committed Jan 8, 2025
1 parent 2218813 commit 3c57fa7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
20 changes: 20 additions & 0 deletions db/migrate/20250107194839_add_logidze_to_metadata_templates.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions db/triggers/logidze_on_metadata_templates_v01.sql
Original file line number Diff line number Diff line change
@@ -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');
24 changes: 15 additions & 9 deletions test/models/metadata_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3c57fa7

Please sign in to comment.