diff --git a/db/post_migrate/20171005130944_schedule_create_gpg_key_subkeys_from_gpg_keys.rb b/db/post_migrate/20171005130944_schedule_create_gpg_key_subkeys_from_gpg_keys.rb new file mode 100644 index 00000000000..c8bbfbccc08 --- /dev/null +++ b/db/post_migrate/20171005130944_schedule_create_gpg_key_subkeys_from_gpg_keys.rb @@ -0,0 +1,25 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class ScheduleCreateGpgKeySubkeysFromGpgKeys < ActiveRecord::Migration + disable_ddl_transaction! + + DOWNTIME = false + + class GpgKey < ActiveRecord::Base + self.table_name = 'gpg_keys' + end + + def up + GpgKey.select(:id).in_batches do |relation| + jobs = relation.pluck(:id).map do |id| + ['CreateGpgKeySubkeysFromGpgKeys', [id]] + end + + BackgroundMigrationWorker.perform_bulk(jobs) + end + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index 3bcfbcc3fd1..3a69e3d3056 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171004121444) do +ActiveRecord::Schema.define(version: 20171005130944) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb b/lib/gitlab/background_migration/create_gpg_key_subkeys_from_gpg_keys.rb similarity index 66% rename from db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb rename to lib/gitlab/background_migration/create_gpg_key_subkeys_from_gpg_keys.rb index 04c81ea684f..3a32134b991 100644 --- a/db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb +++ b/lib/gitlab/background_migration/create_gpg_key_subkeys_from_gpg_keys.rb @@ -1,11 +1,4 @@ -# See http://doc.gitlab.com/ce/development/migration_style_guide.html -# for more information on how to write migrations for GitLab. - -class CreateGpgKeySubkeysForExistingGpgKeys < ActiveRecord::Migration - disable_ddl_transaction! - - DOWNTIME = false - +class Gitlab::BackgroundMigration::CreateGpgKeySubkeysFromGpgKeys class GpgKey < ActiveRecord::Base self.table_name = 'gpg_keys' @@ -27,17 +20,13 @@ class CreateGpgKeySubkeysForExistingGpgKeys < ActiveRecord::Migration sha_attribute :fingerprint end - def up - GpgKey.with_subkeys.each_batch do |batch| - batch.each do |gpg_key| - next if gpg_key.subkeys.any? + def perform(gpg_key_id) + gpg_key = GpgKey.find_by(id: gpg_key_id) - create_subkeys(gpg_key) && update_signatures(gpg_key) - end - end - end + return if gpg_key.nil? + return if gpg_key.subkeys.any? - def down + create_subkeys(gpg_key) && update_signatures(gpg_key) end private