mycmd 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca8366a87ff73ce76f56bdeadaea36a3b5fa79d2
4
- data.tar.gz: cfc34ad1fca34a425ca10c2dcdbaeec68dfc11be
3
+ metadata.gz: b24c37c49a4bae08b918d206beb5a4a3882df2e0
4
+ data.tar.gz: 367143249fa9bf1c78d2b55c9e76114ccfb1ff5c
5
5
  SHA512:
6
- metadata.gz: 961aad1654773f7697114fea426ca74b7bb46943c15bc951b9bb008fbec557a6359249d4292320688e565093c792126e6ed41d17b88fee55fcbcdefbe1a0349a
7
- data.tar.gz: 1667d1e50b020bf2fe045a722c1e206e52f54b299a181532aafff52ce013835aa36cc38391bbe8a740c0cbc054c5e035c3326b9e88bddc0d0e41948e0db70168
6
+ metadata.gz: af74e85f9785004c12c8c41a065cce5fdfde0a6a42e1cc1c77a5e2c828224efe45e066032811476892b278ba23874c6cec4d5408fca1dd5d659072fb7f646029
7
+ data.tar.gz: cb36fbd4f939f2aef815191b92c90a89a3335ef278f4d3588cb5017b506ba7265fce83c011da34628df486a7e43d5f845dade69f2a7dae05f88a2a4f3c0e81a3
data/README.md CHANGED
@@ -21,10 +21,6 @@ Or install it yourself as:
21
21
 
22
22
  $ gem install mycmd
23
23
 
24
- Or clone from Github:
25
-
26
- $ git clone https://github.com/i2bskn/mycmd.git
27
-
28
24
  ## Settings
29
25
 
30
26
  Create settings file:
@@ -59,11 +55,11 @@ Execute sql:
59
55
 
60
56
  #### Config Commands
61
57
 
62
- Print current config file path:
58
+ Display current config file path:
63
59
 
64
60
  $ mycmd config which
65
61
 
66
- Print current config:
62
+ Display current config:
67
63
 
68
64
  $ mycmd config cat
69
65
 
@@ -77,6 +73,24 @@ Search settings:
77
73
 
78
74
  $ mycmd settings search innodb_buffer_pool_size
79
75
 
76
+ Display memory related settings:
77
+
78
+ $ mycmd settings memories
79
+
80
+ #### Status Commands
81
+
82
+ Display database sizes:
83
+
84
+ $ mycmd status size
85
+
86
+ Display query cache hit rate:
87
+
88
+ $ mycmd status qcache_hit_rate
89
+
90
+ Display innodb buffer pool hit rate:
91
+
92
+ $ mycmd status innodb_buffer_hit_rate
93
+
80
94
  ## Contributing
81
95
 
82
96
  1. Fork it
@@ -3,6 +3,7 @@ require "thor"
3
3
  require "mysql2"
4
4
 
5
5
  require "mycmd/version"
6
+ require "mycmd/sql"
6
7
  require "mycmd/configuration"
7
8
  require "mycmd/printer"
8
9
  require "mycmd/client"
@@ -1,8 +1,8 @@
1
1
  # coding: utf-8
2
2
 
3
- require "mycmd/clis/config_commands"
4
- require "mycmd/clis/settings_commands"
5
- require "mycmd/clis/status_commands"
3
+ require "mycmd/cli/config_commands"
4
+ require "mycmd/cli/settings_commands"
5
+ require "mycmd/cli/status_commands"
6
6
 
7
7
  module Mycmd
8
8
  class CLI < Thor
@@ -13,29 +13,40 @@ module Mycmd
13
13
 
14
14
  desc "console", "console will start sql shell."
15
15
  def console
16
- raise "mysql not found" unless Kernel.system("which mysql > /dev/null")
17
- conf = Configuration.new
18
- cmd = conf.to_hash.inject(["mysql"]) do |c,(k,v)|
19
- case k
20
- when :host then c << "-h#{v}"
21
- when :port then c << "-P#{v}"
22
- when :username then c << "-u#{v}"
23
- when :password then c << "-p#{v}"
24
- when :database then c << v
25
- end
16
+ begin
17
+ raise "mysql command not found" unless Kernel.system("which mysql > /dev/null")
18
+ Kernel.system(Client.command)
19
+ rescue => e
20
+ puts e.message
26
21
  end
27
-
28
- Kernel.system(cmd.join(" "))
29
22
  end
30
23
 
31
24
  desc 'query "[SQL]"', "query will execute sql."
32
25
  def query(sql)
33
- Client.query(sql).print
26
+ begin
27
+ Client.query(sql).print
28
+ rescue => e
29
+ puts e.message
30
+ end
34
31
  end
35
32
 
36
33
  desc 'tasks [TASK NAME]p', "tasks will execute register sql."
37
- def tasks(task)
38
- Client.execute_task(task).print
34
+ option :list, aliases: "-l", desc: "Display the tasks"
35
+ def tasks(task=nil)
36
+ if options[:list].nil?
37
+ begin
38
+ Client.execute_task(task).print
39
+ rescue => e
40
+ puts e.message
41
+ end
42
+ else
43
+ conf = Configuration.new
44
+ if conf.tasks.nil?
45
+ puts "task is not registered"
46
+ else
47
+ conf.tasks.each{|k,v| puts "#{k}:\t#{v}"}
48
+ end
49
+ end
39
50
  end
40
51
  end
41
52
  end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ module Mycmd
4
+ class ConfigCommands < Thor
5
+ namespace :config
6
+
7
+ desc "which", "which will find config file"
8
+ def which
9
+ conf = Configuration.config_find
10
+ puts conf.nil? ? "config not found" : conf
11
+ end
12
+
13
+ desc "cat", "cat will print configuration"
14
+ def cat
15
+ begin
16
+ conf = Configuration.config_find
17
+ raise "config not found" if conf.nil?
18
+ File.open(conf, "r").each {|line| puts line}
19
+ rescue => e
20
+ puts e.message
21
+ end
22
+ end
23
+
24
+ desc "edit", "edit will edit configuration"
25
+ def edit
26
+ begin
27
+ conf = Configuration.config_find
28
+ raise "config not found" if conf.nil?
29
+ Kernel.system("#{ENV['EDITOR']} #{conf}")
30
+ rescue => e
31
+ puts e.message
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+
3
+ module Mycmd
4
+ class StatusCommands < Thor
5
+ namespace :status
6
+
7
+ desc "size", "size will print database/table size"
8
+ option :database, aliases: "-d", desc: "target database."
9
+ def size
10
+ begin
11
+ sql = options["database"].nil? ? SQL::ALL_DATABASE_SIZES : SQL.table_sizes(options['database'])
12
+ Client.query(sql).print
13
+ rescue => e
14
+ puts e.message
15
+ end
16
+ end
17
+
18
+ desc "qcache_hit_rate", "qcache_hit_rate will print query cache hit rate"
19
+ def qcache_hit_rate
20
+ begin
21
+ raise "Query cache is disabled." if Client.query(SQL::QCACHE_SIZE).result.first["size"] == "0"
22
+ print_rate(Client.query(SQL::QCACHE_HIT_RATE).result, 20)
23
+ rescue => e
24
+ puts e.message
25
+ end
26
+ end
27
+
28
+ desc "innodb_buffer_hit_rate", "innodb_buffer_hit_rate will print buffer hit rate"
29
+ def innodb_buffer_hit_rate
30
+ begin
31
+ print_rate(Client.query(SQL::INNODB_BUFFER_HIT_RATE).result, 90)
32
+ rescue => e
33
+ puts e.message
34
+ end
35
+ end
36
+
37
+ private
38
+ def print_rate(rate, threshold)
39
+ if rate.nil?
40
+ rate = "\e[31munknown\e[m"
41
+ else
42
+ rate = rate.first["rate"].to_i
43
+ rate = rate >= threshold ? "\e[32m#{rate} %\e[m" : "\e[31m#{rate} %\e[m"
44
+ end
45
+ puts rate
46
+ end
47
+ end
48
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Mycmd
4
4
  class Client
5
+ attr_reader :result
6
+
5
7
  def initialize
6
8
  @configuration = Configuration.new
7
9
  @connection = @configuration.connect
@@ -21,6 +23,18 @@ module Mycmd
21
23
  Printer.new(@result, header).print
22
24
  end
23
25
 
26
+ def command
27
+ @configuration.to_hash.inject(["mysql"]) do |c,(k,v)|
28
+ case k
29
+ when :host then c << "-h#{v}"
30
+ when :port then c << "-P#{v}"
31
+ when :username then c << "-u#{v}"
32
+ when :password then c << "-p#{v}"
33
+ when :database then c << v
34
+ end
35
+ end.join(" ")
36
+ end
37
+
24
38
  class << self
25
39
  def method_missing(action, *args)
26
40
  client = self.new
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ module Mycmd
4
+ module SQL
5
+ ALL_DATABASE_SIZES = "SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T"
6
+ QCACHE_SIZE = "SELECT G.VARIABLE_VALUE AS size FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES AS G WHERE G.VARIABLE_NAME = 'QUERY_CACHE_SIZE'"
7
+ QCACHE_HIT_RATE = "SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS rate"
8
+ INNODB_BUFFER_HIT_RATE = "SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS rate"
9
+
10
+ class << self
11
+ def table_sizes(db)
12
+ "SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{db}' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{db}'"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Mycmd
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -18,8 +18,8 @@ describe Mycmd::ConfigCommands do
18
18
  expect(
19
19
  capture(:stdout){
20
20
  Mycmd::ConfigCommands.start(args)
21
- }
22
- ).to eq("config not found\n")
21
+ }.chomp
22
+ ).to eq("config not found")
23
23
  end
24
24
 
25
25
  it "should print config path" do
@@ -28,48 +28,52 @@ describe Mycmd::ConfigCommands do
28
28
  expect(
29
29
  capture(:stdout){
30
30
  Mycmd::ConfigCommands.start(args)
31
- }
32
- ).to eq("#{config}\n")
31
+ }.chomp
32
+ ).to eq(config)
33
33
  end
34
34
  end
35
35
 
36
- describe "#edit" do
37
- let(:args) {["edit"]}
36
+ describe "#cat" do
37
+ let(:args) {["cat"]}
38
38
 
39
- it "should execute edit command" do
40
- Mycmd::Configuration.stub(:config_find).and_return(".mycmd.yml")
41
- Kernel.should_receive(:system).with("#{ENV['EDITOR']} .mycmd.yml")
39
+ it "should call Configuration.config_find" do
40
+ Mycmd::Configuration.should_receive(:config_find).and_return(".mycmd.yml")
41
+ File.stub(:open).and_return([])
42
42
  Mycmd::ConfigCommands.start(args)
43
43
  end
44
44
 
45
- it "should generate exception if file not found" do
45
+ it "should print error message if file not found" do
46
46
  Mycmd::Configuration.stub(:config_find).and_return(nil)
47
- expect {
48
- Mycmd::ConfigCommands.start(args)
49
- }.to raise_error(RuntimeError)
47
+ expect(
48
+ capture(:stdout){
49
+ Mycmd::ConfigCommands.start(args)
50
+ }.chomp
51
+ ).to eq("config not found")
50
52
  end
53
+ end
54
+
55
+ describe "#edit" do
56
+ let(:args) {["edit"]}
51
57
 
52
58
  it "should call Configuration.config_find" do
53
59
  Mycmd::Configuration.should_receive(:config_find).and_return(".mycmd.yml")
54
60
  Kernel.stub(:system)
55
61
  Mycmd::ConfigCommands.start(args)
56
62
  end
57
- end
58
63
 
59
- describe "#cat" do
60
- let(:args) {["cat"]}
61
-
62
- it "should call Configuration.config_find" do
63
- Mycmd::Configuration.should_receive(:config_find).and_return(".mycmd.yml")
64
- File.stub(:open).and_return([])
64
+ it "should execute edit command" do
65
+ Mycmd::Configuration.stub(:config_find).and_return(".mycmd.yml")
66
+ Kernel.should_receive(:system).with("#{ENV['EDITOR']} .mycmd.yml")
65
67
  Mycmd::ConfigCommands.start(args)
66
68
  end
67
69
 
68
- it "should generate exception if file not found" do
70
+ it "should print error message if file not found" do
69
71
  Mycmd::Configuration.stub(:config_find).and_return(nil)
70
- expect {
71
- Mycmd::ConfigCommands.start(args)
72
- }.to raise_error(RuntimeError)
72
+ expect(
73
+ capture(:stdout){
74
+ Mycmd::ConfigCommands.start(args)
75
+ }.chomp
76
+ ).to eq("config not found")
73
77
  end
74
78
  end
75
79
  end
@@ -0,0 +1,198 @@
1
+ require "spec_helper"
2
+
3
+ describe Mycmd::StatusCommands do
4
+ let(:conn_mock) {double("connection mock")}
5
+ let(:client) {create_configuration_mock}
6
+
7
+ describe "#size" do
8
+ context "with not arguments" do
9
+ let(:args) {["size"]}
10
+ let(:sql) {"SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T"}
11
+
12
+ context "without failed" do
13
+ after {Mycmd::StatusCommands.start(args)}
14
+
15
+ it "should call Mycmd::Client.#query" do
16
+ Mycmd::Client.should_receive(:query).with(sql).and_return(client)
17
+ end
18
+
19
+ it "should call Mycmd::Client#print" do
20
+ client.should_receive(:print)
21
+ Mycmd::Client.stub(:query).and_return(client)
22
+ end
23
+ end
24
+
25
+ context "with failed" do
26
+ it "should display error message" do
27
+ Mycmd::Client.should_receive(:query).with(sql).and_raise("test")
28
+ expect(
29
+ capture(:stdout){
30
+ Mycmd::StatusCommands.start(args)
31
+ }.chomp
32
+ ).to eq("test")
33
+ end
34
+ end
35
+ end
36
+
37
+ context "with arguments" do
38
+ let(:args) {["size", "-d", "some_db"]}
39
+ let(:sql) {"SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db'"}
40
+
41
+ context "without failed" do
42
+ after {Mycmd::StatusCommands.start(args)}
43
+
44
+ it "should call Mycmd::Client.#query" do
45
+ Mycmd::Client.should_receive(:query).with(sql).and_return(client)
46
+ end
47
+
48
+ it "should call Mycmd::Client#print" do
49
+ client.should_receive(:print)
50
+ Mycmd::Client.stub(:query).and_return(client)
51
+ end
52
+ end
53
+
54
+ context "with failed" do
55
+ it "should display error message" do
56
+ Mycmd::Client.should_receive(:query).with(sql).and_raise("test")
57
+ expect(
58
+ capture(:stdout){
59
+ Mycmd::StatusCommands.start(args)
60
+ }.chomp
61
+ ).to eq("test")
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#qcache_hit_rate" do
68
+ let(:args) {["qcache_hit_rate"]}
69
+
70
+ context "with qcache enabled" do
71
+ let(:sql) {"SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS rate"}
72
+
73
+ before do
74
+ Mycmd::Client.stub(:query).with("SELECT G.VARIABLE_VALUE AS size FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES AS G WHERE G.VARIABLE_NAME = 'QUERY_CACHE_SIZE'").and_return(double("Mycmd::Client Mock", result: [{"size" => "4000"}]))
75
+ Mycmd::Client.stub(:query).with(sql).and_return(client)
76
+ end
77
+
78
+ it "should call Client.#query" do
79
+ client.stub(:result).and_return([{"rate" => 20}])
80
+ Mycmd::Client.should_receive(:query).with(sql).and_return(client)
81
+ capture(:stdout) {
82
+ Mycmd::StatusCommands.start(args)
83
+ }
84
+ end
85
+
86
+ it "should output the query cache hit rate" do
87
+ client.stub(:result).and_return([{"rate" => 20}])
88
+ expect(
89
+ capture(:stdout) {
90
+ Mycmd::StatusCommands.start(args)
91
+ }.chomp
92
+ ).to eq("\e[32m20 %\e[m")
93
+ end
94
+
95
+ it "should output red color of char if rate is unknown" do
96
+ client.stub(:result).and_return(nil)
97
+ expect(
98
+ capture(:stdout) {
99
+ Mycmd::StatusCommands.start(args)
100
+ }.chomp
101
+ ).to eq("\e[31munknown\e[m")
102
+ end
103
+
104
+ it "should output red color of char if rate < 20" do
105
+ client.stub(:result).and_return([{"rate" => 10}])
106
+ expect(
107
+ capture(:stdout) {
108
+ Mycmd::StatusCommands.start(args)
109
+ }.chomp
110
+ ).to eq("\e[31m10 %\e[m")
111
+ end
112
+
113
+ it "should output green color of char if rate >= 20" do
114
+ client.stub(:result).and_return([{"rate" => 20}])
115
+ expect(
116
+ capture(:stdout) {
117
+ Mycmd::StatusCommands.start(args)
118
+ }.chomp
119
+ ).to eq("\e[32m20 %\e[m")
120
+ end
121
+ end
122
+
123
+ context "with qcache disabled" do
124
+ it "should display error message" do
125
+ Mycmd::Client.stub(:query).with("SELECT G.VARIABLE_VALUE AS size FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES AS G WHERE G.VARIABLE_NAME = 'QUERY_CACHE_SIZE'").and_return(double("Mycmd::Client Mock", result: [{"size" => "0"}]))
126
+ expect(
127
+ capture(:stdout) {
128
+ Mycmd::StatusCommands.start(args)
129
+ }.chomp
130
+ ).to eq("Query cache is disabled.")
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "#innodb_buffer_hit_rate" do
136
+ let(:args) {["innodb_buffer_hit_rate"]}
137
+
138
+ context "without failed" do
139
+ before {Mycmd::Client.stub(:query).and_return(client)}
140
+
141
+ it "should call Client.#query" do
142
+ client.should_receive(:result).and_return([{"rate" => 95}])
143
+ expect(
144
+ capture(:stdout) {
145
+ Mycmd::StatusCommands.start(args)
146
+ }.chomp
147
+ ).not_to be_nil
148
+ end
149
+
150
+ it "should output the innodb buffer hit rate" do
151
+ client.stub(:result).and_return([{"rate" => 95}])
152
+ expect(
153
+ capture(:stdout) {
154
+ Mycmd::StatusCommands.start(args)
155
+ }.chomp
156
+ ).to eq("\e[32m95 %\e[m")
157
+ end
158
+
159
+ it "should output red color of char if rate is unknown" do
160
+ client.stub(:result).and_return(nil)
161
+ expect(
162
+ capture(:stdout) {
163
+ Mycmd::StatusCommands.start(args)
164
+ }.chomp
165
+ ).to eq("\e[31munknown\e[m")
166
+ end
167
+
168
+ it "should output red color of char if rate < 90" do
169
+ client.stub(:result).and_return([{"rate" => 85}])
170
+ expect(
171
+ capture(:stdout) {
172
+ Mycmd::StatusCommands.start(args)
173
+ }.chomp
174
+ ).to eq("\e[31m85 %\e[m")
175
+ end
176
+
177
+ it "should output green color of char if rate >= 90" do
178
+ client.stub(:result).and_return([{"rate" => 95}])
179
+ expect(
180
+ capture(:stdout) {
181
+ Mycmd::StatusCommands.start(args)
182
+ }.chomp
183
+ ).to eq("\e[32m95 %\e[m")
184
+ end
185
+ end
186
+
187
+ context "with failed" do
188
+ it "should display error message" do
189
+ Mycmd::Client.should_receive(:query).and_raise("test")
190
+ expect(
191
+ capture(:stdout) {
192
+ Mycmd::StatusCommands.start(args)
193
+ }.chomp
194
+ ).to eq("test")
195
+ end
196
+ end
197
+ end
198
+ end
@@ -3,68 +3,115 @@ require "spec_helper"
3
3
  describe Mycmd::CLI do
4
4
  let(:client) {double("client mock").as_null_object}
5
5
 
6
- let(:conn_mock) {double("connection mock").as_null_object}
7
- let(:printer_mock) {double("printer mock").as_null_object}
8
-
9
6
  describe "#console" do
10
7
  let(:args) {["console"]}
11
-
12
- before do
13
- Mycmd::Configuration.stub(:config_find).and_return(nil)
14
- end
8
+ before {Mycmd::Client.stub(:command)}
15
9
 
16
10
  it "should call Kernel.system" do
17
- conf = Mycmd::Configuration.new
18
- conf.password = "secret"
19
- conf.database = "test"
20
- Mycmd::Configuration.should_receive(:new).and_return(conf)
21
11
  Kernel.should_receive(:system).exactly(2).and_return(true)
22
12
  expect {
23
13
  Mycmd::CLI.start(args)
24
14
  }.not_to raise_error
25
15
  end
26
16
 
27
- it "should generate exception if mysql command not found" do
28
- Kernel.should_receive(:system).and_return(false)
29
- expect {
30
- Mycmd::CLI.start(args)
31
- }.to raise_error
17
+ it "should print message if mysql command not found" do
18
+ Kernel.should_receive(:system).with("which mysql > /dev/null").exactly(1).and_return(false)
19
+ expect(
20
+ capture(:stdout){
21
+ Mycmd::CLI.start(args)
22
+ }.chomp
23
+ ).to eq("mysql command not found")
32
24
  end
33
25
  end
34
26
 
35
27
  describe "#query" do
36
- before {Mycmd::Client.stub(:query).and_return(client)}
28
+ let(:args) {["query", "some sql"]}
37
29
 
38
- after do
39
- expect {
40
- Mycmd::CLI.start(["query", "some sql"])
41
- }.not_to raise_error
42
- end
30
+ context "with execution of sql is successfull" do
31
+ after do
32
+ expect {
33
+ Mycmd::CLI.start(args)
34
+ }.not_to raise_error
35
+ end
36
+
37
+ it "should call Client.#query" do
38
+ Mycmd::Client.should_receive(:query).and_return(client)
39
+ end
43
40
 
44
- it "should call Client.#query" do
45
- Mycmd::Client.should_receive(:query).and_return(client)
41
+ it "should call Client#print" do
42
+ client.should_receive(:print)
43
+ Mycmd::Client.stub(:query).and_return(client)
44
+ end
46
45
  end
47
46
 
48
- it "should call Client#print" do
49
- client.should_receive(:print)
47
+ context "with execution of sql is failed" do
48
+ it "should print error message" do
49
+ Mycmd::Client.should_receive(:query).and_raise("some error")
50
+ expect(
51
+ capture(:stdout){
52
+ Mycmd::CLI.start(args)
53
+ }.chomp
54
+ ).to eq("some error")
55
+ end
50
56
  end
51
57
  end
52
58
 
53
59
  describe "#tasks" do
54
- before {Mycmd::Client.stub(:execute_task).and_return(client)}
60
+ context "without list option" do
61
+ let(:args) {["tasks", "some_task"]}
55
62
 
56
- after do
57
- expect {
58
- Mycmd::CLI.start(["tasks", "some_task"])
59
- }.not_to raise_error
60
- end
63
+ context "with execution of task is successfull" do
64
+ after do
65
+ expect {
66
+ Mycmd::CLI.start(args)
67
+ }.not_to raise_error
68
+ end
69
+
70
+ it "should call Client.#execute_task" do
71
+ Mycmd::Client.should_receive(:execute_task).and_return(client)
72
+ end
61
73
 
62
- it "should call Client.#execute_task" do
63
- Mycmd::Client.should_receive(:execute_task).and_return(client)
74
+ it "should call Client#print" do
75
+ client.should_receive(:print)
76
+ Mycmd::Client.stub(:execute_task).and_return(client)
77
+ end
78
+ end
79
+
80
+ context "with execution of task is failed" do
81
+ it "should print error message" do
82
+ Mycmd::Client.should_receive(:execute_task).and_raise("some error")
83
+ expect(
84
+ capture(:stdout){
85
+ Mycmd::CLI.start(args)
86
+ }.chomp
87
+ ).to eq("some error")
88
+ end
89
+ end
64
90
  end
65
91
 
66
- it "should call Client#print" do
67
- client.should_receive(:print)
92
+ context "with list option" do
93
+ let(:configuration) {create_configuration_mock}
94
+ let(:args) {["tasks", "-l"]}
95
+
96
+ before {Mycmd::Configuration.should_receive(:new).and_return(configuration)}
97
+
98
+ it "should print message if tasks is not registered" do
99
+ configuration.should_receive(:tasks).and_return(nil)
100
+ expect(
101
+ capture(:stdout){
102
+ Mycmd::CLI.start(args)
103
+ }.chomp
104
+ ).to eq("task is not registered")
105
+ end
106
+
107
+ it "should print tasks" do
108
+ configuration.stub(:tasks).and_return({"key" => "value"})
109
+ expect(
110
+ capture(:stdout){
111
+ Mycmd::CLI.start(args)
112
+ }.chomp
113
+ ).to eq("key:\tvalue")
114
+ end
68
115
  end
69
116
  end
70
117
  end
@@ -82,6 +82,16 @@ describe Mycmd::Client do
82
82
  end
83
83
  end
84
84
 
85
+ describe "#command" do
86
+ before do
87
+ Mycmd::Configuration.stub(:new).and_return(create_configuration_mock)
88
+ end
89
+
90
+ it "should" do
91
+ expect(client.command).to eq("mysql -hlocalhost -uroot -psecret -P3306 sample")
92
+ end
93
+ end
94
+
85
95
  describe ".#method_missing" do
86
96
  before do
87
97
  connection.stub(:query)
@@ -13,6 +13,27 @@ module SpecUtils
13
13
  result
14
14
  end
15
15
 
16
+ def create_configuration_mock
17
+ configuration = double("Mycmd::Configuration Mock").as_null_object
18
+ configuration.stub(:to_hash).and_return(setting_variables)
19
+ configuration.stub(:connect).and_return(create_connection_mock)
20
+ configuration
21
+ end
22
+
23
+ def create_connection_mock
24
+ double("Mysql2::Client Mock").as_null_object
25
+ end
26
+
27
+ def setting_variables
28
+ {
29
+ host: "localhost",
30
+ username: "root",
31
+ password: "secret",
32
+ port: 3306,
33
+ database: "sample"
34
+ }
35
+ end
36
+
16
37
  def create_variables
17
38
  {
18
39
  innodb_buffer_pool_size: "268435456",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mycmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2013-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -98,19 +98,20 @@ files:
98
98
  - bin/mycmd
99
99
  - lib/mycmd.rb
100
100
  - lib/mycmd/cli.rb
101
+ - lib/mycmd/cli/config_commands.rb
102
+ - lib/mycmd/cli/settings_commands.rb
103
+ - lib/mycmd/cli/status_commands.rb
101
104
  - lib/mycmd/client.rb
102
- - lib/mycmd/clis/config_commands.rb
103
- - lib/mycmd/clis/settings_commands.rb
104
- - lib/mycmd/clis/status_commands.rb
105
105
  - lib/mycmd/configuration.rb
106
106
  - lib/mycmd/printer.rb
107
+ - lib/mycmd/sql.rb
107
108
  - lib/mycmd/version.rb
108
109
  - mycmd.gemspec
110
+ - spec/mycmd/cli/config_commands_spec.rb
111
+ - spec/mycmd/cli/settings_commands_spec.rb
112
+ - spec/mycmd/cli/status_commands_spec.rb
109
113
  - spec/mycmd/cli_spec.rb
110
114
  - spec/mycmd/client_spec.rb
111
- - spec/mycmd/clis/config_commands_spec.rb
112
- - spec/mycmd/clis/settings_commands_spec.rb
113
- - spec/mycmd/clis/status_commands_spec.rb
114
115
  - spec/mycmd/configuration_spec.rb
115
116
  - spec/mycmd/printer_spec.rb
116
117
  - spec/spec_helper.rb
@@ -135,16 +136,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
136
  version: '0'
136
137
  requirements: []
137
138
  rubyforge_project:
138
- rubygems_version: 2.1.5
139
+ rubygems_version: 2.1.6
139
140
  signing_key:
140
141
  specification_version: 4
141
142
  summary: MySQL command line tool
142
143
  test_files:
144
+ - spec/mycmd/cli/config_commands_spec.rb
145
+ - spec/mycmd/cli/settings_commands_spec.rb
146
+ - spec/mycmd/cli/status_commands_spec.rb
143
147
  - spec/mycmd/cli_spec.rb
144
148
  - spec/mycmd/client_spec.rb
145
- - spec/mycmd/clis/config_commands_spec.rb
146
- - spec/mycmd/clis/settings_commands_spec.rb
147
- - spec/mycmd/clis/status_commands_spec.rb
148
149
  - spec/mycmd/configuration_spec.rb
149
150
  - spec/mycmd/printer_spec.rb
150
151
  - spec/spec_helper.rb
@@ -1,27 +0,0 @@
1
- # coding: utf-8
2
-
3
- module Mycmd
4
- class ConfigCommands < Thor
5
- namespace :config
6
-
7
- desc "which", "which will find config file"
8
- def which
9
- conf = Configuration.config_find
10
- puts conf.nil? ? "config not found" : conf
11
- end
12
-
13
- desc "cat", "cat will print configuration"
14
- def cat
15
- conf = Configuration.config_find
16
- raise "config not found" if conf.nil?
17
- File.open(conf, "r").each {|line| puts line}
18
- end
19
-
20
- desc "edit", "edit will edit configuration"
21
- def edit
22
- conf = Configuration.config_find
23
- raise "config not found" if conf.nil?
24
- Kernel.system("#{ENV['EDITOR']} #{conf}")
25
- end
26
- end
27
- end
@@ -1,43 +0,0 @@
1
- # coding: utf-8
2
-
3
- module Mycmd
4
- class StatusCommands < Thor
5
- namespace :status
6
-
7
- desc "size", "size will print database/table size"
8
- option :database, aliases: "-d", desc: "target database."
9
- def size
10
- if options["database"].nil?
11
- sql = "SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T"
12
- else
13
- sql = "SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{options['database']}' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = '#{options['database']}'"
14
- end
15
- Client.query(sql).print
16
- end
17
-
18
- desc "qcache_hit_rate", "qcache_hit_rate will print query cache hit rate"
19
- def qcache_hit_rate
20
- client = Configuration.connect
21
- rate = client.query("SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS rate")
22
- print_rate(rate, 20)
23
- end
24
-
25
- desc "innodb_buffer_hit_rate", "innodb_buffer_hit_rate will print buffer hit rate"
26
- def innodb_buffer_hit_rate
27
- client = Configuration.connect
28
- rate = client.query("SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS rate")
29
- print_rate(rate, 90)
30
- end
31
-
32
- private
33
- def print_rate(rate, threshold)
34
- if rate.nil?
35
- rate = "\e[31munknown\e[m"
36
- else
37
- rate = rate.first["rate"]
38
- rate = rate >= threshold ? "\e[32m#{rate} %\e[m" : "\e[31m#{rate} %\e[m"
39
- end
40
- puts rate
41
- end
42
- end
43
- end
@@ -1,113 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mycmd::StatusCommands do
4
- let(:conn_mock) {double("connection mock")}
5
-
6
- describe "#size" do
7
- let(:client) {double("Mycmd::Client Mock").as_null_object}
8
-
9
- context "with not arguments" do
10
- let(:args) {["size"]}
11
- let(:sql) {"SELECT T.TABLE_SCHEMA, CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T GROUP BY T.TABLE_SCHEMA UNION SELECT 'all_databases', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T"}
12
-
13
- after {Mycmd::StatusCommands.start(args)}
14
-
15
- it "should call Mycmd::Client.#query" do
16
- Mycmd::Client.should_receive(:query).with(sql).and_return(client)
17
- end
18
-
19
- it "should call Mycmd::Client#print" do
20
- client.should_receive(:print)
21
- Mycmd::Client.stub(:query).and_return(client)
22
- end
23
- end
24
-
25
- context "with arguments" do
26
- let(:args) {["size", "-d", "some_db"]}
27
- let(:sql) {"SELECT T.TABLE_NAME, CAST(((T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db' UNION SELECT 'all_tables', CAST((SUM(T.DATA_LENGTH+T.INDEX_LENGTH)/1024/1024) AS CHAR) FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_SCHEMA = 'some_db'"}
28
-
29
- after {Mycmd::StatusCommands.start(args)}
30
-
31
- it "should call Mycmd::Client.#query" do
32
- Mycmd::Client.should_receive(:query).with(sql).and_return(client)
33
- end
34
-
35
- it "should call Mycmd::Client#print" do
36
- client.should_receive(:print)
37
- Mycmd::Client.stub(:query).and_return(client)
38
- end
39
- end
40
- end
41
-
42
- describe "#qcache_hit_rate" do
43
- let(:args) {["qcache_hit_rate"]}
44
-
45
- before do
46
- conn_mock.stub(:query).and_return(double.as_null_object)
47
- Mycmd::Configuration.stub(:connect).and_return(conn_mock)
48
- end
49
-
50
- after do
51
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).not_to be_nil
52
- end
53
-
54
- it "should call Configuration.#connect" do
55
- Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
56
- end
57
-
58
- it "should output the query cache hit rate" do
59
- conn_mock.should_receive(:query).with("SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS rate").and_return(double.as_null_object)
60
- end
61
-
62
- it "should output red color of char if rate is unknown" do
63
- conn_mock.should_receive(:query).and_return(nil)
64
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31munknown\e[m\n")
65
- end
66
-
67
- it "should output red color of char if rate < 20" do
68
- conn_mock.should_receive(:query).and_return([{'rate' => 10}])
69
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31m10 %\e[m\n")
70
- end
71
-
72
- it "should output green color of char if rate >= 20" do
73
- conn_mock.should_receive(:query).and_return([{'rate' => 20}])
74
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[32m20 %\e[m\n")
75
- end
76
- end
77
-
78
- describe "#innodb_buffer_hit_rate" do
79
- let(:args) {["innodb_buffer_hit_rate"]}
80
-
81
- before do
82
- conn_mock.stub(:query).and_return(double.as_null_object)
83
- Mycmd::Configuration.stub(:connect).and_return(conn_mock)
84
- end
85
-
86
- after do
87
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).not_to be_nil
88
- end
89
-
90
- it "should call Configuration.#connect" do
91
- Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
92
- end
93
-
94
- it "should output the innodb buffer hit rate" do
95
- conn_mock.should_receive(:query).with("SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS rate").and_return(double.as_null_object)
96
- end
97
-
98
- it "should output red color of char if rate is unknown" do
99
- conn_mock.should_receive(:query).and_return(nil)
100
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31munknown\e[m\n")
101
- end
102
-
103
- it "should output red color of char if rate < 90" do
104
- conn_mock.should_receive(:query).and_return([{'rate' => 80}])
105
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31m80 %\e[m\n")
106
- end
107
-
108
- it "should output green color of char if rate >= 90" do
109
- conn_mock.should_receive(:query).and_return([{'rate' => 90}])
110
- expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[32m90 %\e[m\n")
111
- end
112
- end
113
- end