Commit 02e156c2 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Add foreign key on pipeline_schedules.owner_id

Set to nullify, so the settings remain if the user is deleted.

Fixes gitlab-org/gitlab-ce#31932
parent 9f933953
class AddForeignKeyOnPipelineScheduleOwner < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
execute <<-SQL
UPDATE ci_pipeline_schedules
SET owner_id = NULL
WHERE NOT EXISTS (
SELECT true
FROM users
WHERE ci_pipeline_schedules.owner_id = users.id
)
SQL
add_concurrent_foreign_key(:ci_pipeline_schedules, :users, column: :owner_id, on_delete: on_delete)
end
def down
remove_foreign_key(:ci_pipeline_schedules, column: :owner_id)
end
private
def on_delete
if Gitlab::Database.mysql?
:nullify
else
'SET NULL'
end
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170508190732) do
ActiveRecord::Schema.define(version: 20170510101043) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -1414,6 +1414,7 @@ ActiveRecord::Schema.define(version: 20170508190732) do
add_foreign_key "chat_teams", "namespaces", on_delete: :cascade
add_foreign_key "ci_builds", "ci_pipelines", column: "auto_canceled_by_id", name: "fk_a2141b1522", on_delete: :nullify
add_foreign_key "ci_pipeline_schedules", "projects", name: "fk_8ead60fcc4", on_delete: :cascade
add_foreign_key "ci_pipeline_schedules", "users", column: "owner_id", name: "fk_9ea99f58d2", on_delete: :nullify
add_foreign_key "ci_pipelines", "ci_pipeline_schedules", column: "pipeline_schedule_id", name: "fk_3d34ab2e06", on_delete: :nullify
add_foreign_key "ci_pipelines", "ci_pipelines", column: "auto_canceled_by_id", name: "fk_262d4c2d19", on_delete: :nullify
add_foreign_key "ci_trigger_requests", "ci_triggers", column: "trigger_id", name: "fk_b8ec8b7245", on_delete: :cascade
......
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