From dc727627a645472a82d7678efb335bdd4d481167 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 29 Mar 2018 21:21:57 +0300 Subject: [PATCH] Add version to auditable_index --- CHANGELOG.md | 3 ++- lib/audited/audit.rb | 2 +- .../add_version_to_auditable_index.rb | 21 +++++++++++++++++++ lib/generators/audited/templates/install.rb | 2 +- lib/generators/audited/upgrade_generator.rb | 4 ++++ test/db/version_6.rb | 2 ++ test/upgrade_generator_test.rb | 10 +++++++++ 7 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 lib/generators/audited/templates/add_version_to_auditable_index.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f885fe61..f8a4a5bf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,8 @@ Added Changed -- None +- Add version to auditable_index + [#427](https://github.com/collectiveidea/audited/pull/427) Fixed diff --git a/lib/audited/audit.rb b/lib/audited/audit.rb index 4d96d02c1..ff82e4e35 100644 --- a/lib/audited/audit.rb +++ b/lib/audited/audit.rb @@ -171,7 +171,7 @@ def self.collection_cache_key(collection = all, timestamp_column = :created_at) private def set_version_number - max = self.class.auditable_finder(auditable_id, auditable_type).descending.first.try(:version) || 0 + max = self.class.auditable_finder(auditable_id, auditable_type).maximum(:version) || 0 self.version = max + 1 end diff --git a/lib/generators/audited/templates/add_version_to_auditable_index.rb b/lib/generators/audited/templates/add_version_to_auditable_index.rb new file mode 100644 index 000000000..79a4045d6 --- /dev/null +++ b/lib/generators/audited/templates/add_version_to_auditable_index.rb @@ -0,0 +1,21 @@ +class <%= migration_class_name %> < <%= migration_parent %> + def self.up + if index_exists?(:audits, [:auditable_type, :auditable_id], name: index_name) + remove_index :audits, name: index_name + add_index :audits, [:auditable_type, :auditable_id, :version], name: index_name + end + end + + def self.down + if index_exists?(:audits, [:auditable_type, :auditable_id, :version], name: index_name) + remove_index :audits, name: index_name + add_index :audits, [:auditable_type, :auditable_id], name: index_name + end + end + + private + + def index_name + 'auditable_index' + end +end diff --git a/lib/generators/audited/templates/install.rb b/lib/generators/audited/templates/install.rb index 77bf46aa3..1d43f093c 100644 --- a/lib/generators/audited/templates/install.rb +++ b/lib/generators/audited/templates/install.rb @@ -17,7 +17,7 @@ def self.up t.column :created_at, :datetime end - add_index :audits, [:auditable_type, :auditable_id], :name => 'auditable_index' + add_index :audits, [:auditable_type, :auditable_id, :version], :name => 'auditable_index' add_index :audits, [:associated_type, :associated_id], :name => 'associated_index' add_index :audits, [:user_id, :user_type], :name => 'user_index' add_index :audits, :request_uuid diff --git a/lib/generators/audited/upgrade_generator.rb b/lib/generators/audited/upgrade_generator.rb index 91c87444c..d2b019704 100644 --- a/lib/generators/audited/upgrade_generator.rb +++ b/lib/generators/audited/upgrade_generator.rb @@ -58,6 +58,10 @@ def migrations_to_be_applied if indexes.any? { |i| i.columns == %w[associated_id associated_type] } yield :revert_polymorphic_indexes_order end + + if indexes.any? { |i| i.columns == %w[auditable_type auditable_id] } + yield :add_version_to_auditable_index + end end end end diff --git a/test/db/version_6.rb b/test/db/version_6.rb index 776569606..281ea9227 100644 --- a/test/db/version_6.rb +++ b/test/db/version_6.rb @@ -14,4 +14,6 @@ t.column :associated_id, :integer t.column :associated_type, :string end + + add_index :audits, [:auditable_type, :auditable_id], name: 'auditable_index' end diff --git a/test/upgrade_generator_test.rb b/test/upgrade_generator_test.rb index 53ada067c..376710804 100644 --- a/test/upgrade_generator_test.rb +++ b/test/upgrade_generator_test.rb @@ -79,6 +79,16 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase end end + test "should add 'version' to auditable_index" do + load_schema 6 + + run_generator %w(upgrade) + + assert_migration "db/migrate/add_version_to_auditable_index.rb" do |content| + assert_match(/add_index :audits, \[:auditable_type, :auditable_id, :version\]/, content) + end + end + test "generate migration with correct AR migration parent" do load_schema 1