Commit 0ba21860 authored by Yorick Peterse's avatar Yorick Peterse

Merge branch 'issue_3359' into 'master'

Remove duplicated notification settings and add unique index

See merge request !4472
parents f73292e4 92642031
...@@ -36,6 +36,7 @@ v 8.9.0 (unreleased) ...@@ -36,6 +36,7 @@ v 8.9.0 (unreleased)
- Cache project build count in sidebar nav - Cache project build count in sidebar nav
- Reduce number of queries needed to render issue labels in the sidebar - Reduce number of queries needed to render issue labels in the sidebar
- Improve error handling importing projects - Improve error handling importing projects
- Remove duplicated notification settings
- Put project Files and Commits tabs under Code tab - Put project Files and Commits tabs under Code tab
- Replace Colorize with Rainbow for coloring console output in Rake tasks. - Replace Colorize with Rainbow for coloring console output in Rake tasks.
- An indicator is now displayed at the top of the comment field for confidential issues. - An indicator is now displayed at the top of the comment field for confidential issues.
......
class RemoveDuplicatedNotificationSettings < ActiveRecord::Migration
def up
execute <<-SQL
DELETE FROM notification_settings WHERE id NOT IN ( SELECT min_id from (SELECT MIN(id) as min_id FROM notification_settings GROUP BY user_id, source_type, source_id) as dups )
SQL
end
end
class AddIndexToNotificationSettings < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
def change
add_concurrent_index :notification_settings, [:user_id, :source_id, :source_type], { unique: true, name: "index_notifications_on_user_id_and_source_id_and_source_type" }
end
end
...@@ -11,7 +11,7 @@ module Gitlab ...@@ -11,7 +11,7 @@ module Gitlab
# add_concurrent_index :users, :some_column # add_concurrent_index :users, :some_column
# #
# See Rails' `add_index` for more info on the available arguments. # See Rails' `add_index` for more info on the available arguments.
def add_concurrent_index(*args) def add_concurrent_index(table_name, column_name, options = {})
if transaction_open? if transaction_open?
raise 'add_concurrent_index can not be run inside a transaction, ' \ raise 'add_concurrent_index can not be run inside a transaction, ' \
'you can disable transactions by calling disable_ddl_transaction! ' \ 'you can disable transactions by calling disable_ddl_transaction! ' \
...@@ -19,10 +19,10 @@ module Gitlab ...@@ -19,10 +19,10 @@ module Gitlab
end end
if Database.postgresql? if Database.postgresql?
args << { algorithm: :concurrently } options = options.merge({ algorithm: :concurrently })
end end
add_index(*args) add_index(table_name, column_name, options)
end end
# Updates the value of a column in batches. # Updates the value of a column in batches.
......
...@@ -16,14 +16,21 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -16,14 +16,21 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
end end
context 'using PostgreSQL' do context 'using PostgreSQL' do
it 'creates the index concurrently' do before { expect(Gitlab::Database).to receive(:postgresql?).and_return(true) }
expect(Gitlab::Database).to receive(:postgresql?).and_return(true)
it 'creates the index concurrently' do
expect(model).to receive(:add_index). expect(model).to receive(:add_index).
with(:users, :foo, algorithm: :concurrently) with(:users, :foo, algorithm: :concurrently)
model.add_concurrent_index(:users, :foo) model.add_concurrent_index(:users, :foo)
end end
it 'creates unique index concurrently' do
expect(model).to receive(:add_index).
with(:users, :foo, { algorithm: :concurrently, unique: true })
model.add_concurrent_index(:users, :foo, unique: true)
end
end end
context 'using MySQL' do context 'using MySQL' do
...@@ -31,7 +38,7 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -31,7 +38,7 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
expect(Gitlab::Database).to receive(:postgresql?).and_return(false) expect(Gitlab::Database).to receive(:postgresql?).and_return(false)
expect(model).to receive(:add_index). expect(model).to receive(:add_index).
with(:users, :foo) with(:users, :foo, {})
model.add_concurrent_index(:users, :foo) model.add_concurrent_index(:users, :foo)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment