mysql_rake_tasks 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.rdoc +43 -0
- data/Rakefile +12 -0
- data/lib/mysql_rake_tasks/mysql_rake_tasks.rake +12 -0
- data/lib/mysql_rake_tasks/railtie.rb +10 -0
- data/lib/mysql_rake_tasks/tasks.rb +77 -0
- data/lib/mysql_rake_tasks/version.rb +3 -0
- data/lib/mysql_rake_tasks.rb +7 -0
- data/mysql_rake_tasks.gemspec +30 -0
- data/test/tasks_test.rb +116 -0
- metadata +85 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2011 Gregory Ostermayr
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= mysql_rake_tasks
|
2
|
+
|
3
|
+
A collection of rails rake tasks for mysql
|
4
|
+
1. create_users - creates localhost mysql user accounts for each database listing in the database.yml
|
5
|
+
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
1. Add mysql_rake_tasks to your gem file
|
10
|
+
|
11
|
+
gem 'mysql_rake_tasks', '~> 0.0.1'
|
12
|
+
|
13
|
+
2. Run the bundle command
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
== Examples
|
18
|
+
|
19
|
+
=== create_users
|
20
|
+
|
21
|
+
To create mysql users in interactive mode run:
|
22
|
+
|
23
|
+
rake db:mysql:create_users
|
24
|
+
mysql user: root
|
25
|
+
mysql pass:
|
26
|
+
|
27
|
+
You can also specify your root username and password on the command line:
|
28
|
+
|
29
|
+
rake db:mysel:create_users[root,mypassword]
|
30
|
+
|
31
|
+
== License
|
32
|
+
|
33
|
+
MIT
|
34
|
+
|
35
|
+
== Credits
|
36
|
+
|
37
|
+
Author: Gregory Ostermayr gregory.ostermayr@gmail.com
|
38
|
+
|
39
|
+
Contributed code and/or ideas:
|
40
|
+
|
41
|
+
Kevin Woods kwoods@privateergroup.com
|
42
|
+
Travis Herrick
|
43
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
namespace :db do
|
2
|
+
namespace :mysql do
|
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|
|
6
|
+
MysqlRakeTasks::Tasks::create_users(args)
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'mysql2'
|
3
|
+
|
4
|
+
module MysqlRakeTasks
|
5
|
+
class Tasks
|
6
|
+
|
7
|
+
# Parses input args for username and password, if not given
|
8
|
+
# it will prompt the user
|
9
|
+
def self.get_input(*args)
|
10
|
+
if !args[0].nil? then
|
11
|
+
root_user = args[0][:root_user]
|
12
|
+
root_pass = args[0][:pass]
|
13
|
+
end
|
14
|
+
|
15
|
+
if root_user.nil? or root_pass.nil? then
|
16
|
+
$stdout.puts 'mysql user:'
|
17
|
+
root_user = $stdin.gets.chomp
|
18
|
+
|
19
|
+
$stdout.puts 'mysql password:'
|
20
|
+
system 'stty -echo'
|
21
|
+
root_pass = $stdin.gets.chomp
|
22
|
+
system 'stty echo'
|
23
|
+
end
|
24
|
+
|
25
|
+
{:root_user => root_user, :pass => root_pass}
|
26
|
+
end
|
27
|
+
|
28
|
+
# creates user permissions for mysql database for localhost only
|
29
|
+
def self.create_users(args)
|
30
|
+
args = self.get_input(args)
|
31
|
+
@root_user = args[:root_user]
|
32
|
+
@pass = args[:pass]
|
33
|
+
|
34
|
+
# create a mysql user for each listing in database.yml file
|
35
|
+
Rails::configuration.database_configuration.each do |listing|
|
36
|
+
begin
|
37
|
+
@config = listing[1]
|
38
|
+
db = Mysql2::Client.new(
|
39
|
+
:host => 'localhost',
|
40
|
+
:username => @root_user,
|
41
|
+
:password => @pass,
|
42
|
+
:socket => @config['socket'])
|
43
|
+
|
44
|
+
sql = self.create_user_sql(@config)
|
45
|
+
db.query sql
|
46
|
+
$stdout.puts "Created #{@config['username']} on #{@config['database']}\n"
|
47
|
+
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
|
52
|
+
db.close if db
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.create_user_sql(config)
|
58
|
+
if config.nil? then
|
59
|
+
return ""
|
60
|
+
end
|
61
|
+
|
62
|
+
if config['username'].nil? then
|
63
|
+
puts 'Error code: missing username entry'
|
64
|
+
end
|
65
|
+
|
66
|
+
sql = <<-SQL
|
67
|
+
GRANT
|
68
|
+
ALL PRIVILEGES
|
69
|
+
ON #{config['database']}.*
|
70
|
+
TO #{config['username']}@localhost
|
71
|
+
IDENTIFIED BY '#{config['password']}';
|
72
|
+
SQL
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mysql_rake_tasks/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mysql_rake_tasks"
|
7
|
+
s.version = MysqlRakeTasks::VERSION
|
8
|
+
s.authors = ["Gregory Ostermayr"]
|
9
|
+
s.email = ["gregory.ostermayr@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/gregors/mysql_rake_tasks"
|
11
|
+
s.summary = %q{Rake tasks for mysql}
|
12
|
+
s.description = %q{A collection of rails rake tasks for mysql.}
|
13
|
+
s.license = "MIT"
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
|
19
|
+
s.rubyforge_project = "mysql_rake_tasks"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = Dir['test/*.rb']
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
|
26
|
+
# specify any dependencies here; for example:
|
27
|
+
# s.add_development_dependency "rspec"
|
28
|
+
s.add_development_dependency "rake"
|
29
|
+
s.add_runtime_dependency "mysql2"
|
30
|
+
end
|
data/test/tasks_test.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require File.expand_path('../../lib/mysql_rake_tasks/tasks',__FILE__)
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'stringio'
|
5
|
+
require 'mocha'
|
6
|
+
|
7
|
+
class TasksTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_two_args_returns_two_values
|
10
|
+
output = MysqlRakeTasks::Tasks::get_input({:root_user => 'user', :pass => 'pass'})
|
11
|
+
|
12
|
+
assert_equal 2, output.length
|
13
|
+
assert_equal 'user', output[:root_user]
|
14
|
+
assert_equal 'pass', output[:pass]
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_lack_of_args_invokes_cli_interface
|
18
|
+
output = ""
|
19
|
+
screen = io_mock do |input|
|
20
|
+
input.string = "root\nmypassword\n"
|
21
|
+
output = MysqlRakeTasks::Tasks.get_input
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_equal 'mysql user:', screen[0]
|
25
|
+
assert_equal 'mysql password:', screen[1]
|
26
|
+
assert_equal 'root', output[:root_user]
|
27
|
+
assert_equal 'mypassword', output[:pass]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def test_unsuccessful_authentication
|
32
|
+
Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
|
33
|
+
Rails.configuration.stubs(:database_configuration).returns(stub_config)
|
34
|
+
|
35
|
+
screen = io_mock do |input|
|
36
|
+
MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'wrong')
|
37
|
+
end
|
38
|
+
|
39
|
+
assert_equal 'Error code: 1045', screen[0]
|
40
|
+
assert_equal "Error message: Access denied for user 'root'@'localhost' (using password: YES)", screen[1]
|
41
|
+
assert_equal 'Error code: 1045', screen[2]
|
42
|
+
assert_equal "Error message: Access denied for user 'root'@'localhost' (using password: YES)", screen[3]
|
43
|
+
assert_equal 'Error code: 1045', screen[4]
|
44
|
+
assert_equal "Error message: Access denied for user 'root'@'localhost' (using password: YES)", screen[5]
|
45
|
+
end
|
46
|
+
|
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
|
+
screen = io_mock do |input|
|
52
|
+
# :pass needs to be set to mysql root in order to pass
|
53
|
+
MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'myrootpass')
|
54
|
+
end
|
55
|
+
|
56
|
+
assert_equal "Created dev on task_development", screen[0], 'Note: ***check test machine password***'
|
57
|
+
assert_equal "Created test on task_test", screen[1]
|
58
|
+
assert_equal "Created prod on task_production", screen[2]
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_lack_of_user_throws_error
|
62
|
+
config = stub_config
|
63
|
+
config["development"].delete "username"
|
64
|
+
|
65
|
+
Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
|
66
|
+
Rails.configuration.stubs(:database_configuration).returns(config)
|
67
|
+
|
68
|
+
screen = io_mock do |input|
|
69
|
+
# :pass needs to be set to mysql root in order to pass
|
70
|
+
MysqlRakeTasks::Tasks.create_users(:root_user => 'root', :pass => 'myrootpass')
|
71
|
+
end
|
72
|
+
|
73
|
+
assert_equal 'Error code: missing username entry', screen[0], 'Note: ***check test machine password***'
|
74
|
+
assert_equal 'Error code: 1064', screen[1]
|
75
|
+
end
|
76
|
+
|
77
|
+
def stub_config
|
78
|
+
{"development"=>{"adapter"=>"mysql2",
|
79
|
+
"encoding"=>"utf8",
|
80
|
+
"reconnect"=>false,
|
81
|
+
"database"=>"task_development",
|
82
|
+
"pool"=>5,
|
83
|
+
"username"=>"dev",
|
84
|
+
"password"=>"devpassword"},
|
85
|
+
"test"=>{"adapter"=>"mysql2",
|
86
|
+
"encoding"=>"utf8",
|
87
|
+
"reconnect"=>false,
|
88
|
+
"database"=>"task_test",
|
89
|
+
"pool"=>5,
|
90
|
+
"username"=>"test",
|
91
|
+
"password"=>"taskpassword"},
|
92
|
+
"production"=>{"adapter"=>"mysql2",
|
93
|
+
"encoding"=>"utf8",
|
94
|
+
"reconnect"=>false,
|
95
|
+
"database"=>"task_production",
|
96
|
+
"pool"=>5,
|
97
|
+
"username"=>"prod",
|
98
|
+
"password"=>"prodpassword"}}
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def io_mock
|
103
|
+
org_stdin = $stdin
|
104
|
+
org_stdin = $stdout
|
105
|
+
|
106
|
+
$stdin = StringIO.new
|
107
|
+
$stdout = StringIO.new
|
108
|
+
|
109
|
+
yield $stdin
|
110
|
+
|
111
|
+
return $stdout.string.split("\n")
|
112
|
+
ensure
|
113
|
+
$stdin = org_stdin
|
114
|
+
$stdout = org_stdin
|
115
|
+
end
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql_rake_tasks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gregory Ostermayr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-05 00:00:00.000000000 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
17
|
+
requirement: &16396240 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *16396240
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mysql2
|
28
|
+
requirement: &16395820 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *16395820
|
37
|
+
description: A collection of rails rake tasks for mysql.
|
38
|
+
email:
|
39
|
+
- gregory.ostermayr@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files:
|
43
|
+
- LICENSE
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- lib/mysql_rake_tasks.rb
|
52
|
+
- lib/mysql_rake_tasks/mysql_rake_tasks.rake
|
53
|
+
- lib/mysql_rake_tasks/railtie.rb
|
54
|
+
- lib/mysql_rake_tasks/tasks.rb
|
55
|
+
- lib/mysql_rake_tasks/version.rb
|
56
|
+
- mysql_rake_tasks.gemspec
|
57
|
+
- test/tasks_test.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: https://github.com/gregors/mysql_rake_tasks
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project: mysql_rake_tasks
|
80
|
+
rubygems_version: 1.6.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Rake tasks for mysql
|
84
|
+
test_files:
|
85
|
+
- test/tasks_test.rb
|