Class: Baza::Cloner

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/cloner.rb

Class Method Summary collapse

Class Method Details

.from_active_record_connection(connection) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/baza/cloner.rb', line 2

def self.from_active_record_connection(connection)
  if connection.class.name.include?("Mysql2Adapter")
    connection = connection.instance_variable_get(:@connection)

    config = connection.instance_variable_get(:@query_options)
    config ||= connection.instance_variable_get(:@config)

    db_args = {
      type: :mysql2,
      host: config[:host],
      user: config[:username],
      pass: config[:password],
      db: config[:database]
    }

    Baza::Db.new(db_args)
  elsif connection.class.name.include?("MysqlAdapter")
    connection = connection.instance_variable_get(:@connection)
    connection = connection.instance_variable_get(:@connection) if RUBY_PLATFORM == "java"

    db_args = {
      type: :mysql,
      conn: connection
    }

    Baza::Db.new(db_args)
  else
    raise "Unsupported adapter: #{connection.class.name}"
  end
end