-
Notifications
You must be signed in to change notification settings - Fork 614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return inserted ids MySQL #279
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ module ActiveRecord::Import::MysqlAdapter | |
def insert_many( sql, values, *args ) # :nodoc: | ||
# the number of inserts default | ||
number_of_inserts = 0 | ||
first_inserted_id = nil | ||
ids = [] | ||
|
||
base_sql, post_sql = if sql.is_a?( String ) | ||
[sql, ''] | ||
|
@@ -32,21 +34,26 @@ def insert_many( sql, values, *args ) # :nodoc: | |
|
||
# if we can insert it all as one statement | ||
if NO_MAX_PACKET == max || total_bytes < max | ||
number_of_inserts += 1 | ||
number_of_inserts = values.size | ||
sql2insert = base_sql + values.join( ',' ) + post_sql | ||
insert( sql2insert, *args ) | ||
first_inserted_id = insert( sql2insert, *args ) | ||
else | ||
value_sets = ::ActiveRecord::Import::ValueSetsBytesParser.parse(values, | ||
reserved_bytes: sql_size, | ||
max_bytes: max) | ||
value_sets.each do |value_set| | ||
number_of_inserts += 1 | ||
sql2insert = base_sql + value_set.join( ',' ) + post_sql | ||
insert( sql2insert, *args ) | ||
ids << insert( sql2insert, *args ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will result in missing ids because this is a batch operation. |
||
end | ||
end | ||
|
||
[number_of_inserts, []] | ||
if first_inserted_id | ||
last_inserted_id = first_inserted_id + number_of_inserts - 1 | ||
ids = (first_inserted_id..last_inserted_id).to_a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zdennis what do you think about this approach? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @robertomiranda, thank you for contributing to ar-import! With regard to your question above I have the same concerns as @tonic20 and @jkowens. I'd want to ensure that we however this implemented we can guarantee reliability and avoid unsuspecting issues when the returned IDs are wrong. |
||
end | ||
|
||
[number_of_inserts, ids] | ||
end | ||
|
||
# Returns the maximum number of bytes that the server will allow | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be reverted to
number_of_inserts += 1
because it is not counting the number of rows inserted, but the actual number of insert operations executed.