Sha256: 6e9830d8baea89298267cff33b70f403f9c3c6a475e4dcf6ab29c24ddc33856a

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

module RealDataTests
  module RSpecHelper
    def load_real_test_data(name)
      dump_path = File.join(RealDataTests.configuration.dump_path, "#{name}.sql")
      raise Error, "Test data file not found: #{dump_path}" unless File.exist?(dump_path)

      ActiveRecord::Base.transaction do
        # Disable foreign key checks
        ActiveRecord::Base.connection.execute('SET session_replication_role = replica;')

        begin
          # Load the SQL dump quietly
          result = system("psql #{connection_options} -q < #{dump_path}")
          raise Error, "Failed to load test data: #{dump_path}" unless result

        ensure
          # Re-enable foreign key checks
          ActiveRecord::Base.connection.execute('SET session_replication_role = DEFAULT;')
        end
      end
    end

    private

    def connection_options
      config = if ActiveRecord::Base.respond_to?(:connection_db_config)
        ActiveRecord::Base.connection_db_config.configuration_hash
      else
        ActiveRecord::Base.connection_config
      end

      options = []
      options << "-h #{config[:host]}" if config[:host]
      options << "-p #{config[:port]}" if config[:port]
      options << "-U #{config[:username]}" if config[:username]
      options << "-d #{config[:database]}"
      options << "-q"
      options.join(" ")
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
real_data_tests-0.3.3 lib/real_data_tests/rspec_helper.rb
real_data_tests-0.3.2 lib/real_data_tests/rspec_helper.rb
real_data_tests-0.3.1 lib/real_data_tests/rspec_helper.rb
real_data_tests-0.3.0 lib/real_data_tests/rspec_helper.rb
real_data_tests-0.2.1 lib/real_data_tests/rspec_helper.rb
real_data_tests-0.2.0 lib/real_data_tests/rspec_helper.rb
real_data_tests-0.1.0 lib/real_data_tests/rspec_helper.rb