Created
June 15, 2017 13:20
-
-
Save yn-misaki/60c370cafd909030f22dc417aed1c624 to your computer and use it in GitHub Desktop.
BULK INSERTの戻り値にPRIMARY KEYは返らない(activerecord-import) ref: http://qiita.com/yn-misaki/items/4e0c86945f51eb4d6067
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# idsが空っぽ! | |
[1] pry > User.import users | |
=> #<struct ActiveRecord::Import::Result failed_instances=[], num_inserts=5, ids=[]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
class << self | |
# = Returns | |
# This returns an object which responds to +failed_instances+ and +num_inserts+. | |
# * failed_instances - an array of objects that fails validation and were not committed to the database. An empty array if no validation is performed. | |
# * num_inserts - the number of insert statements it took to import the data | |
# * ids - the primary keys of the imported ids if the adapter supports it, otherwise an empty array. | |
def import(*args) | |
if args.first.is_a?( Array ) && args.first.first.is_a?(ActiveRecord::Base) | |
options = {} | |
options.merge!( args.pop ) if args.last.is_a?(Hash) | |
models = args.first | |
import_helper(models, options) | |
else | |
import_helper(*args) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveRecord::Import::MysqlAdapter | |
def insert_many( sql, values, options = {}, *args ) # :nodoc: | |
# the number of inserts default | |
number_of_inserts = 0 | |
base_sql, post_sql = if sql.is_a?( String ) | |
[sql, ''] | |
elsif sql.is_a?( Array ) | |
[sql.shift, sql.join( ' ' )] | |
end | |
# 〜(略)〜 | |
[number_of_inserts, []] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# UserのnameはUNIQ制約がない | |
users = names.uniq.map do |name| | |
User.new(name: name) | |
end | |
# BULK INSERTが挿入する前のIDを取得し、+1する | |
before_id = User.last.try(:id).to_i + 1 | |
# BULK INSERTでデータを挿入 | |
User.import users | |
# BULK INSERTが挿入した後のIDを取得する | |
after_id = User.last.id | |
# 挿入前後のidで絞り、挿入したデータを取得する | |
User.where(id: before_id..after_id, name: names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# UserのnameはUNIQ制約がない | |
users = names.uniq.map do |name| | |
User.new(name: name) | |
end | |
# BULK INSERTでデータを挿入 | |
User.import users | |
# 挿入したデータを取得する | |
User.where(name: names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment