Chatwoot/db/migrate/20211221125545_add_unique_index_on_inbox_members.rb
Sojan Jose 009abc1948
chore: API improvements (#3637)
- Unique validations for Inbox members and Team member objects
- Move notification processing to Async
2021-12-21 22:48:01 +05:30

19 lines
717 B
Ruby

# ref: https://dev.to/nodefiend/rails-migration-adding-a-unique-index-and-deleting-duplicates-5cde
class AddUniqueIndexOnInboxMembers < ActiveRecord::Migration[6.1]
def up
# partioning the duplicate records and then removing where more than one row is found
ActiveRecord::Base.connection.execute('
DELETE FROM inbox_members WHERE id IN (SELECT id from (
SELECT id, user_id, inbox_id, ROW_NUMBER() OVER w AS rnum FROM inbox_members WINDOW w AS (
PARTITION BY inbox_id, user_id ORDER BY id
)
) t WHERE t.rnum > 1)
')
add_index :inbox_members, [:inbox_id, :user_id], unique: true
end
def down
remove_index :inbox_members, [:inbox_id, :user_id]
end
end