diff options
author | Bob Lail <bob.lailfamily@gmail.com> | 2019-03-05 13:16:44 -0600 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2019-03-05 11:16:44 -0800 |
commit | 91ed21b304c468db8ce9fd830312c151432935d0 (patch) | |
tree | 354a329c8b7308a8762ef39a73e2ed1c30c51baa /activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | |
parent | db94f492c099db89746f945a522aa7e59ede97cb (diff) | |
download | rails-91ed21b304c468db8ce9fd830312c151432935d0.tar.gz rails-91ed21b304c468db8ce9fd830312c151432935d0.tar.bz2 rails-91ed21b304c468db8ce9fd830312c151432935d0.zip |
Add insert_all to ActiveRecord models (#35077)
Adds a method to ActiveRecord allowing records to be inserted in bulk without instantiating ActiveRecord models. This method supports options for handling uniqueness violations by skipping duplicate records or overwriting them in an UPSERT operation.
ActiveRecord already supports bulk-update and bulk-destroy actions that execute SQL UPDATE and DELETE commands directly. It also supports bulk-read actions through `pluck`. It makes sense for it also to support bulk-creation.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 823efee301..cf6a1217a0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -387,6 +387,22 @@ module ActiveRecord false end + def supports_insert_returning? + false + end + + def supports_insert_on_duplicate_skip? + false + end + + def supports_insert_on_duplicate_update? + false + end + + def supports_insert_conflict_target? + false + end + # This is meant to be implemented by the adapters that support extensions def disable_extension(name) end @@ -542,6 +558,19 @@ module ActiveRecord index.using.nil? end + # Called by ActiveRecord::InsertAll, + # Passed an instance of ActiveRecord::InsertAll::Builder, + # This method implements standard bulk inserts for all databases, but + # should be overridden by adapters to implement common features with + # non-standard syntax like handling duplicates or returning values. + def build_insert_sql(insert) # :nodoc: + if insert.skip_duplicates? || insert.update_duplicates? + raise NotImplementedError, "#{self.class} should define `build_insert_sql` to implement adapter-specific logic for handling duplicates during INSERT" + end + + "INSERT #{insert.into} #{insert.values_list}" + end + private def check_version end |