Sha256: cb67cd04f392866ea0718fd3ce4686c843b9ca11a793398b9a8d9bb7d19f37d7

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'

module PostgreSQLHelper
  puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"

  # ActiveRecord::Base.logger = Logger.new(STDERR)

  def default_config
    db_config['postgres']
  end

  def create_db
    @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
    begin
      establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
      ActiveRecord::Base.connection.drop_database(default_config['database']) rescue nil
      ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
    rescue Exception => e
      $stderr.puts e, *(e.backtrace)
      $stderr.puts "Couldn't create database for #{default_config.inspect}"
    end
  end

  def establish_connection(config = default_config)
    ActiveRecord::Base.establish_connection(config)
  end

  def active_record_pg_setup
    create_db
    establish_connection
    active_record_load_schema
  end

  def active_record_pg_migrate
    create_db
    establish_connection
    ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations'
  end

  def active_record_pg_connection
    ActiveRecord::Base.connection
  end
end

RSpec.configure do |c|
  c.include PostgreSQLHelper
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
database_cleaner-1.6.2 spec/support/active_record/postgresql_setup.rb
database_cleaner-1.6.1 spec/support/active_record/postgresql_setup.rb
database_cleaner-1.6.0 spec/support/active_record/postgresql_setup.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/spec/support/active_record/postgresql_setup.rb
database_cleaner-1.5.3 spec/support/active_record/postgresql_setup.rb
database_cleaner-1.5.2 spec/support/active_record/postgresql_setup.rb