Skip to content

Commit

Permalink
Enable cascading deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Yorkley committed Jan 27, 2021
1 parent c7cc6eb commit aed912a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 20 additions & 0 deletions db/migrate/20210127174120_add_cascading_deletes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AddCascadingDeletes < ActiveRecord::Migration
def change
# Updates foreign key definitions between orders, shipments, and inventory_units
# to allow for cascading deletes at database level. If an order is intentionally
# deleted *without callbacks*, it's shipments and inventory units will be removed
# cleanly without throwing foreign key errors.

remove_foreign_key :spree_shipments, :spree_orders
add_foreign_key :spree_shipments, :spree_orders, column: 'order_id',
on_delete: :cascade

remove_foreign_key :spree_inventory_units, :spree_shipments
add_foreign_key :spree_inventory_units, :spree_shipments, column: 'shipment_id',
on_delete: :cascade

remove_foreign_key :spree_inventory_units, :spree_orders
add_foreign_key :spree_inventory_units, :spree_orders, column: 'order_id',
on_delete: :cascade
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20210115143738) do
ActiveRecord::Schema.define(version: 20210127174120) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -1252,9 +1252,9 @@
add_foreign_key "proxy_orders", "subscriptions", name: "proxy_orders_subscription_id_fk"
add_foreign_key "spree_addresses", "spree_countries", column: "country_id", name: "spree_addresses_country_id_fk"
add_foreign_key "spree_addresses", "spree_states", column: "state_id", name: "spree_addresses_state_id_fk"
add_foreign_key "spree_inventory_units", "spree_orders", column: "order_id", name: "spree_inventory_units_order_id_fk"
add_foreign_key "spree_inventory_units", "spree_orders", column: "order_id", on_delete: :cascade
add_foreign_key "spree_inventory_units", "spree_return_authorizations", column: "return_authorization_id", name: "spree_inventory_units_return_authorization_id_fk"
add_foreign_key "spree_inventory_units", "spree_shipments", column: "shipment_id", name: "spree_inventory_units_shipment_id_fk"
add_foreign_key "spree_inventory_units", "spree_shipments", column: "shipment_id", on_delete: :cascade
add_foreign_key "spree_inventory_units", "spree_variants", column: "variant_id", name: "spree_inventory_units_variant_id_fk"
add_foreign_key "spree_line_items", "spree_orders", column: "order_id", name: "spree_line_items_order_id_fk"
add_foreign_key "spree_line_items", "spree_variants", column: "variant_id", name: "spree_line_items_variant_id_fk"
Expand Down Expand Up @@ -1290,7 +1290,7 @@
add_foreign_key "spree_roles_users", "spree_roles", column: "role_id", name: "spree_roles_users_role_id_fk"
add_foreign_key "spree_roles_users", "spree_users", column: "user_id", name: "spree_roles_users_user_id_fk"
add_foreign_key "spree_shipments", "spree_addresses", column: "address_id", name: "spree_shipments_address_id_fk"
add_foreign_key "spree_shipments", "spree_orders", column: "order_id", name: "spree_shipments_order_id_fk"
add_foreign_key "spree_shipments", "spree_orders", column: "order_id", on_delete: :cascade
add_foreign_key "spree_state_changes", "spree_users", column: "user_id", name: "spree_state_changes_user_id_fk"
add_foreign_key "spree_states", "spree_countries", column: "country_id", name: "spree_states_country_id_fk"
add_foreign_key "spree_tax_rates", "spree_tax_categories", column: "tax_category_id", name: "spree_tax_rates_tax_category_id_fk"
Expand Down

0 comments on commit aed912a

Please sign in to comment.