rails v7.0.5, v7.0.6 非互換変更まとめメモ
https://github.com/rails/rails/releases/tag/v7.0.5
https://github.com/rails/rails/releases/tag/v7.0.6
割と非互換な変更が多いのでまとめておく
has_one関連でのcreate_associationの挙動変更
https://github.com/rails/rails/pull/46386 のバックポートがさりげなくv7.0.5, v7.0.6に入っている
code:ruby
ActiveRecord::Schema.define do
create_table(:suppliers, force: true) do |t|
end
create_table(:accounts, force: true) do |t|
t.references :supplier, index: { unique: true }
end
end
class Supplier < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
end
supplier = Supplier.take
supplier.create_account!
# 再度has_oneレコードをcreateしようとした場合の挙動が変わる
supplier.create_account!
# v7.0.4 ActiveRecord::RecordNotUnique 発生
# v7.0.5 accountsがDELETE & INSERT される
※ 同じ修正が原因だが、https://github.com/rails/rails/issues/48330 の問題もある
=> 一旦リバートされた模様 https://github.com/rails/rails/pull/48406
=> mainブランチに再度取り込まれた https://github.com/rails/rails/commit/6be41aded8c067b2b4af3b05193e551f126fb0e3
=> v7.0.6として再度そのままリリースされた
=> やっぱり影響大きいと判断されリバートされた
https://github.com/rails/rails/pull/48808
元の挙動に依存している場合は↓のように修正が必要
code:ruby
# before
supplier.create_account!
# after
Account.create!(supplier:)
https://blog.willnet.in/entry/2023/07/04/113321 が詳しい
元の挙動を再現するための機能提案PR https://github.com/rails/rails/pull/48643
enum値マッピングの挙動が変わった
https://github.com/rails/rails/issues/46892 の対応っぽい
バグとかではなくこういう挙動変更ということでアプリケーションコードに影響ある場合は要修正
code:ruby
ActiveRecord::Schema.define do
create_table(:customers, force: true) do |t|
t.integer :status, default: 0, null: false
end
create_table(:purchases, force: true) do |t|
t.references :customer
end
end
class Customer < ActiveRecord::Base
enum :status, %iactive slipping lost recovered
has_many :purchases
end
class Purchase < ActiveRecord::Base
belongs_to :customer
end
Customer.create!(status: :active).tap { |costomer| costomer.purchases.create! }
Purchase.joins(:customer).group('customers.status').count
# v7.0.4
=> {0=>1}
# v7.0.5, v7.0.6
=> {"active"=>1}
missing時に余計なalias指定される問題
mainブランチでは修正されているが、v7.0.5へのバックポートでミスってバグっている状態に見える
https://github.com/rails/rails/issues/48334
=> v7.0.6 修正版がリリースされた https://github.com/rails/rails/commit/b55307909bdf5c86c6a5e07b43f9b20ff90092d5
#アップグレード