Skip to content

Use a single bulk insertion statement for fixtures per table #1

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

Open
wants to merge 1 commit into
base: 4-2-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,40 @@ def insert_fixture(fixture, table_name)
execute "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES (#{value_list.join(', ')})", 'Fixture Insert'
end

def insert_fixtures(fixtures, table_name)
columns = schema_cache.columns_hash(table_name).symbolize_keys

key_list = {}
fixtures.each do |fixture|
fixture.keys.each do |key|
key_list[key.to_sym] ||= true
end
end
key_list = key_list.keys.sort

sql = "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES "

fixtures.each_with_index do |fixture, index|
sql << ", " if index != 0
sql << "("

fixture = fixture.symbolize_keys

key_list.each_with_index do |column, index2|
sql << "," if index2 != 0
if fixture.key?(column)
sql << quote(fixture[column], columns[column])
else
sql << "DEFAULT"
end
end

sql << ")"
end

execute sql, 'Fixtures Insert'
end

def empty_insert_statement_value
"DEFAULT VALUES"
end
Expand Down
4 changes: 1 addition & 3 deletions activerecord/lib/active_record/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,7 @@ def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}
end

table_rows.each do |fixture_set_name, rows|
rows.each do |row|
conn.insert_fixture(row, fixture_set_name)
end
conn.insert_fixtures(rows, fixture_set_name)
end

# Cap primary key sequences to max(pk).
Expand Down