Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Adjust foreign keys migrations.

Pavel Balashov0af3ab4a2c00
diff --git a/db/migrate/20230314165213_add_foreign_keys_to_work_packages.rb b/db/migrate/20230314165213_add_foreign_keys_to_work_packages.rb
index 6b98476c3ef..fcb2216ca56 100644
--- a/db/migrate/20230314165213_add_foreign_keys_to_work_packages.rb
+++ b/db/migrate/20230314165213_add_foreign_keys_to_work_packages.rb
@@ -27,8 +27,15 @@
#++
class AddForeignKeysToWorkPackages < ActiveRecord::Migration[7.0]
- def change
+ def up
+ WorkPackage.where.not(type: Type.all).destroy_all
add_foreign_key :work_packages, :types, on_delete: :cascade, on_update: :cascade
+ WorkPackage.where.not(type: Status.all).destroy_all
add_foreign_key :work_packages, :statuses, on_delete: :cascade, on_update: :cascade
end
+
+ def down
+ remove_foreign_key :work_packages, :types
+ remove_foreign_key :work_packages, :statuses
+ end
end
diff --git a/db/migrate/20230315103437_add_foreign_keys_to_workflows.rb b/db/migrate/20230315103437_add_foreign_keys_to_workflows.rb
index d2d408efc2a..423e4f583de 100644
--- a/db/migrate/20230315103437_add_foreign_keys_to_workflows.rb
+++ b/db/migrate/20230315103437_add_foreign_keys_to_workflows.rb
@@ -26,10 +26,20 @@
#++
class AddForeignKeysToWorkflows < ActiveRecord::Migration[7.0]
- def change
+ def up
+ Workflow.where.not(type: Type.all).destroy_all
add_foreign_key :workflows, :types, on_delete: :cascade, on_update: :cascade
+ Workflow.where.not(old_status_id: Status.all).or(Workflow.where.not(new_status_id: Status.all)).destroy_all
add_foreign_key :workflows, :statuses, column: 'old_status_id', on_delete: :cascade, on_update: :cascade
add_foreign_key :workflows, :statuses, column: 'new_status_id', on_delete: :cascade, on_update: :cascade
+ Workflow.where.not(type: Role.all).destroy_all
add_foreign_key :workflows, :roles, on_delete: :cascade, on_update: :cascade
end
+
+ def down
+ remove_foreign_key :workflows, :types
+ remove_foreign_key :workflows, :statuses
+ remove_foreign_key :workflows, :statuses
+ remove_foreign_key :workflows, :roles
+ end
end
diff --git a/db/migrate/20230315183431_add_foreign_keys_to_projects_types.rb b/db/migrate/20230315183431_add_foreign_keys_to_projects_types.rb
index cd28a7065f8..2893bb93701 100644
--- a/db/migrate/20230315183431_add_foreign_keys_to_projects_types.rb
+++ b/db/migrate/20230315183431_add_foreign_keys_to_projects_types.rb
@@ -25,8 +25,18 @@
#++
class AddForeignKeysToProjectsTypes < ActiveRecord::Migration[7.0]
- def change
+ def up
+ execute <<~SQL.squish
+ DELETE FROM projects_types
+ WHERE type_id NOT IN (SELECT id FROM types) OR
+ project_id NOT IN (SELECT id FROM projects)
+ SQL
add_foreign_key :projects_types, :types, on_delete: :cascade, on_update: :cascade
add_foreign_key :projects_types, :projects, on_delete: :cascade, on_update: :cascade
end
+
+ def down
+ remove_foreign_key :projects_types, :types
+ remove_foreign_key :projects_types, :projects
+ end
end
diff --git a/db/migrate/20230315184533_add_foreign_keys_to_custom_fields_projects.rb b/db/migrate/20230315184533_add_foreign_keys_to_custom_fields_projects.rb
index 07f3889242c..6d79969483c 100644
--- a/db/migrate/20230315184533_add_foreign_keys_to_custom_fields_projects.rb
+++ b/db/migrate/20230315184533_add_foreign_keys_to_custom_fields_projects.rb
@@ -27,8 +27,18 @@
#++
class AddForeignKeysToCustomFieldsProjects < ActiveRecord::Migration[7.0]
- def change
+ def up
+ execute <<~SQL.squish
+ DELETE FROM custom_fields_projects
+ WHERE custom_field_id NOT IN (SELECT id FROM custom_fields) OR
+ project_id NOT IN (SELECT id FROM projects)
+ SQL
add_foreign_key :custom_fields_projects, :custom_fields, on_delete: :cascade, on_update: :cascade
add_foreign_key :custom_fields_projects, :projects, on_delete: :cascade, on_update: :cascade
end
+
+ def down
+ remove_foreign_key :custom_fields_projects, :custom_fields
+ remove_foreign_key :custom_fields_projects, :projects
+ end
end