|
2 | 2 |
|
3 | 3 | class AddShippingCategoryForeignKeys < ActiveRecord::Migration[7.0] |
4 | 4 | def change |
5 | | - add_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false |
6 | | - add_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false |
7 | | - add_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false |
| 5 | + # Uncomment the following code to remove orphaned records if the following code fails |
| 6 | + # |
| 7 | + # say_with_time "Removing orphaned products (no corresponding shipping category)" do |
| 8 | + # Spree::Product.left_joins(:shipping_category).where(spree_shipping_category: { id: nil }).delete_all |
| 9 | + # end |
| 10 | + begin |
| 11 | + add_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false |
| 12 | + rescue ActiveRecord::StatementInvalid => e |
| 13 | + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) |
| 14 | + Rails.logger.warn <<~MSG |
| 15 | + ⚠️ Foreign key constraint failed when adding :spree_products => :spree_shipping_categories. |
| 16 | + To fix this: |
| 17 | + 1. Uncomment the code that removes orphaned records. |
| 18 | + 2. Rerun the migration. |
| 19 | + Offending error: #{e.cause.class} - #{e.cause.message} |
| 20 | + MSG |
| 21 | + end |
| 22 | + raise |
| 23 | + end |
| 24 | + |
| 25 | + # Uncomment the following code to remove orphaned records if the following code fails |
| 26 | + # |
| 27 | + # say_with_time "Removing orphaned shipping method categories (no corresponding shipping category)" do |
| 28 | + # Spree::ShippingMethodCategory.left_joins(:shipping_category).where(spree_shipping_category: { id: nil }).delete_all |
| 29 | + # end |
| 30 | + begin |
| 31 | + add_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false |
| 32 | + rescue ActiveRecord::StatementInvalid => e |
| 33 | + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) |
| 34 | + Rails.logger.warn <<~MSG |
| 35 | + ⚠️ Foreign key constraint failed when adding :spree_shipping_method_categories => :spree_shipping_methods. |
| 36 | + To fix this: |
| 37 | + 1. Uncomment the code that removes orphaned records. |
| 38 | + 2. Rerun the migration. |
| 39 | + Offending error: #{e.cause.class} - #{e.cause.message} |
| 40 | + MSG |
| 41 | + end |
| 42 | + raise |
| 43 | + end |
| 44 | + |
| 45 | + # Uncomment the following code to remove orphaned records if the following code fails |
| 46 | + # |
| 47 | + # say_with_time "Removing orphaned shipping method categories (no corresponding shipping method)" do |
| 48 | + # Spree::ShippingMethodCategory.left_joins(:shipping_method).where(spree_shipping_method: { id: nil }).delete_all |
| 49 | + # end |
| 50 | + begin |
| 51 | + add_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false |
| 52 | + rescue ActiveRecord::StatementInvalid => e |
| 53 | + if e.cause.is_a?(PG::ForeignKeyViolation) || e.cause.is_a?(Mysql2::Error) || e.cause.is_a?(SQLite3::ConstraintException) |
| 54 | + Rails.logger.warn <<~MSG |
| 55 | + ⚠️ Foreign key constraint failed when adding :spree_shipping_method_categories => :spree_shipping_categories. |
| 56 | + To fix this: |
| 57 | + 1. Uncomment the code that removes orphaned records. |
| 58 | + 2. Rerun the migration. |
| 59 | + Offending error: #{e.cause.class} - #{e.cause.message} |
| 60 | + MSG |
| 61 | + end |
| 62 | + raise |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + def down |
| 67 | + remove_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false |
| 68 | + remove_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false |
| 69 | + remove_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false |
8 | 70 | end |
9 | 71 | end |
0 commit comments