Class: Strike::Dumper
- Inherits:
-
Object
- Object
- Strike::Dumper
- Defined in:
- lib/strike/dumper.rb
Instance Method Summary collapse
-
#call(cli, database_url) ⇒ nil
Dumps the data from the given database to a tmp file.
-
#dump_data(cli, db_config, file) ⇒ nil
Dump the data from the database configuration and outputs it to the given file.
-
#initialize(config = {}) ⇒ Dumper
constructor
A new instance of Dumper.
-
#parse_url(database_url) ⇒ Hash
Converts a database_url to Hash with all the db data.
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.
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.
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) = %w(-c --add-drop-table --add-locks --single-transaction --set-charset --create-options --disable-keys --quick).join(' ') << " -u #{db_config[:user]}" if db_config[:user] << " -h #{db_config[:host]}" if db_config[:host] if db_config[:port] && !db_config[:port].empty? << " -P #{db_config[:port]}" end << " -p#{db_config[:password]}" if db_config[:password] << " #{db_config[:database]}" run cli, dump_cmd(, 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')
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 |