Merge pull request #7525 from artoscinote/ma_SCI_10669

Add unique constraint to viewable states [SCI-10669]
This commit is contained in:
Martin Artnik 2024-05-06 10:46:14 +02:00 committed by GitHub
commit 220160b075
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View file

@ -0,0 +1,18 @@
# 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

View file

@ -1294,6 +1294,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_01_18_094253) do
t.bigint "viewable_id"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["user_id", "viewable_id", "viewable_type"], name: "index_view_states_on_user_id_and_viewable_id_and_viewable_type", unique: true
t.index ["user_id"], name: "index_view_states_on_user_id"
t.index ["viewable_type", "viewable_id"], name: "index_view_states_on_viewable_type_and_viewable_id"
end