mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-07 08:01:51 +08:00
19 lines
539 B
Ruby
19 lines
539 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
class AddUniqueConstraintToViewStates < ActiveRecord::Migration[7.0]
|
||
|
def up
|
||
|
# delete the duplicates
|
||
|
execute 'WITH uniq AS
|
||
|
(SELECT DISTINCT ON (user_id, viewable_id, viewable_type) * FROM view_states)
|
||
|
DELETE FROM view_states WHERE view_states.id NOT IN
|
||
|
(SELECT id FROM uniq)'
|
||
|
|
||
|
# add index
|
||
|
add_index :view_states, %i(user_id viewable_id viewable_type), unique: true
|
||
|
end
|
||
|
|
||
|
def down
|
||
|
remove_index :view_states, columns: %i(user_id viewable_id viewable_type)
|
||
|
end
|
||
|
end
|