Class: RealDataTests::LoadStrategies::Native
- Defined in:
- lib/real_data_tests/load_strategies/native.rb
Overview
Loads a SQL dump on the ActiveRecord connection, so the data participates in the caller's transaction (e.g. DatabaseCleaner's :transaction strategy) and rolls back with it.
Dumps without COPY ... FROM stdin blocks are sent as a single multi-statement execute (one server round-trip). Dumps containing COPY blocks fall back to block-by-block execution, streaming COPY data through raw_connection.copy_data on the same libpq session.
Instance Method Summary collapse
Methods inherited from Base
Instance Method Details
#call(dump_path) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/real_data_tests/load_strategies/native.rb', line 14 def call(dump_path) sql_content = File.read(dump_path) ActiveRecord::Base.transaction do connection = ActiveRecord::Base.connection # Disable foreign key checks connection.execute('SET session_replication_role = replica;') begin if sql_content.match?(/^COPY .* FROM stdin/i) blocks = SqlDumpParser.parse(sql_content) blocks.each_with_index do |block, index| execute_block(block, index + 1, blocks.length) end else # No COPY blocks: send the whole dump in one round-trip. The # server parses the multi-statement string, so semicolons inside # string literals are handled correctly without client-side splitting. connection.execute(sql_content) end ensure connection.execute('SET session_replication_role = DEFAULT;') end end end |