diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2017-05-16 12:18:55 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2017-05-16 12:18:55 +0300 |
commit | b0ce5e1e307f5696aa3d9b030a3bce424becb3b5 (patch) | |
tree | 32b961370888361510bcfecd617d337d892f9beb /db/post_migrate | |
parent | 791dd5a6f134088f5cbf3ecf7bab23cf2f63b3a1 (diff) | |
parent | 02e156c2e726c2b06df87d371511694f0e03027e (diff) |
Merge branch 'zj-pipeline-schedule-owner' into 'master'
Foreign key for Pipeline schedule owner
Closes #31932
See merge request !11233
Diffstat (limited to 'db/post_migrate')
-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 |