From 8450fe3074140b86e7d2e5a85d2cdb65051906ee Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Fri, 3 Jun 2016 15:49:34 -0300 Subject: Add index to notification settings --- CHANGELOG | 1 + .../20160603180330_remove_duplicated_notification_settings.rb | 7 +++++++ db/migrate/20160603182247_add_index_to_notification_settings.rb | 9 +++++++++ lib/gitlab/database/migration_helpers.rb | 6 +++++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20160603180330_remove_duplicated_notification_settings.rb create mode 100644 db/migrate/20160603182247_add_index_to_notification_settings.rb diff --git a/CHANGELOG b/CHANGELOG index 7215a919d79..bcb7e290ede 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -34,6 +34,7 @@ v 8.9.0 (unreleased) - Cache project build count in sidebar nav - Reduce number of queries needed to render issue labels in the sidebar - Improve error handling importing projects + - Remove duplicated notification settings - Put project Files and Commits tabs under Code tab v 8.8.4 diff --git a/db/migrate/20160603180330_remove_duplicated_notification_settings.rb b/db/migrate/20160603180330_remove_duplicated_notification_settings.rb new file mode 100644 index 00000000000..c2fcac4c53d --- /dev/null +++ b/db/migrate/20160603180330_remove_duplicated_notification_settings.rb @@ -0,0 +1,7 @@ +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 diff --git a/db/migrate/20160603182247_add_index_to_notification_settings.rb b/db/migrate/20160603182247_add_index_to_notification_settings.rb new file mode 100644 index 00000000000..06462042b09 --- /dev/null +++ b/db/migrate/20160603182247_add_index_to_notification_settings.rb @@ -0,0 +1,9 @@ +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 diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb index fd14234c558..b88e50748f6 100644 --- a/lib/gitlab/database/migration_helpers.rb +++ b/lib/gitlab/database/migration_helpers.rb @@ -19,7 +19,11 @@ module Gitlab end if Database.postgresql? - args << { algorithm: :concurrently } + if args[2].present? + args[2].merge!({ algorithm: :concurrently }) + else + args << { algorithm: :concurrently } + end end add_index(*args) -- cgit v1.2.1 From 9264203103bbd2b4f46ce777304f210b07765c43 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Mon, 6 Jun 2016 11:30:17 -0300 Subject: change add_concurrent_index function arguments --- lib/gitlab/database/migration_helpers.rb | 10 +++------- spec/lib/gitlab/database/migration_helpers_spec.rb | 13 ++++++++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb index b88e50748f6..978c3f7896d 100644 --- a/lib/gitlab/database/migration_helpers.rb +++ b/lib/gitlab/database/migration_helpers.rb @@ -11,7 +11,7 @@ module Gitlab # add_concurrent_index :users, :some_column # # 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? raise 'add_concurrent_index can not be run inside a transaction, ' \ 'you can disable transactions by calling disable_ddl_transaction! ' \ @@ -19,14 +19,10 @@ module Gitlab end if Database.postgresql? - if args[2].present? - args[2].merge!({ algorithm: :concurrently }) - else - args << { algorithm: :concurrently } - end + options = options.merge({ algorithm: :concurrently }) end - add_index(*args) + add_index(table_name, column_name, options) end # Updates the value of a column in batches. diff --git a/spec/lib/gitlab/database/migration_helpers_spec.rb b/spec/lib/gitlab/database/migration_helpers_spec.rb index 35ade7a2be0..83ddabe6b0b 100644 --- a/spec/lib/gitlab/database/migration_helpers_spec.rb +++ b/spec/lib/gitlab/database/migration_helpers_spec.rb @@ -16,14 +16,21 @@ describe Gitlab::Database::MigrationHelpers, lib: true do end context 'using PostgreSQL' do - it 'creates the index concurrently' do - expect(Gitlab::Database).to receive(:postgresql?).and_return(true) + before { expect(Gitlab::Database).to receive(:postgresql?).and_return(true) } + it 'creates the index concurrently' do expect(model).to receive(:add_index). with(:users, :foo, algorithm: :concurrently) model.add_concurrent_index(:users, :foo) 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 context 'using MySQL' do @@ -31,7 +38,7 @@ describe Gitlab::Database::MigrationHelpers, lib: true do expect(Gitlab::Database).to receive(:postgresql?).and_return(false) expect(model).to receive(:add_index). - with(:users, :foo) + with(:users, :foo, {}) model.add_concurrent_index(:users, :foo) end -- cgit v1.2.1