Sha256: cc828a7e3559208bfd52e5801fbae4ad2e8ff82c5a7f197678db3983dc869622

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 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
    ActionCable.server.pubsub.shutdown
    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

12 entries across 12 versions & 1 rubygems

Version Path
marty-14.3.0 spec/support/shared_connection_db_helpers.rb
marty-14.0.0 spec/support/shared_connection_db_helpers.rb
marty-13.0.2 spec/support/shared_connection_db_helpers.rb
marty-11.0.0 spec/support/shared_connection_db_helpers.rb
marty-10.0.3 spec/support/shared_connection_db_helpers.rb
marty-10.0.2 spec/support/shared_connection_db_helpers.rb
marty-10.0.0 spec/support/shared_connection_db_helpers.rb
marty-9.5.1 spec/support/shared_connection_db_helpers.rb
marty-9.5.0 spec/support/shared_connection_db_helpers.rb
marty-9.3.3 spec/support/shared_connection_db_helpers.rb
marty-9.3.2 spec/support/shared_connection_db_helpers.rb
marty-9.3.0 spec/support/shared_connection_db_helpers.rb