Commit f2a878e8 authored by charlie ablett's avatar charlie ablett Committed by Mayra Cabrera

Use validate_false option

Use new option introduced in https://gitlab.com/gitlab-org/gitlab/merge_requests/22282 which allows a
concurrent foreign key constraint to be added without
validating, which gives the opportunity to fix existing
invalid rows before validating.
parent ef2c3ddf
---
title: Add epic milestone sourcing foreign key
merge_request: 21907
author:
type: fixed
# frozen_string_literal: true
class AddEpicDateSourcingMilestoneIndexes < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :epics, due_date_column
add_concurrent_index :epics, start_date_column
end
def down
remove_concurrent_index :epics, start_date_column
remove_concurrent_index :epics, due_date_column
end
private
def due_date_column
:due_date_sourcing_milestone_id
end
def start_date_column
:start_date_sourcing_milestone_id
end
end
# frozen_string_literal: true
class AddEpicStartDateSourcingMilestoneIdForeignKey < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_foreign_key :epics, :milestones, column: start_date_column, on_delete: :nullify, validate: false
end
def down
remove_foreign_key_if_exists :epics, column: start_date_column
end
private
def start_date_column
:start_date_sourcing_milestone_id
end
end
# frozen_string_literal: true
class AddEpicDueDateSourcingMilestoneIdForeignKey < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_foreign_key :epics, :milestones, column: due_date_column, on_delete: :nullify, validate: false
end
def down
remove_foreign_key_if_exists :epics, column: due_date_column
end
private
def due_date_column
:due_date_sourcing_milestone_id
end
end
# frozen_string_literal: true
class FixInvalidEpicSourcingMilestoneIds < ActiveRecord::Migration[5.2]
DOWNTIME = false
def up
nullify_invalid_data(:start_date_sourcing_milestone_id)
nullify_invalid_data(:due_date_sourcing_milestone_id)
end
def down
# no-op
end
private
def nullify_invalid_data(column_name)
execute(<<-SQL.squish)
UPDATE epics
SET #{column_name} = null
WHERE #{column_name} NOT IN (SELECT id FROM milestones);
SQL
end
end
# frozen_string_literal: true
class ValidateForeignKeyEpicStartDateSourcingMilestone < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
validate_foreign_key(:epics, :start_date_sourcing_milestone_id)
end
def down
# no-op
end
end
# frozen_string_literal: true
class ValidateForeignKeyEpicDueDateSourcingMilestone < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
validate_foreign_key(:epics, :due_date_sourcing_milestone_id)
end
def down
# no-op
end
end
......@@ -1573,6 +1573,7 @@ ActiveRecord::Schema.define(version: 2020_01_14_113341) do
t.index ["author_id"], name: "index_epics_on_author_id"
t.index ["closed_by_id"], name: "index_epics_on_closed_by_id"
t.index ["due_date_sourcing_epic_id"], name: "index_epics_on_due_date_sourcing_epic_id", where: "(due_date_sourcing_epic_id IS NOT NULL)"
t.index ["due_date_sourcing_milestone_id"], name: "index_epics_on_due_date_sourcing_milestone_id"
t.index ["end_date"], name: "index_epics_on_end_date"
t.index ["group_id"], name: "index_epics_on_group_id"
t.index ["iid"], name: "index_epics_on_iid"
......@@ -1580,6 +1581,7 @@ ActiveRecord::Schema.define(version: 2020_01_14_113341) do
t.index ["parent_id"], name: "index_epics_on_parent_id"
t.index ["start_date"], name: "index_epics_on_start_date"
t.index ["start_date_sourcing_epic_id"], name: "index_epics_on_start_date_sourcing_epic_id", where: "(start_date_sourcing_epic_id IS NOT NULL)"
t.index ["start_date_sourcing_milestone_id"], name: "index_epics_on_start_date_sourcing_milestone_id"
end
create_table "events", id: :serial, force: :cascade do |t|
......@@ -4587,6 +4589,8 @@ ActiveRecord::Schema.define(version: 2020_01_14_113341) do
add_foreign_key "epics", "epics", column: "due_date_sourcing_epic_id", name: "fk_013c9f36ca", on_delete: :nullify
add_foreign_key "epics", "epics", column: "parent_id", name: "fk_25b99c1be3", on_delete: :cascade
add_foreign_key "epics", "epics", column: "start_date_sourcing_epic_id", name: "fk_9d480c64b2", on_delete: :nullify
add_foreign_key "epics", "milestones", column: "due_date_sourcing_milestone_id", name: "fk_3c1fd1cccc", on_delete: :nullify
add_foreign_key "epics", "milestones", column: "start_date_sourcing_milestone_id", name: "fk_1fbed67632", on_delete: :nullify
add_foreign_key "epics", "milestones", on_delete: :nullify
add_foreign_key "epics", "namespaces", column: "group_id", name: "fk_f081aa4489", on_delete: :cascade
add_foreign_key "epics", "users", column: "assignee_id", name: "fk_dccd3f98fc", on_delete: :nullify
......
......@@ -13,7 +13,7 @@ module EE
approvers: %w[target_id user_id],
boards: %w[milestone_id],
draft_notes: %w[discussion_id],
epics: %w[updated_by_id last_edited_by_id start_date_sourcing_milestone_id due_date_sourcing_milestone_id],
epics: %w[updated_by_id last_edited_by_id],
geo_event_log: %w[hashed_storage_attachments_event_id],
geo_job_artifact_deleted_events: %w[job_artifact_id],
geo_lfs_object_deleted_events: %w[lfs_object_id],
......
......@@ -34,7 +34,7 @@ describe 'Database schema' do
draft_notes: %w[discussion_id commit_id],
emails: %w[user_id],
events: %w[target_id],
epics: %w[updated_by_id last_edited_by_id start_date_sourcing_milestone_id due_date_sourcing_milestone_id state_id],
epics: %w[updated_by_id last_edited_by_id state_id],
forked_project_links: %w[forked_from_project_id],
geo_event_log: %w[hashed_storage_attachments_event_id],
geo_job_artifact_deleted_events: %w[job_artifact_id],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment