Class: MysqlCli
- Inherits:
-
Object
- Object
- MysqlCli
- Defined in:
- lib/mysql_cli.rb,
lib/mysql_cli/version.rb
Constant Summary collapse
- VERSION =
'0.0.1'
Instance Method Summary collapse
- #dry_run? ⇒ Boolean
-
#initialize ⇒ MysqlCli
constructor
A new instance of MysqlCli.
- #read_credentials_from_config(config_file, env = 'development') ⇒ Object
- #run(cl) ⇒ Object
- #sql(file_or_string, ignore_errors = false) ⇒ Object
Constructor Details
#initialize ⇒ MysqlCli
Returns a new instance of MysqlCli.
16 17 18 19 |
# File 'lib/mysql_cli.rb', line 16 def initialize self.dry_run = true self.log = false end |
Instance Method Details
#dry_run? ⇒ Boolean
56 57 58 |
# File 'lib/mysql_cli.rb', line 56 def dry_run? !!dry_run end |
#read_credentials_from_config(config_file, env = 'development') ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/mysql_cli.rb', line 21 def read_credentials_from_config(config_file, env = 'development') db_credentials = YAML.load_file(config_file)[env] db_credentials.symbolize_keys! db_credentials[:host] ||= '127.0.0.1' db_credentials[:username] ||= 'root' self.credentials = db_credentials end |
#run(cl) ⇒ Object
51 52 53 54 |
# File 'lib/mysql_cli.rb', line 51 def run(cl) puts cl.command if log dry_run? ? cl.command : cl.run end |
#sql(file_or_string, ignore_errors = false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mysql_cli.rb', line 30 def sql(file_or_string, ignore_errors = false) params = '-h :host -u :username' params = "-p:password #{params}" if credentials[:password].present? if File.extname(file_or_string) == '.sql' params = "#{params} #{credentials[:database]} < :file_path" param_values = credentials.merge(file_path: file_or_string.path) elsif File.extname(file_or_string) == '.gz' params = "< :file_path | mysql #{params} #{credentials[:database]}" param_values = credentials.merge(file_path: file_or_string.path) param_values.merge!(expected_outcodes: [0, 1]) if ignore_errors cl = Cocaine::CommandLine.new('gunzip', params, param_values) else params = "-e :sql #{params}" use_db_sql = "USE #{credentials[:database]}; #{file_or_string}" param_values = credentials.merge(sql: use_db_sql) end param_values.merge!(expected_outcodes: [0, 1]) if ignore_errors cl ||= Cocaine::CommandLine.new('mysql', params, param_values) run cl end |