Class: RailsSync::Adapter

Inherits:
Object
  • Object
show all
Extended by:
ActiveRecord::ConnectionHandling
Defined in:
lib/rails_sync/adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec, options = {}) ⇒ Adapter

Returns a new instance of Adapter.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rails_sync/adapter.rb', line 7

def initialize(spec, options = {})
  @adapter_options = RailsSync.options.fetch(spec, {})
  @adapter_options[:name] = spec

  begin
    self.connection
  rescue
    client = self.class.connection_handler.establish_connection(@adapter_options)
    puts "established connection: #{client}"
  end
end

Instance Method Details

#connectionObject



37
38
39
# File 'lib/rails_sync/adapter.rb', line 37

def connection
  retrieve_connection
end

#retrieve_connectionObject



19
20
21
# File 'lib/rails_sync/adapter.rb', line 19

def retrieve_connection
  self.class.connection_handler.retrieve_connection(@adapter_options[:name])
end

#server_idObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_sync/adapter.rb', line 23

def server_id
  begin
    result = connection.query('select @@server_uuid')
  rescue
    result = connection.query('select @@server_id')
  end
  _id = result.to_a.flatten.first
  if _id.is_a?(Hash)
    _id.values.first
  else
    _id
  end
end

#urlObject



41
42
43
# File 'lib/rails_sync/adapter.rb', line 41

def url
  @url ||= "mysql://#{@adapter_options[:username]}:#{@adapter_options[:password]}@#{@adapter_options[:host]}:#{@adapter_options[:port]}/#{@adapter_options[:database]}"
end