Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version to auditable_index #427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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