From 7f45dd92eaf70ec624d639f9d0354b6f3c512dc6 Mon Sep 17 00:00:00 2001 From: Alexis Reigel Date: Thu, 31 Aug 2017 15:50:24 +0200 Subject: [PATCH] destroy all signatures and add with default value To avoid having to implement legacy code handling for the obsolete `verified_signature` attribute and to avoid any race conditions during the zero-downtime-deployment we do the following: 1. Destroy all records 2. Migration: Use add_column_with_default to add the new attribute and update the verification_status values on records created between 1. and 2. 3. Deploy the new code 4. Post migration: Destroy all records Like this we make sure that at no point there is a record with a `nil` value for the new `verification_status` attribute. --- ...d_verification_status_to_gpg_signatures.rb | 25 +++++++++++++++++-- db/schema.rb | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/db/migrate/20170817123339_add_verification_status_to_gpg_signatures.rb b/db/migrate/20170817123339_add_verification_status_to_gpg_signatures.rb index d50277aa066..126036a95d8 100644 --- a/db/migrate/20170817123339_add_verification_status_to_gpg_signatures.rb +++ b/db/migrate/20170817123339_add_verification_status_to_gpg_signatures.rb @@ -1,7 +1,28 @@ class AddVerificationStatusToGpgSignatures < ActiveRecord::Migration DOWNTIME = false - def change - add_column :gpg_signatures, :verification_status, :smallint + include Gitlab::Database::MigrationHelpers + disable_ddl_transaction! + + class GpgSignature < ActiveRecord::Base + self.table_name = 'gpg_signatures' + + include EachBatch + end + + def up + # First we remove all signatures because we need to re-verify them all + # again anyway (because of the updated verification logic). + # + # This makes adding the column with default values faster + GpgSignature.each_batch do |relation| + relation.delete_all + end + + add_column_with_default(:gpg_signatures, :verification_status, :smallint, default: 0) + end + + def down + remove_column(:gpg_signatures, :verification_status) end end diff --git a/db/schema.rb b/db/schema.rb index e2c602b000f..8db0d080166 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -614,7 +614,7 @@ ActiveRecord::Schema.define(version: 20170830125940) do t.binary "gpg_key_primary_keyid" t.text "gpg_key_user_name" t.text "gpg_key_user_email" - t.integer "verification_status", limit: 2 + t.integer "verification_status", limit: 2, default: 0, null: false end add_index "gpg_signatures", ["commit_sha"], name: "index_gpg_signatures_on_commit_sha", unique: true, using: :btree -- GitLab