From 4caf58a19b0e738889b512598c790582c24ee5c8 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Tue, 8 May 2018 11:23:13 +0100 Subject: [PATCH] Adds not null constraint to ProjectMirrorData's foreign_key project_id --- ...aint_to_project_mirror_data_foreign_key.rb | 21 +++++++++++++++++++ db/schema.rb | 4 ++-- ...to_project_mirror_data_foreign_key_spec.rb | 18 ++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20180508100222_add_not_null_constraint_to_project_mirror_data_foreign_key.rb create mode 100644 spec/migrations/add_not_null_constraint_to_project_mirror_data_foreign_key_spec.rb diff --git a/db/migrate/20180508100222_add_not_null_constraint_to_project_mirror_data_foreign_key.rb b/db/migrate/20180508100222_add_not_null_constraint_to_project_mirror_data_foreign_key.rb new file mode 100644 index 00000000000..82087d15ccb --- /dev/null +++ b/db/migrate/20180508100222_add_not_null_constraint_to_project_mirror_data_foreign_key.rb @@ -0,0 +1,21 @@ +class AddNotNullConstraintToProjectMirrorDataForeignKey < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + class ProjectImportState < ActiveRecord::Base + include EachBatch + + self.table_name = 'project_mirror_data' + end + + def up + ProjectImportState.where(project_id: nil).delete_all + + change_column_null :project_mirror_data, :project_id, false + end + + def down + change_column_null :project_mirror_data, :project_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6fd10785d77..6f40f70d350 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: 20180508055821) do +ActiveRecord::Schema.define(version: 20180508100222) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1529,7 +1529,7 @@ ActiveRecord::Schema.define(version: 20180508055821) do add_index "project_import_data", ["project_id"], name: "index_project_import_data_on_project_id", using: :btree create_table "project_mirror_data", force: :cascade do |t| - t.integer "project_id" + t.integer "project_id", null: false t.string "status" t.string "jid" t.text "last_error" diff --git a/spec/migrations/add_not_null_constraint_to_project_mirror_data_foreign_key_spec.rb b/spec/migrations/add_not_null_constraint_to_project_mirror_data_foreign_key_spec.rb new file mode 100644 index 00000000000..6fd3cb1f44e --- /dev/null +++ b/spec/migrations/add_not_null_constraint_to_project_mirror_data_foreign_key_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' +require Rails.root.join('db', 'migrate', '20180508100222_add_not_null_constraint_to_project_mirror_data_foreign_key.rb') + +describe AddNotNullConstraintToProjectMirrorDataForeignKey, :migration do + let(:namespaces) { table(:namespaces) } + let(:projects) { table(:projects) } + let(:import_state) { table(:project_mirror_data) } + + before do + import_state.create!(id: 1, project_id: nil, status: :started) + end + + it 'removes every import state without an associated project_id' do + expect do + subject.up + end.to change { import_state.count }.from(1).to(0) + end +end