Sha256: a6fb944a9901244f6d949663b0b5fadd15e7e7073c56fc05ee5df4f63c85f002

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require 'active_support/core_ext/hash/keys'

module ActiveRecordHelpers
  extend Forwardable

  CONNECTION_DELEGATES = %i(create_table add_index execute)

  def connection
    ActiveRecord::Base.connection
  end

  delegate CONNECTION_DELEGATES => :connection

  def setup_postgresql!
    ActiveRecord::Base.connection.execute <<-SQL
      CREATE EXTENSION "uuid-ossp";
    SQL
  end

  # see https://github.com/pat/combustion/blob/master/lib/combustion/database.rb
  def connect_to_database!(db)
    db_config = ActiveRecord::Base.configurations[db.to_s].symbolize_keys

    if ActiveRecord::Base.connected?
      return if ActiveRecord::Base.connection_config == db_config
      ActiveRecord::Base.remove_connection
    end

    case db_config[:adapter]
    when 'sqlite3'
      fail 'only support sqlite3 in-memory' unless db_config[:database] == ':memory:'
    when 'postgresql'
      ActiveRecord::Base.establish_connection(db_config.merge(database: 'postgres'))
      ActiveRecord::Base.connection.drop_database(db_config[:database])
      ActiveRecord::Base.connection.create_database(db_config[:database], db_config)
      ActiveRecord::Base.remove_connection
    when 'mysql2'
      require 'mysql2'
      ActiveRecord::Base.establish_connection(db_config.merge(database: nil))
      ActiveRecord::Base.connection.drop_database(db_config[:database])
      ActiveRecord::Base.connection.create_database(db_config[:database], db_config)
      ActiveRecord::Base.remove_connection
    end

    ActiveRecord::Base.establish_connection(db)
    ActiveRecord::Base.connection # triggers connection to actually happen
    fail "failed to connect to #{db_config.inspect}" unless ActiveRecord::Base.connected?

    setup_postgresql! if db_config[:adapter] == 'postgresql'
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
schema_expectations-0.5.0 spec/support/active_record_helpers.rb
schema_expectations-0.4.0 spec/support/active_record_helpers.rb
schema_expectations-0.3.0 spec/support/active_record_helpers.rb