Class: Arql::Mysqldump

Inherits:
Object show all
Defined in:
lib/arql/mysqldump.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Mysqldump

Returns a new instance of Mysqldump.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/arql/mysqldump.rb', line 4

def initialize(options = nil)
  options ||= App.connect_options
  @options = options
  if options[:socket]
    port_or_sock = "-S #{options[:socket]}"
  else
    port_or_sock = "-P #{options[:port] || 3306}"
  end
  @base_dump_cmd = "mysqldump -u %{user} -h %{host} %{port_or_sock} %{password} --skip-lock-tables " % {
    user: options[:username],
    host: options[:host],
    password: options[:password].presence.try { |e| "-p#{e}" } || '',
    port_or_sock: port_or_sock
  }
end

Instance Method Details

#dump_database(filename, no_create_db = false) ⇒ Object



24
25
26
# File 'lib/arql/mysqldump.rb', line 24

def dump_database(filename, no_create_db = false)
  system dump_database_cmd(no_create_db) + " > #{filename}"
end

#dump_database_cmd(no_create_db = false) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/arql/mysqldump.rb', line 36

def dump_database_cmd(no_create_db = false)
  @base_dump_cmd + " " + if no_create_db
                           "--no-create-db --add-drop-database --databases #{@options[:database]}"
                         else
                           "--add-drop-database --add-drop-table --databases #{@options[:database]}"
                         end
end

#dump_table(filename, table_name, no_create_table = false) ⇒ Object



20
21
22
# File 'lib/arql/mysqldump.rb', line 20

def dump_table(filename, table_name, no_create_table = false)
  system dump_table_cmd(table_name, no_create_table) + " > #{filename}"
end

#dump_table_cmd(table_name, no_create_table = false) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/arql/mysqldump.rb', line 28

def dump_table_cmd(table_name, no_create_table = false)
  @base_dump_cmd + " " + if no_create_table
                           "--no-create-info #{@options[:database]} #{table_name}"
                         else
                           "--add-drop-table #{@options[:database]} #{table_name}"
                         end
end