Sha256: de725a7a79db6f08dbb75457d995dbd3993f60de4ae895c80656b350e5a0ac76

Contents?: true

Size: 1.34 KB

Versions: 44

Compression:

Stored size: 1.34 KB

Contents

module Marty; module RSpec; module SharedConnectionDbHelpers
  def current_db
    ActiveRecord::Base.connection_config[:database]
  end

  def save_clean_db(clean_file)
    if db_host == 'localhost'
      `pg_dump -O -Fc #{current_db} > #{clean_file}`
    else
      `#{remote_db_pw} pg_dump -O -Fc #{remote_db_args} #{current_db} > #{clean_file}`
    end
  end

  def restore_clean_db(clean_file, remove_file = true)
    self.use_transactional_tests = false

    if db_host == 'localhost'
      `pg_restore -j 2 -O -x -c -d #{current_db} #{clean_file}`
    else
      `#{remote_db_pw} pg_restore #{remote_db_args} #{restore_args(current_db, clean_file)}`
    end

    `rm -f #{clean_file}` if remove_file
    ActiveRecord::Base.clear_all_connections!
    ActiveRecord::Base.reset_shared_connection
    self.use_transactional_tests = true
  end

  private

  def current_db
    ActiveRecord::Base.connection_config[:database]
  end

  def db_host
    ActiveRecord::Base.connection_config[:host] || 'localhost'
  end

  def db_user
    ActiveRecord::Base.connection_config[:username]
  end

  def db_password
    ActiveRecord::Base.connection_config[:password]
  end

  def restore_args db, from
    "-j 2 -O -x -c -d #{db} #{from}"
  end

  def remote_db_args
    "-h #{db_host} -U #{db_user} -w"
  end

  def remote_db_pw
    "PGPASSWORD='#{db_password}'"
  end
end end end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
marty-8.5.0 spec/support/shared_connection_db_helpers.rb
marty-8.4.1 spec/support/shared_connection_db_helpers.rb
marty-8.3.1 spec/support/shared_connection_db_helpers.rb
marty-8.2.0 spec/support/shared_connection_db_helpers.rb
marty-8.0.0 spec/support/shared_connection_db_helpers.rb
marty-6.1.0 spec/support/shared_connection_db_helpers.rb
marty-5.2.0 spec/support/shared_connection_db_helpers.rb
marty-5.1.4 spec/support/shared_connection_db_helpers.rb
marty-5.1.3 spec/support/shared_connection_db_helpers.rb
marty-5.1.2 spec/support/shared_connection_db_helpers.rb
marty-5.1.1 spec/support/shared_connection_db_helpers.rb
marty-5.1.0 spec/support/shared_connection_db_helpers.rb
marty-3.1.0 spec/support/shared_connection_db_helpers.rb
marty-3.0.1 spec/support/shared_connection_db_helpers.rb
marty-4.0.0.rc2 spec/support/shared_connection_db_helpers.rb
marty-3.0.0 spec/support/shared_connection_db_helpers.rb
marty-2.9.3 spec/support/shared_connection_db_helpers.rb
marty-2.9.2 spec/support/shared_connection_db_helpers.rb
marty-2.9.1 spec/support/shared_connection_db_helpers.rb
marty-2.8.0 spec/support/shared_connection_db_helpers.rb