-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix races that lead to duplicate votes creation
- Loading branch information
Showing
5 changed files
with
68 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Change log | ||
|
||
## master (unreleased) | ||
|
||
### Bug Fixes | ||
|
||
* [#127](https://github.com/ryanto/acts_as_votable/issues/127): Fix races that lead to duplicate votes creation. ([@fatkodima][]) | ||
|
||
For this to work, you need to update your database schema and existing data. | ||
```ruby | ||
# 1. Add a new column to the `votes` table. | ||
add_column :votes, :uniqueness_token, :string | ||
|
||
# 2. Manually remove erroneously created duplicate votes. | ||
|
||
# 3. If you are using `duplicate: true` while voting in your codebase, | ||
# update all those duplicated votes by setting a unique `uniqueness_token` for each of them, | ||
# something like a unique hex value would suffice. | ||
|
||
# 4. For other non duplicated votes update all of them setting the | ||
# `uniqueness_token` field to "unique vote" string value. | ||
|
||
# 5. Update `uniqueness_token` column to disallow NULL values. | ||
change_column_null :votes, :uniqueness_token, false | ||
|
||
# 6. Add a new unique index | ||
add_index :votes, [:voter_id, :voter_type, :vote_scope, :votable_id, :votable_type, :uniqueness_token], | ||
name: "index_votes_uniqueness", unique: true | ||
|
||
# 7. Now you can delete the old index | ||
remove_index :votes, [:voter_id, :voter_type, :vote_scope] | ||
``` | ||
|
||
NOTE: Make sure you adapted that steps for your workload and run that commands safely | ||
(adding/removing indexes concurrently, updating columns in batches, etc). | ||
|
||
[@fatkodima]: https://github.com/fatkodima |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 10 additions & 12 deletions
22
lib/generators/acts_as_votable/migration/templates/active_record/migration.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
class ActsAsVotableMigration < ActiveRecord::Migration<%= migration_version %> | ||
def self.up | ||
def change | ||
create_table :votes do |t| | ||
t.references :votable, null: false, polymorphic: true, index: false | ||
t.references :voter, null: false, polymorphic: true, index: false | ||
|
||
t.references :votable, :polymorphic => true | ||
t.references :voter, :polymorphic => true | ||
|
||
t.boolean :vote_flag | ||
t.boolean :vote_flag, null: false, default: true | ||
t.string :vote_scope | ||
t.integer :vote_weight | ||
t.integer :vote_weight, null: false, default: 1 | ||
t.string :uniqueness_token, null: false # used for 'duplicate' feature | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :votes, [:voter_id, :voter_type, :vote_scope] | ||
add_index :votes, [:votable_id, :votable_type, :vote_scope] | ||
end | ||
t.index [:voter_id, :voter_type, :vote_scope, :votable_id, :votable_type, :uniqueness_token], | ||
name: "index_votes_uniqueness", unique: true | ||
|
||
def self.down | ||
drop_table :votes | ||
t.index [:votable_id, :votable_type, :vote_scope] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters