mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 12:42:46 +08:00
FIX: add a unique index to developers table (#8436)
This commit is contained in:
parent
11a73b3f15
commit
6005fadaca
24
db/migrate/20191128222140_add_unique_index_to_developers.rb
Normal file
24
db/migrate/20191128222140_add_unique_index_to_developers.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddUniqueIndexToDevelopers < ActiveRecord::Migration[6.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
DELETE FROM developers d1
|
||||
USING (
|
||||
SELECT MAX(id) as id, user_id
|
||||
FROM developers
|
||||
GROUP BY user_id
|
||||
HAVING COUNT(*) > 1
|
||||
) d2
|
||||
WHERE
|
||||
d1.user_id = d2.user_id AND
|
||||
d1.id <> d2.id
|
||||
SQL
|
||||
|
||||
add_index :developers, %i(user_id), unique: true
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user