diff --git a/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb b/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb index 73d88369d1..01f160dd11 100644 --- a/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb +++ b/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb @@ -1,7 +1,28 @@ # frozen_string_literal: true class AddAddressbookForeignKey < ActiveRecord::Migration[7.0] - def change + def up + # Uncomment the following code to remove orphaned records if this migration fails + # + # say_with_time "Removing orphaned address book entries (no corresponding address)" do + # Spree::UserAddress.left_joins(:address).where(spree_addresses: { id: nil }).delete_all + # end + add_foreign_key :spree_user_addresses, :spree_addresses, column: :address_id, null: false + rescue ActiveRecord::StatementInvalid => e + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) + Rails.logger.warn <<~MSG + ⚠️ Foreign key constraint failed when adding :spree_user_addresses => :spree_addresses. + To fix this: + 1. Uncomment the code that removes orphaned records. + 2. Rerun the migration. + Offending error: #{e.cause.class} - #{e.cause.message} + MSG + end + raise + end + + def down + remove_foreign_key :spree_user_addresses, :spree_addresses, column: :address_id, null: false end end diff --git a/core/db/migrate/20250605105424_add_shipping_category_foreign_keys.rb b/core/db/migrate/20250605105424_add_shipping_category_foreign_keys.rb index baf77136f2..9bdda77e2f 100644 --- a/core/db/migrate/20250605105424_add_shipping_category_foreign_keys.rb +++ b/core/db/migrate/20250605105424_add_shipping_category_foreign_keys.rb @@ -2,8 +2,70 @@ class AddShippingCategoryForeignKeys < ActiveRecord::Migration[7.0] def change - add_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false - add_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false - add_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false + # Uncomment the following code to remove orphaned records if the following code fails + # + # say_with_time "Removing orphaned products (no corresponding shipping category)" do + # Spree::Product.left_joins(:shipping_category).where(spree_shipping_category: { id: nil }).delete_all + # end + begin + add_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false + rescue ActiveRecord::StatementInvalid => e + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) + Rails.logger.warn <<~MSG + ⚠️ Foreign key constraint failed when adding :spree_products => :spree_shipping_categories. + To fix this: + 1. Uncomment the code that removes orphaned records. + 2. Rerun the migration. + Offending error: #{e.cause.class} - #{e.cause.message} + MSG + end + raise + end + + # Uncomment the following code to remove orphaned records if the following code fails + # + # say_with_time "Removing orphaned shipping method categories (no corresponding shipping category)" do + # Spree::ShippingMethodCategory.left_joins(:shipping_category).where(spree_shipping_category: { id: nil }).delete_all + # end + begin + add_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false + rescue ActiveRecord::StatementInvalid => e + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) + Rails.logger.warn <<~MSG + ⚠️ Foreign key constraint failed when adding :spree_shipping_method_categories => :spree_shipping_methods. + To fix this: + 1. Uncomment the code that removes orphaned records. + 2. Rerun the migration. + Offending error: #{e.cause.class} - #{e.cause.message} + MSG + end + raise + end + + # Uncomment the following code to remove orphaned records if the following code fails + # + # say_with_time "Removing orphaned shipping method categories (no corresponding shipping method)" do + # Spree::ShippingMethodCategory.left_joins(:shipping_method).where(spree_shipping_method: { id: nil }).delete_all + # end + begin + add_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false + rescue ActiveRecord::StatementInvalid => e + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) + Rails.logger.warn <<~MSG + ⚠️ Foreign key constraint failed when adding :spree_shipping_method_categories => :spree_shipping_categories. + To fix this: + 1. Uncomment the code that removes orphaned records. + 2. Rerun the migration. + Offending error: #{e.cause.class} - #{e.cause.message} + MSG + end + raise + end + end + + def down + remove_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false + remove_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false + remove_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false end end