diff --git a/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb b/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb index 01f160dd11..b5112675e7 100644 --- a/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb +++ b/core/db/migrate/20250530102541_add_addressbook_foreign_key.rb @@ -1,17 +1,18 @@ # frozen_string_literal: true class AddAddressbookForeignKey < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_user_addresses => :spree_addresses. To fix this: 1. Uncomment the code that removes orphaned records. diff --git a/core/db/migrate/20250604072105_add_fk_products_variant_property_rules.rb b/core/db/migrate/20250604072105_add_fk_products_variant_property_rules.rb index 472de0a6a8..2e9e7b31c6 100644 --- a/core/db/migrate/20250604072105_add_fk_products_variant_property_rules.rb +++ b/core/db/migrate/20250604072105_add_fk_products_variant_property_rules.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddFkProductsVariantPropertyRules < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def up # Uncomment the following code to remove orphaned records if this migration fails # @@ -10,8 +12,8 @@ def up add_foreign_key :spree_variant_property_rules, :spree_products, column: :product_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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_variant_property_rules => :spree_products. To fix this: 1. Uncomment the code that removes orphaned records. diff --git a/core/db/migrate/20250604072555_add_fk_to_product_properties.rb b/core/db/migrate/20250604072555_add_fk_to_product_properties.rb index 92ec81d0ed..ac02aa0441 100644 --- a/core/db/migrate/20250604072555_add_fk_to_product_properties.rb +++ b/core/db/migrate/20250604072555_add_fk_to_product_properties.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddFkToProductProperties < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def up # Uncomment the following code to remove orphaned records if this migration fails # @@ -10,8 +12,8 @@ def up begin add_foreign_key :spree_product_properties, :spree_products, column: :product_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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_product_properties => :spree_products. To fix this: 1. Uncomment the code that removes orphaned records. @@ -30,8 +32,8 @@ def up begin add_foreign_key :spree_product_properties, :spree_properties, column: :property_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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_product_properties => :spree_properties. To fix this: 1. Uncomment the code that removes orphaned records. diff --git a/core/db/migrate/20250604072948_add_fk_to_product_option_types.rb b/core/db/migrate/20250604072948_add_fk_to_product_option_types.rb index 60df7a375d..05fa6004f0 100644 --- a/core/db/migrate/20250604072948_add_fk_to_product_option_types.rb +++ b/core/db/migrate/20250604072948_add_fk_to_product_option_types.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddFkToProductOptionTypes < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def up # Uncomment the following code to remove orphaned records if this migration fails # @@ -10,8 +12,8 @@ def up begin add_foreign_key :spree_product_option_types, :spree_products, column: :product_id 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_product_option_types => :spree_products. To fix this: 1. Uncomment the code that removes orphaned records. @@ -30,8 +32,8 @@ def up begin add_foreign_key :spree_product_option_types, :spree_option_types, column: :option_type_id 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_product_option_types => :spree_option_types. To fix this: 1. Uncomment the code that removes orphaned records. diff --git a/core/db/migrate/20250604073219_add_fk_to_classifications.rb b/core/db/migrate/20250604073219_add_fk_to_classifications.rb index b81c34169f..f8519eed06 100644 --- a/core/db/migrate/20250604073219_add_fk_to_classifications.rb +++ b/core/db/migrate/20250604073219_add_fk_to_classifications.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddFkToClassifications < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def up # Uncomment the following code to remove orphaned records if this migration fails # @@ -10,8 +12,8 @@ def up begin add_foreign_key :spree_products_taxons, :spree_products, column: :product_id 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_products_taxons => :spree_products. To fix this: 1. Uncomment the code that removes orphaned records. @@ -30,8 +32,8 @@ def up begin add_foreign_key :spree_products_taxons, :spree_taxons, column: :taxon_id 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_products_taxons => :spree_taxons. To fix this: 1. Uncomment the code that removes orphaned records. 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 9bdda77e2f..910eca3d2a 100644 --- a/core/db/migrate/20250605105424_add_shipping_category_foreign_keys.rb +++ b/core/db/migrate/20250605105424_add_shipping_category_foreign_keys.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddShippingCategoryForeignKeys < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def change # Uncomment the following code to remove orphaned records if the following code fails # @@ -10,8 +12,8 @@ def change 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_products => :spree_shipping_categories. To fix this: 1. Uncomment the code that removes orphaned records. @@ -30,8 +32,8 @@ def change 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~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. @@ -50,8 +52,8 @@ def change 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~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. diff --git a/core/db/migrate/20250626112117_add_foreign_key_to_spree_role_users.rb b/core/db/migrate/20250626112117_add_foreign_key_to_spree_role_users.rb index e331684a6d..da61e7a932 100644 --- a/core/db/migrate/20250626112117_add_foreign_key_to_spree_role_users.rb +++ b/core/db/migrate/20250626112117_add_foreign_key_to_spree_role_users.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddForeignKeyToSpreeRoleUsers < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def up # Uncomment the following code to remove orphaned records if this migration fails # @@ -10,8 +12,8 @@ def up add_foreign_key :spree_roles_users, :spree_roles, column: :role_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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_roles_users => :spree_roles. To fix this: 1. Uncomment the code that removes orphaned records. diff --git a/core/db/migrate/20250708120317_add_adjustment_reason_foreign_keys.rb b/core/db/migrate/20250708120317_add_adjustment_reason_foreign_keys.rb index e0eac2f8a9..f7311426b0 100644 --- a/core/db/migrate/20250708120317_add_adjustment_reason_foreign_keys.rb +++ b/core/db/migrate/20250708120317_add_adjustment_reason_foreign_keys.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AddAdjustmentReasonForeignKeys < ActiveRecord::Migration[7.0] + FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException] + def up # Uncomment the following code to remove orphaned records if this migration fails # @@ -10,8 +12,8 @@ def up add_foreign_key :spree_adjustments, :spree_adjustment_reasons, column: :adjustment_reason_id, null: true, on_delete: :restrict 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 + if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS) + say <<~MSG ⚠️ Foreign key constraint failed when adding :spree_adjustments => :spree_adjustment_reasons. To fix this: 1. Uncomment the code that removes invalid adjustment reason IDs from the spree_adjustments table.