Skip to content

Commit 7c5fcb5

Browse files
nganclaude
andcommitted
Group mount work by connection pool and use pool.with_connection
Mirror Rails' fixture loading idiom: group models by connection_pool (stable identity, role-aware) instead of connection, and wrap the batch execution in pool.with_connection. Behaviorally identical inside an rspec-rails transactional fixture (with_connection yields the already-leased connection), but more correct under role switching, multi-DB, or programmatic use of mount outside a test transaction. Also stops calling model.connection multiple times per mount. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8846ecb commit 7c5fcb5

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

lib/fixture_kit/coders/active_record_coder.rb

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ def generate(parent_data: nil, &block)
3030
end
3131

3232
def mount(data)
33-
statements_by_connection(data).each do |connection, statements|
34-
connection.disable_referential_integrity do
35-
# execute_batch is private in current supported Rails versions.
36-
# This should be revisited when Rails 8.2 makes it public.
37-
connection.__send__(:execute_batch, statements, "FixtureKit Insert")
33+
models_by_pool(data).each do |pool, models|
34+
pool.with_connection do |connection|
35+
statements = models.flat_map do |model|
36+
[build_delete_sql(connection, model.table_name), data[model]].compact
37+
end
38+
39+
connection.disable_referential_integrity do
40+
# execute_batch is private in current supported Rails versions.
41+
# This should be revisited when Rails 8.2 makes it public.
42+
connection.__send__(:execute_batch, statements, "FixtureKit Insert")
43+
end
3844
end
3945
end
4046
end
@@ -70,8 +76,8 @@ def generate_statements(models)
7076
end
7177
end
7278

73-
def build_delete_sql(model)
74-
"DELETE FROM #{model.quoted_table_name}"
79+
def build_delete_sql(connection, table_name)
80+
"DELETE FROM #{connection.quote_table_name(table_name)}"
7581
end
7682

7783
def build_insert_sql(table_name, columns, rows, connection)
@@ -81,19 +87,15 @@ def build_insert_sql(table_name, columns, rows, connection)
8187
"INSERT INTO #{quoted_table} (#{quoted_columns.join(", ")}) VALUES #{rows.join(", ")}"
8288
end
8389

84-
def statements_by_connection(records)
85-
deleted_tables = Set.new
90+
def models_by_pool(data)
91+
seen = Set.new
8692

87-
records.each_with_object({}) do |(model, sql), grouped|
88-
connection = model.connection
89-
grouped[connection] ||= []
90-
91-
table_key = [connection, model.table_name]
92-
if deleted_tables.add?(table_key)
93-
grouped[connection] << build_delete_sql(model)
94-
end
93+
data.each_with_object({}) do |(model, _), grouped|
94+
pool = model.connection_pool
95+
next unless seen.add?([pool, model.table_name])
9596

96-
grouped[connection] << sql if sql
97+
grouped[pool] ||= []
98+
grouped[pool] << model
9799
end
98100
end
99101
end

0 commit comments

Comments
 (0)