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
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-06-26 11:28:28 UTC using RuboCop version 1.76.0.
# on 2025-07-08 12:12:57 UTC using RuboCop version 1.76.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -332,7 +332,7 @@ Rails/FilePath:
- "core/lib/spree/testing_support/dummy_app.rb"
- "sample/lib/spree/sample.rb"

# Offense count: 43
# Offense count: 42
# Configuration parameters: Include.
# Include: **/app/models/**/*.rb
Rails/HasManyOrHasOneDependent:
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/adjustment_reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class AdjustmentReason < Spree::Base
has_many :adjustments, inverse_of: :adjustment_reason
has_many :adjustments, inverse_of: :adjustment_reason, dependent: :restrict_with_error

validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
validates :code, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class AddAdjustmentReasonForeignKeys < ActiveRecord::Migration[7.0]
def up
# Uncomment the following code to remove orphaned records if this migration fails
#
# say_with_time "Removing invalid adjustment reason IDs from adjustments table" do
# Spree::Adjustment.where.not(adjustment_reason_id: nil).left_joins(:adjustment_reason).where(spree_adjustment_reasons: { id: nil }).update_all(adjustment_reason_id: nil)
# end

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
⚠️ 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.
2. Rerun the migration.
Offending error: #{e.cause.class} - #{e.cause.message}
MSG
end
raise
end

def down
remove_foreign_key :spree_adjustments, :spree_adjustment_reasons, column: :adjustment_reason_id, null: true, on_delete: :restrict
end
end
Loading