From e193a147feaf08d096fa1b78d1581854e27f4b40 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 13 Sep 2024 23:09:49 +0900 Subject: [PATCH] activerecord: Support insert_all on relation (for Rails 7.2) (#661) ActiveRecord supports insert and upsert methods on relation classes (AssociationRelation and CollectionProxy) since v6.1. The implementation of insert_all has been changed since v7.2. The implementation was moved to AR::Relation from AR::Persistence. So types follow the new structure. refs: * #658 * https://github.com/rails/rails/pull/38899 * https://github.com/rails/rails/pull/51805 * https://github.com/rails/rails/blob/v6.1.7.8/activerecord/CHANGELOG.md --- gems/activerecord/7.2/_test/test.rb | 12 ++++++++++ gems/activerecord/7.2/_test/test.rbs | 13 +++++++++++ gems/activerecord/7.2/activerecord-7.2.rbs | 27 +++++++++++++--------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/gems/activerecord/7.2/_test/test.rb b/gems/activerecord/7.2/_test/test.rb index 0c2b1dc2..5f81cb50 100644 --- a/gems/activerecord/7.2/_test/test.rb +++ b/gems/activerecord/7.2/_test/test.rb @@ -9,6 +9,9 @@ class ApplicationRecord < ActiveRecord::Base end class User < ApplicationRecord + # @dynamic articles + has_many :articles + enum status: { active: 0, inactive: 1 }, _suffix: true enum :role, { admin: 0, user: 1 }, _prefix: true enum :classify, %w[hoge fuga], _default: "hoge" @@ -24,6 +27,9 @@ class User < ApplicationRecord } end + class Article < ApplicationRecord + end + User.where.missing.to_sql User.deterministic_encrypted_attributes User.source_attribute_from_preserved_attribute(:phrase) @@ -38,6 +44,12 @@ class User < ApplicationRecord user.encrypted_attribute?(:secret) user.decrypt user.ciphertext_for(:token) + user.articles.insert({ id: 1, name: 'James' }, returning: %i[id name], unique_by: :id, record_timestamps: true) + user.articles.insert!({ id: 1, name: 'James' }, returning: %i[id name], record_timestamps: true) + user.articles.insert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) + user.articles.insert_all!([{ id: 1, name: 'James' }], returning: %i[id name], record_timestamps: true) + user.articles.upsert({ id: 1, name: 'James' }, returning: %i[id name], unique_by: :id, record_timestamps: true) + user.articles.upsert_all([{ id: 1, name: 'James' }], returning: %i[id name], unique_by: :id, record_timestamps: true) user = User.new user.normalize_attribute(:email) diff --git a/gems/activerecord/7.2/_test/test.rbs b/gems/activerecord/7.2/_test/test.rbs index 5b1ebf57..3379f461 100644 --- a/gems/activerecord/7.2/_test/test.rbs +++ b/gems/activerecord/7.2/_test/test.rbs @@ -7,5 +7,18 @@ module Test class ActiveRecord_Relation < ::ActiveRecord::Relation end + + def articles: () -> Article::ActiveRecord_Associations_CollectionProxy + end + + class Article < ApplicationRecord + extend ::ActiveRecord::Base::ClassMethods[Article, ActiveRecord_Relation, Integer] + + class ActiveRecord_Relation < ::ActiveRecord::Relation + end + + class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy + include ::ActiveRecord::Relation::Methods[Article, Integer] + end end end diff --git a/gems/activerecord/7.2/activerecord-7.2.rbs b/gems/activerecord/7.2/activerecord-7.2.rbs index adcd6f13..3391bd78 100644 --- a/gems/activerecord/7.2/activerecord-7.2.rbs +++ b/gems/activerecord/7.2/activerecord-7.2.rbs @@ -6,6 +6,15 @@ module ActiveRecord extend Normalization::ClassMethods end + class Relation + def insert: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def insert_all: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def insert!: (untyped attributes, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def insert_all!: (untyped attributes, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def upsert: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def upsert_all: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + end + class Relation module QueryMethods def with_recursive: (*untyped) -> untyped @@ -95,22 +104,18 @@ module ActiveRecord ?connection_pool: ConnectionAdapters::ConnectionPool?) -> void end - module Persistence - extend ActiveSupport::Concern - - module ClassMethods - def insert: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + module Querying + def insert: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - def insert_all: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def insert_all: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - def insert!: (untyped attributes, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def insert!: (untyped attributes, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - def insert_all!: (untyped attributes, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def insert_all!: (untyped attributes, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - def upsert: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped + def upsert: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - def upsert_all: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped - end + def upsert_all: (untyped attributes, ?unique_by: untyped?, ?returning: untyped?, ?record_timestamps: bool?) -> untyped end module Querying