Skip to content

Commit

Permalink
Add version to auditable_index
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and tbrisker committed Apr 4, 2018
1 parent 9f6619c commit dc72762
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Added

Changed

- None
- Add version to auditable_index
[#427](https://github.com/collectiveidea/audited/pull/427)

Fixed

Expand Down
2 changes: 1 addition & 1 deletion lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions lib/generators/audited/templates/add_version_to_auditable_index.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/generators/audited/templates/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/audited/upgrade_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/db/version_6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/upgrade_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit dc72762

Please sign in to comment.