rails_mysql 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +12 -2
- data/lib/rails_mysql.rb +1 -0
- data/lib/rails_mysql/cli_command.rb +8 -1
- data/lib/rails_mysql/database_config.rb +5 -6
- data/lib/rails_mysql/dump_command.rb +9 -2
- data/lib/rails_mysql/version.rb +1 -1
- data/spec/lib/rails_mysql/cli_command_spec.rb +36 -1
- data/spec/lib/rails_mysql/dump_command_spec.rb +48 -23
- data/spec/lib/tasks/mysql_rake_spec.rb +20 -8
- data/spec/spec_helper.rb +26 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7f2e583d64423a37a54925b814b1beafba71e63
|
4
|
+
data.tar.gz: e3dad132c87c76685f8ba7f1ca6be4c89449fbac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cce39e097ac6d4751c84e9262766ebdb30d0a0edb46319e52447735c43170762d8ca4f5ec3d20a5ea232ebfcee0159a7a4edb30b6445a175af82d92587e736a
|
7
|
+
data.tar.gz: a412eb9afd695a1e8a105029f29484cd0bfd3be562e4765214bb6ceb97589ed32f46ed7024baad96e96e5f85dc8d5364c4c9729915e3ece8a1692f4a9d686d5d
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -18,11 +18,21 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
After installation into a Rails project, it provides two rake tasks for your
|
22
|
+
use.
|
23
|
+
|
24
|
+
`rake mysql:cli` will execute the command line cli program connecting to
|
25
|
+
the mysql database for your current environment
|
26
|
+
|
27
|
+
`rake mysql:dump` will execute a mysqldump against your configured database.
|
28
|
+
Dump files are gzipped and stored in in the db/ folder.
|
29
|
+
|
30
|
+
Configurations are read from the standard `config/database.yml' file.
|
31
|
+
|
22
32
|
|
23
33
|
## Contributing
|
24
34
|
|
25
|
-
1. Fork it ( https://github.com/
|
35
|
+
1. Fork it ( https://github.com/akatakritos/rails_mysql/fork )
|
26
36
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
37
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
38
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/rails_mysql.rb
CHANGED
@@ -5,7 +5,14 @@ module RailsMysql
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def command
|
8
|
-
|
8
|
+
cmd_parts = []
|
9
|
+
cmd_parts << "-h\"#{config.host}\"" if config.host
|
10
|
+
cmd_parts << "-u\"#{config.username}\"" if config.username
|
11
|
+
cmd_parts << "-p\"#{config.password}\"" if config.password
|
12
|
+
cmd_parts << "-P\"#{config.port}\"" if config.port
|
13
|
+
cmd_parts << "-D\"#{config.database}\"" if config.database
|
14
|
+
|
15
|
+
%Q{mysql #{cmd_parts.join(' ')}}.strip
|
9
16
|
end
|
10
17
|
|
11
18
|
private
|
@@ -1,5 +1,4 @@
|
|
1
1
|
module RailsMysql
|
2
|
-
class ConfigurationError < StandardError; end
|
3
2
|
class DatabaseConfig
|
4
3
|
|
5
4
|
def self.from_yaml(env, file='config/database.yml')
|
@@ -11,11 +10,11 @@ module RailsMysql
|
|
11
10
|
def initialize(options)
|
12
11
|
raise ConfigurationError, "Not a mysql adapter" unless options["adapter"] =~ /mysql/
|
13
12
|
|
14
|
-
@host = options
|
15
|
-
@username = options
|
16
|
-
@password = options
|
17
|
-
@port = options
|
18
|
-
@database = options
|
13
|
+
@host = options['host']
|
14
|
+
@username = options['username']
|
15
|
+
@password = options['password']
|
16
|
+
@port = options['port']
|
17
|
+
@database = options['database']
|
19
18
|
end
|
20
19
|
|
21
20
|
end
|
@@ -2,15 +2,22 @@ module RailsMysql
|
|
2
2
|
class DumpCommand
|
3
3
|
|
4
4
|
def initialize(config)
|
5
|
+
raise RailsMysql::ConfigurationError, "mysqldump requires a database" unless config.database
|
5
6
|
@config = config
|
6
7
|
end
|
7
8
|
|
8
9
|
def command
|
9
|
-
|
10
|
+
cmd_parts = []
|
11
|
+
cmd_parts << "-h \"#{config.host}\"" if config.host
|
12
|
+
cmd_parts << "-P \"#{config.port}\"" if config.port
|
13
|
+
cmd_parts << "-u \"#{config.username}\"" if config.username
|
14
|
+
cmd_parts << "-p\"#{config.password}\"" if config.password
|
15
|
+
|
16
|
+
"mysqldump #{cmd_parts.join(' ')} \"#{config.database}\" | gzip > #{filename}"
|
10
17
|
end
|
11
18
|
|
12
19
|
def filename
|
13
|
-
"db/#{Time.now.utc.iso8601}.sql.gz"
|
20
|
+
"db/#{config.database}-#{Time.now.utc.iso8601}.sql.gz"
|
14
21
|
end
|
15
22
|
|
16
23
|
private
|
data/lib/rails_mysql/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe RailsMysql::CliCommand do
|
4
|
-
describe '
|
4
|
+
describe 'command' do
|
5
5
|
before { Kernel.stub(:exec) }
|
6
6
|
let(:command) { RailsMysql::CliCommand.new(config) }
|
7
7
|
let(:config) { double(:host => "HOST",
|
@@ -9,9 +9,44 @@ describe RailsMysql::CliCommand do
|
|
9
9
|
:password => "PASSWORD",
|
10
10
|
:port => "PORT",
|
11
11
|
:database => "DATABASE") }
|
12
|
+
|
13
|
+
def cli_without(*args)
|
14
|
+
args.each do |arg|
|
15
|
+
config.stub(arg) { nil }
|
16
|
+
end
|
17
|
+
RailsMysql::CliCommand.new(config)
|
18
|
+
end
|
19
|
+
|
12
20
|
it 'Kernel.execs the correct parameters' do
|
13
21
|
expect(command.command).to eq(%Q{mysql -h"HOST" -u"USERNAME" -p"PASSWORD" -P"PORT" -D"DATABASE"})
|
14
22
|
end
|
23
|
+
|
24
|
+
describe 'optional configurations' do
|
25
|
+
it 'allows host to be optional' do
|
26
|
+
expect(cli_without(:host).command).to_not include("-h")
|
27
|
+
end
|
28
|
+
it 'allows username to be optional' do
|
29
|
+
expect(cli_without(:username).command).to_not include("-u")
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'allows port to be optional' do
|
33
|
+
expect(cli_without(:port).command).to_not include("-P")
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'allows database to be optional' do
|
37
|
+
expect(cli_without(:database).command).to_not include("-D")
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'executes just mysql with no options' do
|
41
|
+
expect(cli_without(
|
42
|
+
:database,
|
43
|
+
:username,
|
44
|
+
:password,
|
45
|
+
:port,
|
46
|
+
:host).command).to eq "mysql"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
15
50
|
end
|
16
51
|
end
|
17
52
|
|
@@ -1,20 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'optparse'
|
3
3
|
|
4
|
-
def parse_options(cmd)
|
5
|
-
splitted_cmd = cmd.shellsplit
|
6
|
-
options = {}
|
7
|
-
OptionParser.new do |opts|
|
8
|
-
opts.on("-h host") { |h| options[:host] = h }
|
9
|
-
opts.on("-u username") { |u| options[:username] = u }
|
10
|
-
opts.on("-p password") { |p| options[:password] = p }
|
11
|
-
opts.on("-P port") { |p| options[:port] = p }
|
12
|
-
end.parse!(splitted_cmd)
|
13
|
-
|
14
|
-
options[:cmd] = splitted_cmd.first
|
15
|
-
options[:args] = splitted_cmd[1..-1]
|
16
|
-
options
|
17
|
-
end
|
18
4
|
describe RailsMysql::DumpCommand do
|
19
5
|
let(:config) {
|
20
6
|
double(
|
@@ -30,29 +16,64 @@ describe RailsMysql::DumpCommand do
|
|
30
16
|
cmd = RailsMysql::DumpCommand.new(config).command
|
31
17
|
|
32
18
|
options = parse_options(cmd[/^[^\|]+/])
|
33
|
-
expect(options[:cmd]).to
|
34
|
-
expect(options[
|
35
|
-
expect(options[
|
36
|
-
expect(options[
|
37
|
-
expect(options[
|
38
|
-
expect(options[:args]).to
|
19
|
+
expect(options[:cmd]).to eq "mysqldump"
|
20
|
+
expect(options["-h"]).to eq config.host
|
21
|
+
expect(options["-P"]).to eq config.port
|
22
|
+
expect(options["-u"]).to eq config.username
|
23
|
+
expect(options["-p"]).to eq config.password
|
24
|
+
expect(options[:args]).to eq [config.database]
|
25
|
+
|
26
|
+
end
|
39
27
|
|
28
|
+
it 'doesnt have a space between -p and the password' do
|
29
|
+
cmd = described_class.new(config).command
|
30
|
+
expect(cmd).to_not match "\s-p\s"
|
31
|
+
end
|
32
|
+
|
33
|
+
def dump_without(*args)
|
34
|
+
args.each do |arg|
|
35
|
+
config.stub(arg){ nil }
|
36
|
+
end
|
37
|
+
RailsMysql::DumpCommand.new(config)
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'optional arguments' do
|
41
|
+
it 'doesnt require host' do
|
42
|
+
expect(dump_without(:host).command).to_not include "-h"
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'doesnt require username' do
|
46
|
+
expect(dump_without(:username).command).to_not include "-u"
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'doesnt require password' do
|
50
|
+
expect(dump_without(:password).command).to_not include "-p"
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'doesnt require port' do
|
54
|
+
expect(dump_without(:port).command).to_not include "-P"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'throws when it doesnt have a database' do
|
59
|
+
conf = double(:database => nil)
|
60
|
+
expect{ RailsMysql::DumpCommand.new(conf) }.to raise_error(RailsMysql::ConfigurationError)
|
40
61
|
end
|
41
62
|
|
42
63
|
it 'pipes through gzip' do
|
43
64
|
cmd = RailsMysql::DumpCommand.new(config).command
|
44
|
-
expect(cmd).to
|
65
|
+
expect(cmd).to pipe_to("gzip")
|
45
66
|
end
|
46
67
|
|
47
68
|
it 'cats out to its filename' do
|
48
69
|
dumper = RailsMysql::DumpCommand.new(config)
|
49
70
|
cmd = dumper.command
|
50
71
|
expect(cmd).to match(/\s>\s+#{Regexp.escape(dumper.filename)}$/)
|
51
|
-
|
52
72
|
end
|
53
73
|
|
54
74
|
describe 'filename' do
|
55
75
|
let(:dumper) { RailsMysql::DumpCommand.new(config) }
|
76
|
+
|
56
77
|
it 'is in the db folder' do
|
57
78
|
expect(dumper.filename).to start_with("db/")
|
58
79
|
end
|
@@ -66,7 +87,11 @@ describe RailsMysql::DumpCommand do
|
|
66
87
|
end
|
67
88
|
|
68
89
|
it 'is in utc' do
|
69
|
-
expect(Time.parse(dumper.filename[/db
|
90
|
+
expect(Time.parse(dumper.filename[/db\/.*-(.*?)\.sql\.gz/, 1])).to be_utc
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'has the db name' do
|
94
|
+
expect(dumper.filename).to include "DATABASE"
|
70
95
|
end
|
71
96
|
end
|
72
97
|
|
@@ -21,7 +21,6 @@ ensure
|
|
21
21
|
Dir.chdir old_path
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
24
|
describe 'rake tasks' do
|
26
25
|
|
27
26
|
before(:all) do
|
@@ -34,11 +33,21 @@ describe 'rake tasks' do
|
|
34
33
|
before { Rails.stub(:env) { "development" } }
|
35
34
|
|
36
35
|
it 'calls exec with the correct params' do
|
36
|
+
expect(RakeFileUtils).to receive(:sh) do |cmd|
|
37
|
+
opts = parse_options(cmd)
|
38
|
+
expect(opts[:cmd]).to eq 'mysql'
|
39
|
+
expect(opts["-h"]).to eq 'HOST'
|
40
|
+
expect(opts["-u"]).to eq "USER"
|
41
|
+
expect(opts["-p"]).to eq "PASSWORD"
|
42
|
+
expect(opts["-P"]).to eq "PORT"
|
43
|
+
expect(opts["-D"]).to eq "DATABASE"
|
44
|
+
end
|
45
|
+
|
37
46
|
with_fixture("default") do
|
38
47
|
rake 'mysql:cli'
|
39
48
|
end
|
40
49
|
|
41
|
-
expect(RakeFileUtils).to have_received(:sh).with("mysql -h\"HOST\" -u\"USER\" -p\"PASSWORD\" -P\"PORT\" -D\"DATABASE\"");
|
50
|
+
#expect(RakeFileUtils).to have_received(:sh).with("mysql -h\"HOST\" -u\"USER\" -p\"PASSWORD\" -P\"PORT\" -D\"DATABASE\"");
|
42
51
|
|
43
52
|
end
|
44
53
|
end
|
@@ -50,12 +59,15 @@ describe 'rake tasks' do
|
|
50
59
|
it 'calls exec with the correct params' do
|
51
60
|
|
52
61
|
expect(RakeFileUtils).to receive(:sh) do |cmd|
|
53
|
-
|
54
|
-
|
55
|
-
expect(cmd).to
|
56
|
-
expect(
|
57
|
-
expect(
|
58
|
-
expect(
|
62
|
+
opts = parse_options(cmd)
|
63
|
+
|
64
|
+
expect(opts[:cmd]).to eq 'mysqldump'
|
65
|
+
expect(opts["-h"]).to eq 'HOST'
|
66
|
+
expect(opts["-u"]).to eq "USER"
|
67
|
+
expect(opts["-p"]).to eq "PASSWORD"
|
68
|
+
expect(opts["-P"]).to eq "PORT"
|
69
|
+
|
70
|
+
expect(cmd).to pipe_to "gzip"
|
59
71
|
end
|
60
72
|
|
61
73
|
with_fixture("default") do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,28 @@
|
|
1
1
|
require 'rails_mysql'
|
2
2
|
require 'rake'
|
3
|
+
|
4
|
+
def parse_options(cmd)
|
5
|
+
splitted_cmd = cmd.shellsplit
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.on("-h host") { |h| options["-h"] = h }
|
9
|
+
opts.on("-u username") { |u| options["-u"] = u }
|
10
|
+
opts.on("-p password") { |p| options["-p"] = p }
|
11
|
+
opts.on("-P port") { |p| options["-P"] = p }
|
12
|
+
opts.on("-D database") { |d| options["-D"] = d }
|
13
|
+
end.parse!(splitted_cmd)
|
14
|
+
|
15
|
+
options[:cmd] = splitted_cmd.first
|
16
|
+
options[:args] = splitted_cmd[1..-1]
|
17
|
+
options
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# expect(command).to pipe_to "gzip"
|
22
|
+
# => asserts that command has basically "| gzip " in it somewhere
|
23
|
+
RSpec::Matchers.define :pipe_to do |command|
|
24
|
+
match do |actual|
|
25
|
+
actual.match /\|\s*#{Regexp.quote(command)}\b/
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_mysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Burke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -75,6 +75,7 @@ extra_rdoc_files: []
|
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
77
|
- ".rspec"
|
78
|
+
- CHANGELOG.md
|
78
79
|
- Gemfile
|
79
80
|
- Guardfile
|
80
81
|
- LICENSE.txt
|