Skip to content

Commit 13a7bbd

Browse files
authored
Merge pull request #6303 from mamhoff/adjustment-reason-dependent
Restrict deletion of adjustment reasons if adjustments present
2 parents 008ef4d + 9e1ca7f commit 13a7bbd

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-06-26 11:28:28 UTC using RuboCop version 1.76.0.
3+
# on 2025-07-08 12:12:57 UTC using RuboCop version 1.76.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -332,7 +332,7 @@ Rails/FilePath:
332332
- "core/lib/spree/testing_support/dummy_app.rb"
333333
- "sample/lib/spree/sample.rb"
334334

335-
# Offense count: 43
335+
# Offense count: 42
336336
# Configuration parameters: Include.
337337
# Include: **/app/models/**/*.rb
338338
Rails/HasManyOrHasOneDependent:

core/app/models/spree/adjustment_reason.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Spree
44
class AdjustmentReason < Spree::Base
5-
has_many :adjustments, inverse_of: :adjustment_reason
5+
has_many :adjustments, inverse_of: :adjustment_reason, dependent: :restrict_with_error
66

77
validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
88
validates :code, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
class AddAdjustmentReasonForeignKeys < ActiveRecord::Migration[7.0]
4+
def up
5+
# Uncomment the following code to remove orphaned records if this migration fails
6+
#
7+
# say_with_time "Removing invalid adjustment reason IDs from adjustments table" do
8+
# Spree::Adjustment.where.not(adjustment_reason_id: nil).left_joins(:adjustment_reason).where(spree_adjustment_reasons: { id: nil }).update_all(adjustment_reason_id: nil)
9+
# end
10+
11+
add_foreign_key :spree_adjustments, :spree_adjustment_reasons, column: :adjustment_reason_id, null: true, on_delete: :restrict
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_adjustments => :spree_adjustment_reasons.
16+
To fix this:
17+
1. Uncomment the code that removes invalid adjustment reason IDs from the spree_adjustments table.
18+
2. Rerun the migration.
19+
Offending error: #{e.cause.class} - #{e.cause.message}
20+
MSG
21+
end
22+
raise
23+
end
24+
25+
def down
26+
remove_foreign_key :spree_adjustments, :spree_adjustment_reasons, column: :adjustment_reason_id, null: true, on_delete: :restrict
27+
end
28+
end

0 commit comments

Comments
 (0)