scinote-web/db/migrate/20190227125306_add_unique_index_on_user_teams.rb
Miha Mencin 51b3aea49a added uniquness validator and index on user_projects and user_teams relations
finxing typo, fixied failing test

fixing mistakenly commited file

fixing schema to include bigint migration
2019-02-27 15:09:47 +01:00

16 lines
461 B
Ruby

# frozen_string_literal: true
class AddUniqueIndexOnUserTeams < ActiveRecord::Migration[5.1]
def up
# firstly delete the duplicates
execute 'WITH uniq AS
(SELECT DISTINCT ON (user_id, team_id) * FROM user_teams)
DELETE FROM user_teams WHERE user_teams.id NOT IN
(SELECT id FROM uniq)'
add_index :user_teams, %i(user_id team_id), unique: true
end
def down
remove_index :user_teams, column: %i(user_id team_id)
end
end