summaryrefslogtreecommitdiff
path: root/db/post_migrate
diff options
context:
space:
mode:
authorRubén Dávila <ruben@gitlab.com>2017-10-05 08:14:34 -0500
committerRubén Dávila <ruben@gitlab.com>2017-10-05 08:26:24 -0500
commit6e0a4fc10e745f8d8be65f8ee420c75d36057c16 (patch)
tree14aebfd4b8635dc18ea8f0984c9ac770001a949b /db/post_migrate
parentdd139e65b53f30eae2d8bb50dff180e8eab11fe4 (diff)
downloadgitlab-ce-6e0a4fc10e745f8d8be65f8ee420c75d36057c16.tar.gz
Convert migrations to generate subkeys to a background migration
Diffstat (limited to 'db/post_migrate')
-rw-r--r--db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb61
-rw-r--r--db/post_migrate/20171005130944_schedule_create_gpg_key_subkeys_from_gpg_keys.rb25
2 files changed, 25 insertions, 61 deletions
diff --git a/db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb b/db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb
deleted file mode 100644
index 04c81ea684f..00000000000
--- a/db/post_migrate/20171002161539_create_gpg_key_subkeys_for_existing_gpg_keys.rb
+++ /dev/null
@@ -1,61 +0,0 @@
-# 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 GpgKey < ActiveRecord::Base
- self.table_name = 'gpg_keys'
-
- include EachBatch
- include ShaAttribute
-
- sha_attribute :primary_keyid
- sha_attribute :fingerprint
-
- has_many :subkeys, class_name: 'GpgKeySubkey'
- end
-
- class GpgKeySubkey < ActiveRecord::Base
- self.table_name = 'gpg_key_subkeys'
-
- include ShaAttribute
-
- sha_attribute :keyid
- sha_attribute :fingerprint
- end
-
- def up
- GpgKey.with_subkeys.each_batch do |batch|
- batch.each do |gpg_key|
- next if gpg_key.subkeys.any?
-
- create_subkeys(gpg_key) && update_signatures(gpg_key)
- end
- end
- end
-
- def down
- end
-
- private
-
- def create_subkeys(gpg_key)
- gpg_subkeys = Gitlab::Gpg.subkeys_from_key(gpg_key.key)
-
- gpg_subkeys[gpg_key.primary_keyid.upcase]&.each do |subkey_data|
- gpg_key.subkeys.build(keyid: subkey_data[:keyid], fingerprint: subkey_data[:fingerprint])
- end
-
- # Improve latency by doing all INSERTs in a single call
- GpgKey.transaction do
- gpg_key.save!
- end
- end
-
- def update_signatures(gpg_key)
- InvalidGpgSignatureUpdateWorker.perform_async(gpg_key.id)
- end
-end
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