mysql_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ .rvmrc
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in db_importer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 ralph
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # DbImporter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mysql_cli'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mysql_cli
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+ task :default => :test
@@ -0,0 +1,59 @@
1
+ require 'mysql_cli/version'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'active_support/core_ext/logger.rb'
5
+ require 'cocaine'
6
+ require 'yaml'
7
+
8
+ class MysqlCli
9
+ attr_accessor *[
10
+ :credentials,
11
+ :dry_run,
12
+ :import_file,
13
+ :log,
14
+ ]
15
+
16
+ def initialize
17
+ self.dry_run = true
18
+ self.log = false
19
+ end
20
+
21
+ def read_credentials_from_config(config_file, env = 'development')
22
+ db_credentials = YAML.load_file(config_file)[env]
23
+ db_credentials.symbolize_keys!
24
+ db_credentials[:host] ||= '127.0.0.1'
25
+ db_credentials[:username] ||= 'root'
26
+
27
+ self.credentials = db_credentials
28
+ end
29
+
30
+ def sql(file_or_string, ignore_errors = false)
31
+ params = '-h :host -u :username'
32
+ params = "-p:password #{params}" if credentials[:password].present?
33
+ if File.extname(file_or_string) == '.sql'
34
+ params = "#{params} #{credentials[:database]} < :file_path"
35
+ param_values = credentials.merge(file_path: file_or_string.path)
36
+ elsif File.extname(file_or_string) == '.gz'
37
+ params = "< :file_path | mysql #{params} #{credentials[:database]}"
38
+ param_values = credentials.merge(file_path: file_or_string.path)
39
+ param_values.merge!(expected_outcodes: [0, 1]) if ignore_errors
40
+ cl = Cocaine::CommandLine.new('gunzip', params, param_values)
41
+ else
42
+ params = "-e :sql #{params}"
43
+ use_db_sql = "USE #{credentials[:database]}; #{file_or_string}"
44
+ param_values = credentials.merge(sql: use_db_sql)
45
+ end
46
+ param_values.merge!(expected_outcodes: [0, 1]) if ignore_errors
47
+ cl ||= Cocaine::CommandLine.new('mysql', params, param_values)
48
+ run cl
49
+ end
50
+
51
+ def run(cl)
52
+ puts cl.command if log
53
+ dry_run? ? cl.command : cl.run
54
+ end
55
+
56
+ def dry_run?
57
+ !!dry_run
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ class MysqlCli
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mysql_cli/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Ralph von der Heyden']
6
+ gem.email = ['ralph@rvdh.de']
7
+ gem.description = %q{Talk to Mysql databases via mysql cli tool}
8
+ gem.summary = %q{Talk to Mysql databases via mysql cli tool}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'mysql_cli'
15
+ gem.require_paths = ['lib']
16
+ gem.version = MysqlCli::VERSION
17
+ gem.add_development_dependency 'rake'
18
+ gem.add_runtime_dependency 'activesupport'
19
+ gem.add_runtime_dependency 'cocaine'
20
+ end
@@ -0,0 +1,25 @@
1
+ development:
2
+ adapter: mysql2
3
+ encoding: utf8
4
+ database: application_development
5
+ username: root
6
+ password:
7
+ staging:
8
+ adapter: mysql2
9
+ encoding: utf8
10
+ database: application_development
11
+ username: root
12
+ password:
13
+ test:
14
+ adapter: mysql2
15
+ encoding: utf8
16
+ database: application_test
17
+ username: root
18
+ password:
19
+ pool: 5
20
+ production:
21
+ adapter: mysql2
22
+ encoding: utf8
23
+ database: application_development
24
+ username: root
25
+ password:
@@ -0,0 +1 @@
1
+ CREATE TABLE test (id INT);
Binary file
@@ -0,0 +1,46 @@
1
+ require 'minitest/autorun'
2
+ require 'mysql_cli'
3
+
4
+ class MysqlCliTest < MiniTest::Unit::TestCase
5
+ TEST_ROOT=Pathname.new(__FILE__).dirname
6
+
7
+ def setup
8
+ @cli = MysqlCli.new
9
+ @cli.read_credentials_from_config(TEST_ROOT.join('database.yml'))
10
+ end
11
+
12
+
13
+
14
+ class Credentials < self
15
+ def test_credentials_can_bet_set
16
+ credentials = { host: '127.0.0.1', user: 'root' }
17
+ @cli.credentials = credentials
18
+ assert_equal credentials, @cli.credentials
19
+ end
20
+
21
+ def test_read_credentials_from_config
22
+ refute @cli.credentials.values_at(:host, :username).include?(nil)
23
+ end
24
+ end
25
+
26
+
27
+
28
+ class Sql < self
29
+ def test_sql_string
30
+ expected = "mysql -e 'USE application_development; show databases' -h '127.0.0.1' -u 'root'"
31
+ assert_equal expected, @cli.sql('show databases')
32
+ end
33
+
34
+ def test_sql_file
35
+ path = TEST_ROOT.join('dump.sql').to_s
36
+ expected = "mysql -h '127.0.0.1' -u 'root' application_development < '#{path}'"
37
+ assert_equal expected, @cli.sql(File.new path)
38
+ end
39
+
40
+ def test_gzipped_sql_file
41
+ path = TEST_ROOT.join('dump.sql.gz').to_s
42
+ expected = "gunzip < '#{path}' | mysql -h '127.0.0.1' -u 'root' application_development"
43
+ assert_equal expected, @cli.sql(File.new path)
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ralph von der Heyden
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70146568070420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70146568070420
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70146568070000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70146568070000
36
+ - !ruby/object:Gem::Dependency
37
+ name: cocaine
38
+ requirement: &70146568069580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70146568069580
47
+ description: Talk to Mysql databases via mysql cli tool
48
+ email:
49
+ - ralph@rvdh.de
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - lib/mysql_cli.rb
61
+ - lib/mysql_cli/version.rb
62
+ - mysql_cli.gemspec
63
+ - test/database.yml
64
+ - test/dump.sql
65
+ - test/dump.sql.gz
66
+ - test/mysql_cli_test.rb
67
+ homepage: ''
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: 1867626526838144135
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: 1867626526838144135
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.16
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Talk to Mysql databases via mysql cli tool
97
+ test_files:
98
+ - test/database.yml
99
+ - test/dump.sql
100
+ - test/dump.sql.gz
101
+ - test/mysql_cli_test.rb