Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/activerecord-import/adapters/mysql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, '']
Expand All @@ -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
Copy link
Collaborator

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.

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 )
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zdennis what do you think about this approach?

Copy link
Owner

Choose a reason for hiding this comment

The 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
Expand Down