mysql_rake_tasks 0.0.3 → 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b08d5ab0c8ee9fd347b874e6a137aa16ebf1bc33a94d9ecc961b5958a3c7a0ac
4
+ data.tar.gz: '02799bc1c94a6bddbec2a795a85355782dc937476571cf428a4dc23b8d3a016a'
5
+ SHA512:
6
+ metadata.gz: 95dada497b356fce73cea69e704357e41cc746b93db227ad4d8cdd7f14ca49e667ee9ed8af55915e3592ef7dc2b3d433b7c222e1f95871c610a19f301e8fdc8a
7
+ data.tar.gz: 507bb3184fa1ade352f24a52aa9c7c255ad3c61a8b4398bf403c3feafd9eac54047c5656a99dfac7a271899efe51d6074b524c37c4efb0e6e6a14e483c9f1de2
File without changes
@@ -1,22 +1,22 @@
1
- = mysql_rake_tasks
1
+ # mysql_rake_tasks
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
+ 2. stats - database stats - table size
5
6
 
6
-
7
- == Install
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
 
15
15
  bundle install
16
16
 
17
- == Examples
17
+ ## Examples
18
18
 
19
- === create_users
19
+ ### create_users
20
20
 
21
21
  To create mysql users in interactive mode run:
22
22
 
@@ -28,17 +28,35 @@ 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
- == License
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
+
49
+ ## License
32
50
 
33
51
  MIT
34
52
 
35
- == Credits
53
+ ## Credits
36
54
 
37
55
  Author: Gregory Ostermayr gregory.ostermayr@gmail.com
38
56
 
39
57
  Contributed code and/or ideas:
40
58
 
41
- Kevin Woods kwoods@privateergroup.com
59
+ Kevin Woods
42
60
 
43
61
  Travis Herrick
44
62
 
data/Rakefile CHANGED
@@ -5,6 +5,7 @@ Bundler::GemHelper.install_tasks
5
5
  task :default => :test
6
6
 
7
7
  require 'rake/testtask'
8
+
8
9
  Rake::TestTask.new do |t|
9
10
  t.test_files = FileList['test/*_test.rb']
10
11
  t.verbose = true
@@ -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."
5
- task :create_users, [:root_user, :pass] => [:environment] do |rake_task, args|
4
+ desc "Create MySQL users from database.yml (localhost only). Run without parameters for interactive mode."
5
+ task :create_users, [:root_user, :pass] 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,77 +1,161 @@
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
 
7
13
  # Parses input args for username and password, if not given
8
14
  # it will prompt the user
9
15
  def self.get_input(*args)
10
- if !args[0].nil? then
16
+ unless args[0].nil?
11
17
  root_user = args[0][:root_user]
12
18
  root_pass = args[0][:pass]
13
19
  end
14
20
 
15
- if root_user.nil? or root_pass.nil? then
21
+ if root_user.nil? or root_pass.nil?
16
22
  $stdout.puts 'mysql user:'
17
23
  root_user = $stdin.gets.chomp
18
24
 
19
- $stdout.puts 'mysql password:'
25
+ $stdout.puts 'mysql password:'
20
26
  system 'stty -echo'
21
27
  root_pass = $stdin.gets.chomp
22
28
  system 'stty echo'
23
29
  end
24
30
 
25
- {:root_user => root_user, :pass => root_pass}
31
+ return root_user, 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
- args = self.get_input(args)
31
- @root_user = args[:root_user]
32
- @pass = args[:pass]
36
+ @root_user, @pass = self.get_input(args)
33
37
 
34
38
  # create a mysql user for each listing in database.yml file
35
39
  Rails::configuration.database_configuration.each do |listing|
40
+ @config = listing[1]
41
+ username = @config['username']
42
+ db_name = @config['database']
43
+
36
44
  begin
37
- @config = listing[1]
38
- db = Mysql2::Client.new(
39
- :host => 'localhost',
40
- :username => @root_user,
41
- :password => @pass,
42
- :socket => @config['socket'])
45
+ db = Mysql2::Client.new( host: 'localhost', username: @root_user, password: @pass)
43
46
 
44
47
  sql = self.create_user_sql(@config)
45
- db.query sql
46
- $stdout.puts "Created #{@config['username']} on #{@config['database']}\n"
48
+ db.query(sql)
49
+
50
+ sql = self.grant_user_sql(@config)
51
+ db.query(sql)
52
+
53
+ $stdout.puts "Created #{username} on #{db_name}\n"
47
54
  rescue Mysql2::Error => e
48
- $stdout.puts "Error code: #{e.errno}"
49
- $stdout.puts "Error message: #{e.error}"
50
- $stdout.puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
51
- ensure # disconnect from server
55
+ error_output(e)
56
+ ensure # disconnect from server
52
57
  db.close if db
53
58
  end
54
59
  end
55
60
  end
56
61
 
57
62
  def self.create_user_sql(config)
58
- if config.nil? then
59
- return ""
63
+ return '' unless config
64
+
65
+ if config['username'].nil?
66
+ puts 'Error code: missing username entry'
60
67
  end
61
68
 
62
- if config['username'].nil? then
69
+ sql = <<-SQL
70
+ CREATE USER '#{config['username']}'@'localhost' IDENTIFIED BY '#{config['password']}';
71
+ SQL
72
+ end
73
+
74
+ def self.grant_user_sql(config)
75
+ return '' unless config
76
+
77
+ if config['username'].nil?
63
78
  puts 'Error code: missing username entry'
64
79
  end
65
80
 
66
- sql = <<-SQL
67
- GRANT
68
- ALL PRIVILEGES
69
- ON #{config['database']}.*
70
- TO #{config['username']}@localhost
71
- IDENTIFIED BY '#{config['password']}';
81
+ sql = <<-SQL
82
+ GRANT ALL ON #{config['database']}.* TO '#{config['username']}'@'localhost';
83
+ SQL
84
+ end
85
+
86
+ def self.stats
87
+ config = Rails::configuration.database_configuration[Rails.env].clone
88
+
89
+ begin
90
+ db = Mysql2::Client.new( host: config['host'], username: config['username'], password: config['password'])
91
+
92
+ db_name = config['database']
93
+ version = db.info[:version]
94
+
95
+ sql = stats_query(db_name)
96
+ result = db.query sql
97
+
98
+ print_header
99
+
100
+ db_total = 0
101
+ result.each do |row|
102
+ print_stat_line(row)
103
+ db_total += row["total_size"].to_i
104
+ end
105
+
106
+ print_footer(db_total, db_name, version)
107
+ rescue Mysql2::Error => e
108
+ error_output(e)
109
+ ensure
110
+ db.close if db
111
+ end
112
+ end
113
+
114
+ def self.print_separator
115
+ puts "+--------------------------------+---------------+-----------+----------+------------+"
116
+ end
117
+
118
+ def self.print_header
119
+ print_separator
120
+ printf "| %30s | %13s | %9s | %8s | %8s |\n",
121
+ "Table Name".ljust(30), "Rows", "Data Size", "IDX Size", "Total Size"
122
+ print_separator
123
+ end
124
+
125
+ def self.print_stat_line(row)
126
+ printf "| %30s | %13s | %9s | %8s | %10s |\n",
127
+ row["table_name"].ljust(30),
128
+ number_to_human(row["table_rows"]).rjust(13),
129
+ number_to_human_size(row["data"]),
130
+ number_to_human_size(row["idx"]),
131
+ number_to_human_size(row["total_size"])
132
+ end
133
+
134
+ def self.print_footer(db_total, db_name, version)
135
+ print_separator
136
+ printf "|%70s | %10s |\n",'', number_to_human_size(db_total)
137
+ print_separator
138
+ puts "Database: #{db_name} MySQL Server Version: #{version}\n"
139
+ puts " "
140
+ end
141
+
142
+ def self.stats_query(db_name)
143
+ sql = <<-SQL
144
+ SELECT table_name,
145
+ concat(table_rows) as table_rows,
146
+ concat(data_length) data,
147
+ concat(index_length) idx,
148
+ concat(data_length+index_length) total_size
149
+ FROM information_schema.TABLES
150
+ WHERE table_schema LIKE '#{db_name}'
151
+ ORDER BY table_name;
72
152
  SQL
73
153
  end
74
154
 
155
+ def self.error_output(e)
156
+ $stdout.puts "Error code: #{e.errno}"
157
+ $stdout.puts "Error message: #{e.error}"
158
+ $stdout.puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
159
+ end
75
160
  end
76
161
  end
77
-
@@ -1,3 +1,3 @@
1
1
  module MysqlRakeTasks
2
- VERSION = "0.0.3"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -12,20 +12,21 @@ Gem::Specification.new do |s|
12
12
  s.description = %q{A collection of rails rake tasks for mysql.}
13
13
  s.license = "MIT"
14
14
  s.extra_rdoc_files = [
15
- "LICENSE",
15
+ "LICENSE.txt",
16
16
  "README.rdoc"
17
17
  ]
18
18
 
19
19
  s.rubyforge_project = "mysql_rake_tasks"
20
20
 
21
21
  s.files = `git ls-files`.split("\n")
22
- s.test_files = Dir['test/*.rb']
22
+ s.test_files = Dir['test/*.rb']
23
23
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
24
  s.require_paths = ["lib"]
25
25
 
26
26
  # specify any dependencies here; for example:
27
27
  # s.add_development_dependency "rspec"
28
- s.add_development_dependency 'rake', '~> 0.9.2'
28
+ s.add_development_dependency 'rake', '~> 13.0'
29
29
  s.add_development_dependency 'mocha', '~> 0.9.12'
30
+ s.add_development_dependency 'test-unit-rails', '~> 6.0'
30
31
  s.add_runtime_dependency 'mysql2', '>= 0.2.7'
31
32
  end
@@ -5,37 +5,37 @@ 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
- output = MysqlRakeTasks::Tasks::get_input({:root_user => 'user', :pass => 'pass'})
14
+ user, pass = MysqlRakeTasks::Tasks::get_input({:root_user => 'user', :pass => 'pass'})
11
15
 
12
- assert_equal 2, output.length
13
- assert_equal 'user', output[:root_user]
14
- assert_equal 'pass', output[:pass]
16
+ assert_equal 'user', user
17
+ assert_equal 'pass', pass
15
18
  end
16
19
 
17
20
  def test_lack_of_args_invokes_cli_interface
18
21
  output = ""
19
22
  screen = io_mock do |input|
20
- input.string = "root\nmypassword\n"
21
- output = MysqlRakeTasks::Tasks.get_input
23
+ input.string = "root\nmyrootpass\n"
24
+ @user, @pass = MysqlRakeTasks::Tasks.get_input
22
25
  end
23
26
 
24
27
  assert_equal 'mysql user:', screen[0]
25
28
  assert_equal 'mysql password:', screen[1]
26
- assert_equal 'root', output[:root_user]
27
- assert_equal 'mypassword', output[:pass]
29
+ assert_equal 'root', @user
30
+ assert_equal 'myrootpass',@pass
28
31
  end
29
32
 
30
33
 
31
34
  def test_unsuccessful_authentication
32
- Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
33
- Rails.configuration.stubs(:database_configuration).returns(stub_config)
34
-
35
35
  screen = io_mock do |input|
36
36
  MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'wrong')
37
37
  end
38
-
38
+
39
39
  assert_equal 'Error code: 1045', screen[0]
40
40
  assert_equal "Error message: Access denied for user 'root'@'localhost' (using password: YES)", screen[1]
41
41
  assert_equal 'Error code: 1045', screen[2]
@@ -45,12 +45,13 @@ class TasksTest < Test::Unit::TestCase
45
45
  end
46
46
 
47
47
  def test_successful_creation
48
- Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
49
- Rails.configuration.stubs(:database_configuration).returns(stub_config)
50
-
51
48
  screen = io_mock do |input|
52
49
  # :pass needs to be set to mysql root in order to pass
53
- MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'myrootpass')
50
+ # mysqladmin --user=root password "myrootpass"
51
+ # create database task_test
52
+ # create database task_production
53
+ # drop user 'test'@'localhost';drop user 'dev'@'localhost';drop user 'prod'@'localhost';
54
+ MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'myrootpass')
54
55
  end
55
56
 
56
57
  assert_equal "Created dev on task_development", screen[0], 'Note: ***check test machine password***'
@@ -59,10 +60,11 @@ class TasksTest < Test::Unit::TestCase
59
60
  end
60
61
 
61
62
  def test_lack_of_user_throws_error
62
- config = stub_config
63
+ config = stub_config.clone
63
64
  config["development"].delete "username"
65
+ config["production"].delete "username"
66
+ config["test"].delete "username"
64
67
 
65
- Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
66
68
  Rails.configuration.stubs(:database_configuration).returns(config)
67
69
 
68
70
  screen = io_mock do |input|
@@ -71,7 +73,17 @@ class TasksTest < Test::Unit::TestCase
71
73
  end
72
74
 
73
75
  assert_equal 'Error code: missing username entry', screen[0], 'Note: ***check test machine password***'
74
- assert_equal 'Error code: 1064', screen[1]
76
+ assert_equal 'Error code: 1396', screen[1]
77
+ end
78
+
79
+ def test_stats_outputs_header
80
+ screen = io_mock do |input|
81
+ MysqlRakeTasks::Tasks.stats
82
+ end
83
+
84
+ assert_equal '+--------------------------------+---------------+-----------+----------+------------+', screen[0]
85
+ assert_equal '| Table Name | Rows | Data Size | IDX Size | Total Size |', screen[1]
86
+ assert_equal '+--------------------------------+---------------+-----------+----------+------------+', screen[2]
75
87
  end
76
88
 
77
89
  def stub_config
@@ -95,7 +107,7 @@ class TasksTest < Test::Unit::TestCase
95
107
  "database"=>"task_production",
96
108
  "pool"=>5,
97
109
  "username"=>"prod",
98
- "password"=>"prodpassword"}}
110
+ "password"=>"prodpassword"}}
99
111
  end
100
112
 
101
113
 
metadata CHANGED
@@ -1,62 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_rake_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gregory Ostermayr
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2011-09-05 00:00:00.000000000 -04:00
13
- default_executable:
11
+ date: 2020-07-18 00:00:00.000000000 Z
14
12
  dependencies:
15
13
  - !ruby/object:Gem::Dependency
16
14
  name: rake
17
- requirement: &22621080 !ruby/object:Gem::Requirement
18
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
19
16
  requirements:
20
- - - ~>
17
+ - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: 0.9.2
19
+ version: '13.0'
23
20
  type: :development
24
21
  prerelease: false
25
- version_requirements: *22621080
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
26
27
  - !ruby/object:Gem::Dependency
27
28
  name: mocha
28
- requirement: &22582860 !ruby/object:Gem::Requirement
29
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.9.12
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *22582860
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.12
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '6.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
37
55
  - !ruby/object:Gem::Dependency
38
56
  name: mysql2
39
- requirement: &22582360 !ruby/object:Gem::Requirement
40
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
41
58
  requirements:
42
- - - ! '>='
59
+ - - ">="
43
60
  - !ruby/object:Gem::Version
44
61
  version: 0.2.7
45
62
  type: :runtime
46
63
  prerelease: false
47
- version_requirements: *22582360
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.7
48
69
  description: A collection of rails rake tasks for mysql.
49
70
  email:
50
71
  - gregory.ostermayr@gmail.com
51
72
  executables: []
52
73
  extensions: []
53
74
  extra_rdoc_files:
54
- - LICENSE
75
+ - LICENSE.txt
55
76
  - README.rdoc
56
77
  files:
57
- - .gitignore
78
+ - ".gitignore"
58
79
  - Gemfile
59
- - LICENSE
80
+ - LICENSE.txt
60
81
  - README.rdoc
61
82
  - Rakefile
62
83
  - lib/mysql_rake_tasks.rb
@@ -66,31 +87,28 @@ files:
66
87
  - lib/mysql_rake_tasks/version.rb
67
88
  - mysql_rake_tasks.gemspec
68
89
  - test/tasks_test.rb
69
- has_rdoc: true
70
90
  homepage: https://github.com/gregors/mysql_rake_tasks
71
91
  licenses:
72
92
  - MIT
73
- post_install_message:
93
+ metadata: {}
94
+ post_install_message:
74
95
  rdoc_options: []
75
96
  require_paths:
76
97
  - lib
77
98
  required_ruby_version: !ruby/object:Gem::Requirement
78
- none: false
79
99
  requirements:
80
- - - ! '>='
100
+ - - ">="
81
101
  - !ruby/object:Gem::Version
82
102
  version: '0'
83
103
  required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
104
  requirements:
86
- - - ! '>='
105
+ - - ">="
87
106
  - !ruby/object:Gem::Version
88
107
  version: '0'
89
108
  requirements: []
90
- rubyforge_project: mysql_rake_tasks
91
- rubygems_version: 1.6.2
92
- signing_key:
93
- specification_version: 3
109
+ rubygems_version: 3.0.8
110
+ signing_key:
111
+ specification_version: 4
94
112
  summary: Rake tasks for mysql
95
113
  test_files:
96
114
  - test/tasks_test.rb