mysql_rake_tasks 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  A collection of rails rake tasks for mysql
4
4
  1. create_users - creates localhost mysql user accounts for each database listing in the database.yml
5
-
5
+ 2. stats - database stats - table size
6
6
 
7
7
  == Install
8
8
 
9
9
  1. Add mysql_rake_tasks to your gem file
10
10
 
11
- gem 'mysql_rake_tasks', '~> 0.0.1'
11
+ gem 'mysql_rake_tasks', '~> 0.1.0'
12
12
 
13
13
  2. Run the bundle command
14
14
 
@@ -28,6 +28,24 @@ You can also specify your root username and password on the command line:
28
28
 
29
29
  rake db:mysel:create_users[root,mypassword]
30
30
 
31
+ === stats
32
+
33
+ To display database stats:
34
+
35
+ rake db:mysql:stats
36
+ +--------------------------------+---------------+-----------+----------+------------+
37
+ | Table Name | Rows | Data Size | IDX Size | Total Size |
38
+ +--------------------------------+---------------+-----------+----------+------------+
39
+ | fish | 1.41 Million | 93.6 MB | 41.1 MB | 135 MB |
40
+ | birds | 0 | 16 KB | 0 Bytes | 16 KB |
41
+ | cats | 14 | 16 KB | 0 Bytes | 16 KB |
42
+ | schema_migrations | 7 | 16 KB | 0 Bytes | 16 KB |
43
+ | users | 5 | 16 KB | 32 KB | 48 KB |
44
+ +--------------------------------+---------------+-----------+----------+------------+
45
+ | | 135 MB |
46
+ +--------------------------------+---------------+-----------+----------+------------+
47
+ Database: mydb_development MySQL Server Version: 5.1.58
48
+
31
49
  == License
32
50
 
33
51
  MIT
@@ -1,11 +1,15 @@
1
1
  namespace :db do
2
2
  namespace :mysql do
3
3
 
4
- desc "Create MySQL users from database.yml (localhost only). Run without parameters for interactive mode."
4
+ desc "Create MySQL users from database.yml (localhost only). Run without parameters for interactive mode."
5
5
  task :create_users, [:root_user, :pass] => [:environment] do |rake_task, args|
6
6
  MysqlRakeTasks::Tasks::create_users(args)
7
7
  end
8
8
 
9
+ desc "Display MySQL stats"
10
+ task :stats => [:environment] do |rake_task, args|
11
+ MysqlRakeTasks::Tasks::stats
12
+ end
9
13
  end
10
14
  end
11
15
 
@@ -1,6 +1,12 @@
1
1
  require 'rails'
2
2
  require 'mysql2'
3
3
 
4
+ # test unit seems to need these
5
+ require 'active_support'
6
+ require 'action_view'
7
+
8
+ include ActionView::Helpers::NumberHelper
9
+
4
10
  module MysqlRakeTasks
5
11
  class Tasks
6
12
 
@@ -25,7 +31,7 @@ module MysqlRakeTasks
25
31
  {:root_user => root_user, :pass => root_pass}
26
32
  end
27
33
 
28
- # creates user permissions for mysql database for localhost only
34
+ # creates user permissions for mysql database for localhost only
29
35
  def self.create_users(args)
30
36
  args = self.get_input(args)
31
37
  @root_user = args[:root_user]
@@ -48,7 +54,7 @@ module MysqlRakeTasks
48
54
  $stdout.puts "Error code: #{e.errno}"
49
55
  $stdout.puts "Error message: #{e.error}"
50
56
  $stdout.puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
51
- ensure # disconnect from server
57
+ ensure # disconnect from server
52
58
  db.close if db
53
59
  end
54
60
  end
@@ -72,6 +78,63 @@ module MysqlRakeTasks
72
78
  SQL
73
79
  end
74
80
 
81
+ def self.stats
82
+ config = Rails::configuration.database_configuration[Rails.env]
83
+
84
+ begin
85
+ dbh = Mysql2::Client.new( :host => config['host'], :username => config['username'], :password => config['password'])
86
+ sql = stats_query(config['database'])
87
+ result = dbh.query sql
88
+
89
+ print_header
90
+ db_total = 0
91
+ result.each do |row|
92
+ printf "| %30s | %13s | %9s | %8s | %10s |\n",
93
+ row["table_name"].ljust(30),
94
+ number_to_human(row["rows"]).rjust(13),
95
+ number_to_human_size(row["data"]),
96
+ number_to_human_size(row["idx"]),
97
+ number_to_human_size(row["total_size"])
98
+
99
+ db_total += row["total_size"].to_i
100
+ end
101
+
102
+ print_separator
103
+ printf "|%70s | %10s |\n",'', number_to_human_size(db_total)
104
+ print_separator
105
+ puts "Database: #{config['database']} MySQL Server Version: #{dbh.info[:version]}\n"
106
+ puts " "
107
+ rescue Mysql2::Error => e
108
+ puts "Error code: #{e.errno}"
109
+ puts "Error message: #{e.error}"
110
+ puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
111
+ ensure
112
+ dbh.close if dbh
113
+ end
114
+ end
115
+
116
+ def self.print_separator
117
+ puts "+--------------------------------+---------------+-----------+----------+------------+"
118
+ end
119
+
120
+ def self.print_header
121
+ print_separator
122
+ printf "| %30s | %13s | %9s | %8s | %8s |\n",
123
+ "Table Name".ljust(30), "Rows", "Data Size", "IDX Size", "Total Size"
124
+ print_separator
125
+ end
126
+
127
+ def self.stats_query(db_name)
128
+ sql = <<-SQL
129
+ SELECT table_name,
130
+ concat(table_rows) rows,
131
+ concat(data_length) data,
132
+ concat(index_length) idx,
133
+ concat(data_length+index_length) total_size
134
+ FROM information_schema.TABLES
135
+ WHERE table_schema LIKE '#{db_name}'
136
+ ORDER BY table_name;
137
+ SQL
138
+ end
75
139
  end
76
140
  end
77
-
@@ -1,3 +1,3 @@
1
1
  module MysqlRakeTasks
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/test/tasks_test.rb CHANGED
@@ -5,6 +5,10 @@ require 'stringio'
5
5
  require 'mocha'
6
6
 
7
7
  class TasksTest < Test::Unit::TestCase
8
+ def setup
9
+ Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
10
+ Rails.configuration.stubs(:database_configuration).returns(stub_config)
11
+ end
8
12
 
9
13
  def test_two_args_returns_two_values
10
14
  output = MysqlRakeTasks::Tasks::get_input({:root_user => 'user', :pass => 'pass'})
@@ -17,21 +21,18 @@ class TasksTest < Test::Unit::TestCase
17
21
  def test_lack_of_args_invokes_cli_interface
18
22
  output = ""
19
23
  screen = io_mock do |input|
20
- input.string = "root\nmypassword\n"
24
+ input.string = "root\nmyrootpass\n"
21
25
  output = MysqlRakeTasks::Tasks.get_input
22
26
  end
23
27
 
24
28
  assert_equal 'mysql user:', screen[0]
25
29
  assert_equal 'mysql password:', screen[1]
26
30
  assert_equal 'root', output[:root_user]
27
- assert_equal 'mypassword', output[:pass]
31
+ assert_equal 'myrootpass', output[:pass]
28
32
  end
29
33
 
30
34
 
31
35
  def test_unsuccessful_authentication
32
- Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
33
- Rails.configuration.stubs(:database_configuration).returns(stub_config)
34
-
35
36
  screen = io_mock do |input|
36
37
  MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'wrong')
37
38
  end
@@ -45,9 +46,6 @@ class TasksTest < Test::Unit::TestCase
45
46
  end
46
47
 
47
48
  def test_successful_creation
48
- Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
49
- Rails.configuration.stubs(:database_configuration).returns(stub_config)
50
-
51
49
  screen = io_mock do |input|
52
50
  # :pass needs to be set to mysql root in order to pass
53
51
  MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'myrootpass')
@@ -62,7 +60,6 @@ class TasksTest < Test::Unit::TestCase
62
60
  config = stub_config
63
61
  config["development"].delete "username"
64
62
 
65
- Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
66
63
  Rails.configuration.stubs(:database_configuration).returns(config)
67
64
 
68
65
  screen = io_mock do |input|
@@ -74,6 +71,16 @@ class TasksTest < Test::Unit::TestCase
74
71
  assert_equal 'Error code: 1064', screen[1]
75
72
  end
76
73
 
74
+ def test_stats_outputs_header
75
+ screen = io_mock do |input|
76
+ MysqlRakeTasks::Tasks.stats
77
+ end
78
+
79
+ assert_equal '+--------------------------------+---------------+-----------+----------+------------+', screen[0]
80
+ assert_equal '| Table Name | Rows | Data Size | IDX Size | Total Size |', screen[1]
81
+ assert_equal '+--------------------------------+---------------+-----------+----------+------------+', screen[2]
82
+ end
83
+
77
84
  def stub_config
78
85
  {"development"=>{"adapter"=>"mysql2",
79
86
  "encoding"=>"utf8",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_rake_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-05 00:00:00.000000000 -04:00
13
- default_executable:
12
+ date: 2012-01-09 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rake
17
- requirement: &22621080 !ruby/object:Gem::Requirement
16
+ requirement: &5896580 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: 0.9.2
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *22621080
24
+ version_requirements: *5896580
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: mocha
28
- requirement: &22582860 !ruby/object:Gem::Requirement
27
+ requirement: &5892460 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ~>
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: 0.9.12
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *22582860
35
+ version_requirements: *5892460
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: mysql2
39
- requirement: &22582360 !ruby/object:Gem::Requirement
38
+ requirement: &5890820 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,7 +43,7 @@ dependencies:
44
43
  version: 0.2.7
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *22582360
46
+ version_requirements: *5890820
48
47
  description: A collection of rails rake tasks for mysql.
49
48
  email:
50
49
  - gregory.ostermayr@gmail.com
@@ -66,7 +65,6 @@ files:
66
65
  - lib/mysql_rake_tasks/version.rb
67
66
  - mysql_rake_tasks.gemspec
68
67
  - test/tasks_test.rb
69
- has_rdoc: true
70
68
  homepage: https://github.com/gregors/mysql_rake_tasks
71
69
  licenses:
72
70
  - MIT
@@ -88,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  version: '0'
89
87
  requirements: []
90
88
  rubyforge_project: mysql_rake_tasks
91
- rubygems_version: 1.6.2
89
+ rubygems_version: 1.8.10
92
90
  signing_key:
93
91
  specification_version: 3
94
92
  summary: Rake tasks for mysql