Class: Strike::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/strike/dumper.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Dumper

Returns a new instance of Dumper.



8
9
10
# File 'lib/strike/dumper.rb', line 8

def initialize(config = {})
  @dumpfile_source = config[:dumpfile_source]
end

Instance Method Details

#call(cli, database_url) ⇒ nil

Dumps the data from the given database to a tmp file.

Parameters:

  • cli (#run)

    the cli program that responds to ‘#run`.

  • database_url (String)

    the connection info. @see ‘#parse_url`.

  • optional (Proc)

    block in which the tmp file will be used.

Returns:

  • (nil)


19
20
21
22
23
24
25
26
27
28
# File 'lib/strike/dumper.rb', line 19

def call(cli, database_url)
  tempfile do |file|
    begin
      dump_data(cli, parse_url(database_url), file)
      yield(file) if block_given?
    ensure
      file.unlink
    end
  end
end

#dump_data(cli, db_config, file) ⇒ nil

Dump the data from the database configuration and outputs it to the given file.

Parameters:

  • cli (#run)

    the cli program that responds to ‘#run`.

  • db_config (Hash)

    database configuration from ‘#parse_url`.

  • file (Tempfile, IO, File)

    the file to write the dump.

Returns:

  • (nil)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/strike/dumper.rb', line 86

def dump_data(cli, db_config, file)
  dump_options = %w(-c
                    --add-drop-table
                    --add-locks
                    --single-transaction
                    --set-charset
                    --create-options
                    --disable-keys
                    --quick).join(' ')
  dump_options << " -u #{db_config[:user]}" if db_config[:user]
  dump_options << " -h #{db_config[:host]}" if db_config[:host]
  if db_config[:port] && !db_config[:port].empty?
    dump_options << " -P #{db_config[:port]}"
  end
  dump_options << " -p#{db_config[:password]}" if db_config[:password]
  dump_options << " #{db_config[:database]}"

  run cli, dump_cmd(dump_options, file)
end

#parse_url(database_url) ⇒ Hash

Converts a database_url to Hash with all the db data.

Example:

parse_url('mysql://user:pass@localhost:100/test_db')

Parameters:

  • database_url (String)

    the connection info.

Returns:

  • (Hash)

    the database configuration with the following fields.

    db_type: String || nil,
    host: String || nil,
    port: String || nil,
    user: String || nil,
    password: String || nil,
    database: String || nil,
    



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/strike/dumper.rb', line 46

def parse_url(database_url)
  uri = URI.parse(database_url)

  {
    db_type:  uri.scheme.gsub(/^mysql2/, 'mysql'),
    host:     uri.host,
    port:     uri.port.to_s,
    user:     uri.user,
    password: uri.password,
    database: uri.path.gsub(/^\//, ''),
  }
end