Skip to content

Commit 026d055

Browse files
committed
ask the fixture set for the sql statements
1 parent 9f8762f commit 026d055

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ def reset_sequence!(table, column, sequence = nil)
286286
# Inserts the given fixture into the table. Overridden in adapters that require
287287
# something beyond a simple insert (eg. Oracle).
288288
def insert_fixture(fixture, table_name)
289+
execute fixture_sql(fixture, table_name), 'Fixture Insert'
290+
end
291+
292+
def fixture_sql(fixture, table_name)
289293
columns = schema_cache.columns_hash(table_name)
290294

291295
key_list = []
@@ -294,7 +298,7 @@ def insert_fixture(fixture, table_name)
294298
quote(value, columns[name])
295299
end
296300

297-
execute "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES (#{value_list.join(', ')})", 'Fixture Insert'
301+
"INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES (#{value_list.join(', ')})"
298302
end
299303

300304
def empty_insert_statement_value

activerecord/lib/active_record/fixtures.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,8 @@ def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}
504504
connection.transaction(:requires_new => true) do
505505
fixture_sets.each do |fs|
506506
conn = fs.model_class.respond_to?(:connection) ? fs.model_class.connection : connection
507-
table_rows = fs.table_rows
508-
509-
table_rows.keys.each do |table|
510-
conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete'
511-
end
512-
513-
table_rows.each do |fixture_set_name, rows|
514-
rows.each do |row|
515-
conn.insert_fixture(row, fixture_set_name)
516-
end
507+
fs.fixture_sql(conn).each do |stmt|
508+
conn.execute stmt
517509
end
518510
end
519511

@@ -580,6 +572,16 @@ def size
580572
fixtures.size
581573
end
582574

575+
def fixture_sql(conn)
576+
table_rows = self.table_rows
577+
578+
table_rows.keys.map { |table|
579+
"DELETE FROM #{conn.quote_table_name(table)}"
580+
}.concat table_rows.flat_map { |fixture_set_name, rows|
581+
rows.map { |row| conn.fixture_sql(row, fixture_set_name) }
582+
}
583+
end
584+
583585
# Return a hash of rows to be inserted. The key is the table, the value is
584586
# a list of rows to insert to that table.
585587
def table_rows

0 commit comments

Comments
 (0)