Class: Innkeeper::Adapters::Mysql2Adapter

Inherits:
AbstractAdapter show all
Defined in:
lib/innkeeper/adapters/mysql2_adapter.rb

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#current

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#config_for, #connection_switch!, #create, #current_difference_from, #decorate, #drop, #import_database_schema, #initialize, #load_or_abort, #process_excluded_models, #raise_connect_error!, #reset, #seed_data, #setup_connection_specification_name, #switch, #switch!

Constructor Details

This class inherits a constructor from Innkeeper::Adapters::AbstractAdapter

Instance Method Details

#connection_specification_name(config) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/innkeeper/adapters/mysql2_adapter.rb', line 27

def connection_specification_name(config)
  if Innkeeper.pool_per_config
    "_innkeeper_#{config.hash}".to_sym
  else
    host_hash = Digest::MD5.hexdigest(config[:host] || config[:url] || "127.0.0.1")
    "_innkeeper_#{host_hash}_#{config[:adapter]}".to_sym
  end
end

#create_tenant!(config) ⇒ Object



17
18
19
# File 'lib/innkeeper/adapters/mysql2_adapter.rb', line 17

def create_tenant!(config)
  Innkeeper.connection.create_database(config[:database], config)
end

#simple_switch(config) ⇒ Object



21
22
23
24
25
# File 'lib/innkeeper/adapters/mysql2_adapter.rb', line 21

def simple_switch(config)
  Innkeeper.connection.execute("use `#{config[:database]}`")
rescue ActiveRecord::StatementInvalid => exception
  raise_connect_error!(config[:database], exception)
end

#switch_tenant(config) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/innkeeper/adapters/mysql2_adapter.rb', line 7

def switch_tenant(config)
  difference = current_difference_from(config)

  if difference[:host]
    connection_switch!(config)
  else
    simple_switch(config) if difference[:database]
  end
end