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

Add create_records to speed things up when inserting many records #96

Merged
merged 2 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion lib/sequent/core/base_event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(record_session = Sequent::Core::RecordSessions::ActiveRecordSessi
@record_session = record_session
end

def_delegators :@record_session, :update_record, :create_record, :create_or_update_record, :get_record!, :get_record,
def_delegators :@record_session, :update_record, :create_record, :create_records, :create_or_update_record, :get_record!, :get_record,
:delete_all_records, :update_all_records, :do_with_records, :do_with_record, :delete_record,
:find_records, :last_record, :execute

Expand Down
15 changes: 15 additions & 0 deletions lib/sequent/core/record_sessions/active_record_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ def create_record(record_class, values)
record
end

def create_records(record_class, array_of_value_hashes)
table = Arel::Table.new(record_class.table_name)

query = array_of_value_hashes.map do |values|
insert_manager = Arel::InsertManager.new(ActiveRecord::Base)
insert_manager.into(table)
insert_manager.insert(values.map do |key, value|
[table[key], value]
end)
insert_manager.to_sql
end.join(";")

execute(query)
end

def create_or_update_record(record_class, values, created_at = Time.now)
record = get_record(record_class, values)
unless record
Expand Down
4 changes: 4 additions & 0 deletions lib/sequent/core/record_sessions/replay_events_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def hash
record
end

def create_records(record_class, array_of_value_hashes)
array_of_value_hashes.each { |values| create_record(record_class, values) }
end

def create_or_update_record(record_class, values, created_at = Time.now)
record = get_record(record_class, values)
unless record
Expand Down