rails_mysql 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c456d2132355527a87316b1e94910d46fa97059e
4
+ data.tar.gz: 468297fc3a0c23df03dae5cdd74d833586d9677c
5
+ SHA512:
6
+ metadata.gz: b6317b5b2f848ed031fa2484c7acb6b3446befeda9d3008f914d3990adc356b05b10d2646a7d15716a9fceddd7e209b74c3b170616a151d5d56b258f52546af7
7
+ data.tar.gz: 188a8e73f59f3e249908c5ea64530731e9b8be0d100a0e2257de3662f0dd07ecbb0b3af5a3ce91bc4b2bc395662aa1f965b0e20bdac5a44f111855b4f258999b
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ TODO
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_mysql.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ watch('lib/tasks/mysql.rake') { "spec/lib/tasks/mysql_rake_spec.rb" }
9
+ end
10
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matt Burke
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RailsMysql
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails_mysql'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails_mysql
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/rails_mysql/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,10 @@
1
+ require "rails_mysql/version"
2
+ require "rails_mysql/database_config"
3
+ require 'rails_mysql/cli_command'
4
+ require 'rails_mysql/dump_command'
5
+
6
+ require 'rails_mysql/railtie' if defined?(Rails)
7
+
8
+ module RailsMysql
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,17 @@
1
+ module RailsMysql
2
+ class CliCommand
3
+ def initialize(config)
4
+ @config = config
5
+ end
6
+
7
+ def command
8
+ %Q{mysql -h"#{config.host}" -u"#{config.username}" -p"#{config.password}" -P"#{config.port}" -D"#{config.database}"}
9
+ end
10
+
11
+ private
12
+ def config
13
+ @config
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ module RailsMysql
2
+ class ConfigurationError < StandardError; end
3
+ class DatabaseConfig
4
+
5
+ def self.from_yaml(env, file='config/database.yml')
6
+ self.new(YAML.load_file(file).fetch(env))
7
+ end
8
+
9
+ attr_reader :host, :username, :password, :port, :database
10
+
11
+ def initialize(options)
12
+ raise ConfigurationError, "Not a mysql adapter" unless options["adapter"] =~ /mysql/
13
+
14
+ @host = options.fetch('host', 'localhost')
15
+ @username = options.fetch('username', 'root')
16
+ @password = options.fetch('password', 'root')
17
+ @port = options.fetch('port', '3306')
18
+ @database = options.fetch('database', 'db')
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module RailsMysql
2
+ class DumpCommand
3
+
4
+ def initialize(config)
5
+ @config = config
6
+ end
7
+
8
+ def command
9
+ "mysqldump -h \"#{config.host}\" -P \"#{config.port}\" -u \"#{config.username}\" -p \"#{config.password}\" \"#{config.database}\" | gzip > #{filename}"
10
+ end
11
+
12
+ def filename
13
+ "db/#{Time.now.utc.iso8601}.sql.gz"
14
+ end
15
+
16
+ private
17
+ def config
18
+ @config
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+ module RailsMysql
3
+ class Railtie < Rails::Railtie
4
+ railtie_name :rails_mysql
5
+
6
+ rake_tasks do
7
+ load "tasks/mysql.rake"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module RailsMysql
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'yaml'
2
+ namespace :mysql do
3
+ desc "opens the cli"
4
+ task :cli do
5
+ config = RailsMysql::DatabaseConfig.from_yaml(Rails.env)
6
+ RakeFileUtils.sh RailsMysql::CliCommand.new(config).command
7
+ end
8
+
9
+ desc "dumps to a timestamped file"
10
+ task :dump do
11
+ config = RailsMysql::DatabaseConfig.from_yaml(Rails.env)
12
+ RakeFileUtils.sh RailsMysql::DumpCommand.new(config).command
13
+ end
14
+
15
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_mysql/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_mysql"
8
+ spec.version = RailsMysql::VERSION
9
+ spec.authors = ["Matt Burke"]
10
+ spec.email = ["burkemd1+github@gmail.com"]
11
+ spec.summary = %q{Adds a few mysql tool wrappers as rake tasks.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "rails", "> 3.0"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "guard-rspec"
25
+ end
@@ -0,0 +1,7 @@
1
+ development:
2
+ adapter: mysql2
3
+ host: HOST
4
+ port: PORT
5
+ username: USER
6
+ password: PASSWORD
7
+ database: DATABASE
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsMysql::CliCommand do
4
+ describe 'exec' do
5
+ before { Kernel.stub(:exec) }
6
+ let(:command) { RailsMysql::CliCommand.new(config) }
7
+ let(:config) { double(:host => "HOST",
8
+ :username => "USERNAME",
9
+ :password => "PASSWORD",
10
+ :port => "PORT",
11
+ :database => "DATABASE") }
12
+ it 'Kernel.execs the correct parameters' do
13
+ expect(command.command).to eq(%Q{mysql -h"HOST" -u"USERNAME" -p"PASSWORD" -P"PORT" -D"DATABASE"})
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ include RailsMysql
5
+ describe RailsMysql::DatabaseConfig do
6
+
7
+ let(:database_yml) {
8
+ {
9
+ "development" => {
10
+ "adapter" => "mysql2",
11
+ 'host' => 'HOST',
12
+ 'port' => 'PORT',
13
+ 'username' => 'USER',
14
+ 'password' => 'PASSWORD',
15
+ "database" => "database",
16
+ },
17
+ "production" => {
18
+ "adapter" => "sqlite"
19
+ }
20
+ }
21
+ }
22
+ describe '#from_yaml' do
23
+ before { YAML.stub(:load_file) { database_yml } }
24
+ it 'reads the yaml file' do
25
+ DatabaseConfig.from_yaml("development")
26
+ expect(YAML).to have_received(:load_file).with("config/database.yml")
27
+ end
28
+
29
+ it 'gets the correct environment settings' do
30
+ config = DatabaseConfig.from_yaml("development")
31
+ expect(config.host).to eq database_yml['development']['host']
32
+ expect(config.username).to eq database_yml['development']['username']
33
+ expect(config.password).to eq database_yml['development']['password']
34
+ expect(config.port).to eq database_yml['development']['port']
35
+ expect(config.database).to eq database_yml['development']['database']
36
+ end
37
+
38
+ it 'throws on non-mysql adapter' do
39
+ expect {
40
+ DatabaseConfig.from_yaml("production")
41
+ }.to raise_error ConfigurationError
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ require 'optparse'
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
+ describe RailsMysql::DumpCommand do
19
+ let(:config) {
20
+ double(
21
+ :host => "HOST",
22
+ :database => "DATABASE",
23
+ :username => "USERNAME",
24
+ :password => "PASSWORD",
25
+ :port => "PORT")
26
+ }
27
+
28
+
29
+ it 'returns mysqldump with the right params' do
30
+ cmd = RailsMysql::DumpCommand.new(config).command
31
+
32
+ options = parse_options(cmd[/^[^\|]+/])
33
+ expect(options[:cmd]).to eq "mysqldump"
34
+ expect(options[:host]).to eq config.host
35
+ expect(options[:port]).to eq config.port
36
+ expect(options[:username]).to eq config.username
37
+ expect(options[:password]).to eq config.password
38
+ expect(options[:args]).to eq [config.database]
39
+
40
+ end
41
+
42
+ it 'pipes through gzip' do
43
+ cmd = RailsMysql::DumpCommand.new(config).command
44
+ expect(cmd).to match(/\|\s+gzip\s+>.*$/)
45
+ end
46
+
47
+ it 'cats out to its filename' do
48
+ dumper = RailsMysql::DumpCommand.new(config)
49
+ cmd = dumper.command
50
+ expect(cmd).to match(/\s>\s+#{Regexp.escape(dumper.filename)}$/)
51
+
52
+ end
53
+
54
+ describe 'filename' do
55
+ let(:dumper) { RailsMysql::DumpCommand.new(config) }
56
+ it 'is in the db folder' do
57
+ expect(dumper.filename).to start_with("db/")
58
+ end
59
+
60
+ it 'is a parsable time' do
61
+ expect{Time.parse(dumper.filename[/db\/(.*?)\.sql\.gz/, 1])}.to_not raise_error
62
+ end
63
+
64
+ it 'ends in .sql.gz' do
65
+ expect(dumper.filename).to end_with(".sql.gz")
66
+ end
67
+
68
+ it 'is in utc' do
69
+ expect(Time.parse(dumper.filename[/db\/(.*?)\.sql\.gz/, 1])).to be_utc
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'rails'
3
+
4
+ def rake_path
5
+ [File.expand_path(File.join(%W{ lib tasks }))]
6
+ end
7
+
8
+ def rake(task)
9
+ Rake.application[task].invoke
10
+ end
11
+
12
+ def fixture_path(fixture)
13
+ File.expand_path(File.dirname(__FILE__) + "../../../fixtures/#{fixture}")
14
+ end
15
+
16
+ def with_fixture(fixture)
17
+ old_path = Dir.pwd
18
+ Dir.chdir fixture_path(fixture)
19
+ yield
20
+ ensure
21
+ Dir.chdir old_path
22
+ end
23
+
24
+
25
+ describe 'rake tasks' do
26
+
27
+ before(:all) do
28
+ Rake.application = Rake::Application.new
29
+ Rake.application.rake_require 'mysql', rake_path
30
+ end
31
+
32
+ describe 'rake mysql:cli' do
33
+ before { RakeFileUtils.stub(:sh) }
34
+ before { Rails.stub(:env) { "development" } }
35
+
36
+ it 'calls exec with the correct params' do
37
+ with_fixture("default") do
38
+ rake 'mysql:cli'
39
+ end
40
+
41
+ expect(RakeFileUtils).to have_received(:sh).with("mysql -h\"HOST\" -u\"USER\" -p\"PASSWORD\" -P\"PORT\" -D\"DATABASE\"");
42
+
43
+ end
44
+ end
45
+
46
+ describe 'rake mysql:dump' do
47
+ before { RakeFileUtils.stub(:sh) }
48
+ before { Rails.stub(:env) { "development" } }
49
+
50
+ it 'calls exec with the correct params' do
51
+
52
+ expect(RakeFileUtils).to receive(:sh) do |cmd|
53
+ expect(cmd).to match(/^mysqldump\b/)
54
+ expect(cmd).to match(/-h\s+"HOST"/)
55
+ expect(cmd).to match(/-u\s+"USER"/)
56
+ expect(cmd).to match(/-p\s+"PASSWORD"/)
57
+ expect(cmd).to match(/-P\s+"PORT"/)
58
+ expect(cmd).to match(/|\s+>\s+.*/) # cats to some file
59
+ end
60
+
61
+ with_fixture("default") do
62
+ rake 'mysql:dump'
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,2 @@
1
+ require 'rails_mysql'
2
+ require 'rake'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_mysql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Burke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - burkemd1+github@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/rails_mysql.rb
84
+ - lib/rails_mysql/cli_command.rb
85
+ - lib/rails_mysql/database_config.rb
86
+ - lib/rails_mysql/dump_command.rb
87
+ - lib/rails_mysql/railtie.rb
88
+ - lib/rails_mysql/version.rb
89
+ - lib/tasks/mysql.rake
90
+ - rails_mysql.gemspec
91
+ - spec/fixtures/default/config/database.yml
92
+ - spec/lib/rails_mysql/cli_command_spec.rb
93
+ - spec/lib/rails_mysql/database_config_spec.rb
94
+ - spec/lib/rails_mysql/dump_command_spec.rb
95
+ - spec/lib/tasks/mysql_rake_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: ''
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.2.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Adds a few mysql tool wrappers as rake tasks.
121
+ test_files:
122
+ - spec/fixtures/default/config/database.yml
123
+ - spec/lib/rails_mysql/cli_command_spec.rb
124
+ - spec/lib/rails_mysql/database_config_spec.rb
125
+ - spec/lib/rails_mysql/dump_command_spec.rb
126
+ - spec/lib/tasks/mysql_rake_spec.rb
127
+ - spec/spec_helper.rb