Class: Fulmar::Infrastructure::Service::Database::DatabaseService

Inherits:
Object
  • Object
show all
Defined in:
lib/fulmar/infrastructure/service/database/database_service.rb

Overview

Provides basic methods common to all database services

Constant Summary collapse

DEFAULT_CONFIG =
{
  maria: {
    host: '127.0.0.1',
    port: 3306,
    user: 'root',
    password: '',
    encoding: 'utf8'
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DatabaseService

Returns a new instance of DatabaseService.



24
25
26
27
28
29
30
31
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 24

def initialize(config)
  @config = config
  @config.merge DEFAULT_CONFIG
  @tunnel = nil
  @client = nil
  initialize_shell
  config_test
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 10

def client
  @client
end

#connectedObject (readonly) Also known as: connected?

Returns the value of attribute connected.



11
12
13
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 11

def connected
  @connected
end

#shellObject (readonly)

Returns the value of attribute shell.



11
12
13
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 11

def shell
  @shell
end

Instance Method Details

#connectObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 33

def connect
  options = compile_options

  unless local?
    tunnel.open
    options[:port] = tunnel.local_port
  end

  # Wait max 3 seconds for the tunnel to establish
  4.times do |i|
    break if try_connect(options, i)
  end

  @connected = true
end

#create(name) ⇒ Object



68
69
70
71
72
73
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 68

def create(name)
  state_before = connected?
  connect unless connected?
  @client.query "CREATE DATABASE IF NOT EXISTS `#{name}`"
  disconnect unless state_before
end

#disconnectObject



49
50
51
52
53
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 49

def disconnect
  @connected = false
  @client.close
  @tunnel.close if @tunnel # using the variable directly avoids creating a tunnel instance when closing the database connection
end

#download_dump(filename = backup_filename) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 89

def download_dump(filename = backup_filename)
  local_path = filename[0, 1] == '/' ? filename : @config[:local_path] + '/' + filename
  remote_path = dump
  copy = system("scp -q #{@config[:hostname]}:#{remote_path} #{local_path}")
  @shell.run "rm -f \"#{remote_path}\"" # delete temporary file
  if copy
    local_path
  else
    ''
  end
end

#dump(filename = backup_filename) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 75

def dump(filename = backup_filename)
  diffable = @config[:maria][:diffable_dump] ? '--skip-comments --skip-extended-insert ' : ''

  @shell.run "mysqldump -h #{@config[:maria][:host]} -u #{@config[:maria][:user]} --password='#{@config[:maria][:password]}' " \
             "#{@config[:maria][:database]} --single-transaction #{diffable}-r \"#{filename}\""

  @config[:remote_path] + '/' + filename
end

#load_dump(dump_file, database = @config[:maria]) ⇒ Object



84
85
86
87
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 84

def load_dump(dump_file, database = @config[:maria][:database])
  @shell.run "mysql -h #{@config[:maria][:host]} -u #{@config[:maria][:user]} --password='#{@config[:maria][:password]}' " \
             "-D #{database} < #{dump_file}"
end

#local?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 55

def local?
  @config[:hostname] == 'localhost'
end

#query(*arguments) ⇒ Object

shortcut for DatabaseService.client.query



64
65
66
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 64

def query(*arguments)
  @client.query(arguments)
end

#tunnelObject



59
60
61
# File 'lib/fulmar/infrastructure/service/database/database_service.rb', line 59

def tunnel
  @tunnel ||= Fulmar::Infrastructure::Service::TunnelService.new(@config[:hostname], @config[:maria][:port], @config[:maria][:hostname])
end