Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion core/db/migrate/20250530102541_add_addressbook_foreign_key.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading