Sha256: 0c46a44d80dc4c8adb1bac131f897221d7045c7c2acfaff95f8ddaa74d7b4499

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module DatabaseConnection
  def self.setup
    ActiveRecord::Base.establish_connection(DATABASE_CONFIG)
    ActiveRecord::Base.connection
  rescue
    ActiveRecord::Base.establish_connection(DATABASE_CONFIG.merge('database' => nil))
    ActiveRecord::Base.connection.create_database(DATABASE_CONFIG['database'])
    ActiveRecord::Base.establish_connection(DATABASE_CONFIG)
  end

  def self.drop_tables
    TABLES.keys.each do |table|
      ActiveRecord::Base.connection.drop_table(table) if ActiveRecord::Base.connection.table_exists?(table)
    end
  end

  def self.create_tables
    TABLES.each do |table, fields|
      ActiveRecord::Base.connection.create_table(table) do |t|
        fields.each do |column_type, *args|
          t.send(column_type, *args)
        end
      end
    end
  end

  TABLES = {
    :polymorphic_records        => [[:string, :owner_type], [:integer, :owner_id], [:timestamps]],
    :deeply_associated_records  => [[:string, :name], [:integer, :associated_record_id]],
    :associated_records         => [[:string, :name], [:integer, :record_id]],
    :not_cached_records         => [[:string, :name], [:integer, :record_id]],
    :records                    => [[:string, :title], [:timestamps]]
  }

  DATABASE_CONFIG = {
    'adapter'  => 'mysql2',
    'database' => 'identity_cache_test',
    'host'     => '127.0.0.1',
    'username' => 'root'
  }
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
identity_cache-0.0.1 test/helpers/database_connection.rb