Sha256: d92eef583df0044f27ae2b8208994566540ae421e695f731fd2e692f830c70ce

Contents?: true

Size: 1.24 KB

Versions: 11

Compression:

Stored size: 1.24 KB

Contents

require 'yaml'
require 'mysql2'

class DBConnectionHelper

  DATABASE_CONFIG_FILE = "database.yml"

  class << self
    def db_config
      @db_config ||= YAML.load_file(File.expand_path(File.dirname(__FILE__)) + "/../#{DATABASE_CONFIG_FILE}")
    end

    def new_mysql_connection(role = :master, with_data = false, toxic = false)

      key = role.to_s + toxic_postfix(toxic)

      conn = ActiveRecord::Base.establish_connection(
        :host => '127.0.0.1',
        :adapter => "mysql2",
        :username => db_config[key]['user'],
        :password => db_config[key]['password'],
        :database => test_db_name,
        :port => db_config[key]['port']
      )
      conn = conn.connection
      init_with_dummy_data(conn) if with_data
      conn
    end

    def toxic_postfix(toxic)
      toxic ? "_toxic" : ""
    end

    def test_db_name
      @test_db_name ||= "test"
    end

    def test_table_name
      @test_table_name ||= "test"
    end

    def init_with_dummy_data(conn)
      conn.execute("DROP TABLE IF EXISTS #{test_table_name} ")
      conn.execute("CREATE TABLE #{test_table_name} (id int)")

      1.upto(9) do |i|
        query = "INSERT INTO #{test_table_name} (id) VALUE (#{i})"
        conn.execute(query)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
lhm-shopify-4.0.0 spec/integration/sql_retry/db_connection_helper.rb
lhm-teak-3.6.4 spec/integration/sql_retry/db_connection_helper.rb
lhm-teak-3.6.3 spec/integration/sql_retry/db_connection_helper.rb
lhm-teak-3.6.2 spec/integration/sql_retry/db_connection_helper.rb
lhm-teak-3.6.1 spec/integration/sql_retry/db_connection_helper.rb
lhm-teak-3.6.0 spec/integration/sql_retry/db_connection_helper.rb
lhm-shopify-3.5.5 spec/integration/sql_retry/db_connection_helper.rb
lhm-shopify-3.5.4 spec/integration/sql_retry/db_connection_helper.rb
lhm-shopify-3.5.3 spec/integration/sql_retry/db_connection_helper.rb
lhm-shopify-3.5.2 spec/integration/sql_retry/db_connection_helper.rb
lhm-shopify-3.5.1 spec/integration/sql_retry/db_connection_helper.rb