diff options
author | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-05-10 12:04:44 +0300 |
---|---|---|
committer | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-05-15 11:01:29 +0300 |
commit | 02e156c2e726c2b06df87d371511694f0e03027e (patch) | |
tree | 971bdaa0138d79fa7da2d160139bd7701ef0abf0 /db/post_migrate/20170510101043_add_foreign_key_on_pipeline_schedule_owner.rb | |
parent | 9f933953896d4a1ca7ee40ce3fef4ead4b73ab65 (diff) |
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
Diffstat (limited to 'db/post_migrate/20170510101043_add_foreign_key_on_pipeline_schedule_owner.rb')
-rw-r--r-- | db/post_migrate/20170510101043_add_foreign_key_on_pipeline_schedule_owner.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/db/post_migrate/20170510101043_add_foreign_key_on_pipeline_schedule_owner.rb b/db/post_migrate/20170510101043_add_foreign_key_on_pipeline_schedule_owner.rb new file mode 100644 index 00000000000..6a870f08e89 --- /dev/null +++ b/db/post_migrate/20170510101043_add_foreign_key_on_pipeline_schedule_owner.rb @@ -0,0 +1,35 @@ +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 |