slodd 0.1.1 → 0.1.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.
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ StringLiterals:
2
+ EnforcedStyle: "double_quotes"
3
+ Documentation:
4
+ Enabled: false
5
+ Eval:
6
+ Enabled: false
data/README.md CHANGED
@@ -9,16 +9,20 @@ Examples:
9
9
 
10
10
  ###Loading schema from github:
11
11
 
12
- slodd -g errm/awesome_rails_app -t my-secret-oauth-token -d "awesome_app awesome_test"
12
+ `slodd -g errm/awesome_rails_app -t my-secret-oauth-token -d "awesome_app awesome_test"`
13
13
 
14
14
  or from a specific branch:
15
15
 
16
- slodd -g errm/awesome_rails_app -t my-secret-oauth-token -r my-brillant-branch -d "awesome_app awesome_test"
16
+ `slodd -g errm/awesome_rails_app -t my-secret-oauth-token -r my-brillant-branch -d "awesome_app awesome_test"`
17
17
 
18
18
  ###Loading schema from a file (defaults to db/schema.rb):
19
19
 
20
- slodd -d awesome_test
20
+ `slodd -d awesome_test`
21
21
 
22
22
  or
23
23
 
24
- slodd -f db/second_database.schema.rb -d awesome_test
24
+ `slodd -f db/second_database.schema.rb -d awesome_test`
25
+
26
+ ###Downloading a schema (or any text file) from github
27
+
28
+ `gurl -g errm/awesome_rails_app -t my-secret-oauth-token -r my-fancy-branch`
data/Rakefile CHANGED
@@ -1,5 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
5
+ Rubocop::RakeTask.new(:rubocop)
4
6
  RSpec::Core::RakeTask.new(:spec)
5
- task :default => :spec
7
+
8
+ task :default => [:spec, :rubocop]
data/bin/gurl CHANGED
@@ -1,22 +1,23 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
3
- require 'slodd'
4
- require 'optparse'
4
+ require "slodd"
5
+ require "optparse"
5
6
 
6
7
  OptionParser.new do |opt|
7
- opt.on('-g', '--github USER/REPO', "github repo") do |g|
8
+ opt.on("-g", "--github USER/REPO", "github repo") do |g|
8
9
  Slodd::Config.github = g
9
10
  end
10
11
 
11
- opt.on('-t', '--token TOKEN', "github token") do |t|
12
+ opt.on("-t", "--token TOKEN", "github token") do |t|
12
13
  Slodd::Config.token = t
13
14
  end
14
15
 
15
- opt.on('-r', '--ref REF', "github ref") do |r|
16
+ opt.on("-r", "--ref REF", "github ref") do |r|
16
17
  Slodd::Config.ref = g
17
18
  end
18
19
 
19
- opt.on( '-f', '--file-schema-path PATH', 'Path to schema.rb') do |f|
20
+ opt.on("-f", "--file-schema-path PATH", "Path to schema.rb") do |f|
20
21
  Slodd::Config.path = f
21
22
  end
22
23
  end.parse!
data/bin/slodd CHANGED
@@ -1,42 +1,43 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
3
- require 'slodd'
4
- require 'optparse'
4
+ require "slodd"
5
+ require "optparse"
5
6
 
6
7
  OptionParser.new do |opt|
7
- opt.on('-d', '--database DATABASES', "Database names") do |d|
8
+ opt.on("-d", "--database DATABASES", "Database names") do |d|
8
9
  Slodd::Config.databases = d
9
10
  end
10
11
 
11
- opt.on('-s', '--schema URI', "URI for schema.rb") do |s|
12
+ opt.on("-s", "--schema URI", "URI for schema.rb") do |s|
12
13
  Slodd::Config.url = s
13
14
  end
14
15
 
15
- opt.on('-g', '--github USER/REPO', "github repo") do |g|
16
+ opt.on("-g", "--github USER/REPO", "github repo") do |g|
16
17
  Slodd::Config.github = g
17
18
  end
18
19
 
19
- opt.on('-t', '--token TOKEN', "github token") do |t|
20
+ opt.on("-t", "--token TOKEN", "github token") do |t|
20
21
  Slodd::Config.token = t
21
22
  end
22
23
 
23
- opt.on('-r', '--ref REF', "github ref") do |r|
24
+ opt.on("-r", "--ref REF", "github ref") do |r|
24
25
  Slodd::Config.ref = g
25
26
  end
26
27
 
27
- opt.on( '-f', '--file-schema-path PATH', 'Path to schema.rb') do |f|
28
+ opt.on("-f", "--file-schema-path PATH", "Path to schema.rb") do |f|
28
29
  Slodd::Config.path = f
29
30
  end
30
31
 
31
- opt.on('-u', '--username USERNAME', "MySQL Username") do |u|
32
+ opt.on("-u", "--username USERNAME", "MySQL Username") do |u|
32
33
  Slodd::Config.username = u
33
34
  end
34
35
 
35
- opt.on('-p', '--password PASSWORD', "MySQL Password") do |p|
36
+ opt.on("-p", "--password PASSWORD", "MySQL Password") do |p|
36
37
  Slodd::Config.password = p
37
38
  end
38
39
 
39
- opt.on('-h', '--host HOST', "MySQL Hostname") do |h|
40
+ opt.on("-h", "--host HOST", "MySQL Hostname") do |h|
40
41
  Slodd::Config.host = h
41
42
  end
42
43
  end.parse!
data/lib/slodd/config.rb CHANGED
@@ -1,21 +1,23 @@
1
+ # encoding: utf-8
1
2
  require "active_support/core_ext"
2
3
 
3
4
  module Slodd
4
5
  module Config
5
- mattr_accessor :path, :github, :username, :password, :host, :url, :token, :ref
6
+ mattr_accessor :path, :github, :username, :password,
7
+ :host, :url, :token, :ref
6
8
  mattr_writer :databases
7
9
 
8
10
  def self.defaults
9
11
  self.path = "db/schema.rb"
10
12
  self.username = "root"
11
13
  self.host = "localhost"
14
+ self.databases = nil
12
15
  end
13
16
 
14
17
  defaults
15
18
 
16
19
  def self.databases
17
- return @@databases.split if defined?(@@databases)
18
- []
20
+ @@databases ? @@databases.split : []
19
21
  end
20
22
 
21
23
  def self.database_settings
@@ -47,25 +49,14 @@ module Slodd
47
49
  end
48
50
 
49
51
  def self.attributes
50
- {
51
- owner: owner,
52
- repo: repo,
53
- token: token,
54
- path: path,
55
- ref: ref,
56
- url: url,
57
- }.delete_if { |k, v| v.nil? }
58
- end
59
-
60
- def self.reset
61
- defaults
62
- self.github = nil
63
- self.password = nil
64
- self.url = nil
65
- self.token = nil
66
- self.databases = nil
67
- self.ref = nil
68
- instance_eval { remove_class_variable "@@databases" } if defined?(@@databases)
52
+ {
53
+ owner: owner,
54
+ repo: repo,
55
+ token: token,
56
+ path: path,
57
+ ref: ref,
58
+ url: url,
59
+ }.delete_if { |_, v| v.nil? }
69
60
  end
70
61
  end
71
62
  end
data/lib/slodd/github.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require "open-uri"
2
3
 
3
4
  module Slodd
data/lib/slodd/http.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require "open-uri"
2
3
 
3
4
  module Slodd
data/lib/slodd/local.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Slodd
2
3
  class Local
3
4
  attr_accessor :path
data/lib/slodd/runner.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require "active_record"
2
3
  require "mysql2"
3
4
 
@@ -19,22 +20,32 @@ module Slodd
19
20
  end
20
21
 
21
22
  private
23
+
22
24
  def create_database(database)
23
- options = {charset: 'utf8', collation: 'utf8_unicode_ci'}
25
+ puts "create_database(#{database})"
24
26
 
25
27
  begin
26
28
  ActiveRecord::Base.establish_connection database_settings
27
29
  ActiveRecord::Base.connection.drop_database database
28
30
  ActiveRecord::Base.connection.create_database database, options
29
- ActiveRecord::Base.establish_connection database_settings.merge(database: database)
31
+ ActiveRecord::Base.establish_connection database_settings(database)
30
32
  rescue Mysql2::Error => sqlerr
31
- $stderr.puts sqlerr.error
32
- $stderr.puts "Couldn't create database: #{database} settings: #{database_settings.inspect}, charset: utf8, collation: utf8_unicode_ci"
33
+ error_message(sqlerr, database)
33
34
  end
34
35
  end
35
36
 
36
- def database_settings
37
- Config.database_settings
37
+ def database_settings(database = nil)
38
+ Config.database_settings.merge(database: database)
39
+ end
40
+
41
+ def options
42
+ { charset: "utf8", collation: "utf8_unicode_ci" }
43
+ end
44
+
45
+ def error_message(sqlerr, database)
46
+ $stderr.puts sqlerr.error
47
+ settings = database_settings(database).merge(options).inspect
48
+ $stderr.puts "Couldn't create database with settings: #{settings}"
38
49
  end
39
50
 
40
51
  attr_accessor :schema
data/lib/slodd/version.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Slodd
2
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
3
4
  end
data/lib/slodd.rb CHANGED
@@ -1,10 +1,11 @@
1
+ # encoding: utf-8
1
2
  require "slodd/version"
2
3
 
3
- require 'slodd/github'
4
- require 'slodd/local'
5
- require 'slodd/http'
6
- require 'slodd/config'
7
- require 'slodd/runner'
4
+ require "slodd/github"
5
+ require "slodd/local"
6
+ require "slodd/http"
7
+ require "slodd/config"
8
+ require "slodd/runner"
8
9
 
9
10
  module Slodd
10
11
  end
data/slodd.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["ed.robinson@reevoo.com"]
11
11
  gem.description = %q{Schema Loading On Dependent Databases}
12
12
  gem.summary = %q{Schema Loading On Dependent Databases}
13
- gem.homepage = ""
13
+ gem.homepage = "https://github.com/errm/slodd"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -20,4 +20,6 @@ Gem::Specification.new do |gem|
20
20
  gem.add_dependency('mysql2')
21
21
  gem.add_development_dependency('rspec')
22
22
  gem.add_development_dependency('rake')
23
+ gem.add_development_dependency('simplecov')
24
+ gem.add_development_dependency('rubocop')
23
25
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require "spec_helper"
2
3
 
3
4
  describe Slodd::Config do
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require "spec_helper"
2
3
 
3
4
  describe Slodd::Github do
@@ -19,12 +20,14 @@ describe Slodd::Github do
19
20
  )
20
21
  end
21
22
 
23
+ let(:expected_url) do
24
+ "https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}"
25
+ end
26
+
22
27
  describe "#schema" do
23
28
  it "hits the correct url" do
24
29
  allow(subject).to receive(:open) do |url, _|
25
- expect(url).to eq(
26
- "https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}"
27
- )
30
+ expect(url).to eq(expected_url)
28
31
  schema
29
32
  end
30
33
 
@@ -36,9 +39,7 @@ describe Slodd::Github do
36
39
 
37
40
  it "hits a url including the ref param" do
38
41
  allow(subject).to receive(:open) do |url, _|
39
- expect(url).to eq(
40
- "https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}?ref=#{ref}"
41
- )
42
+ expect(url).to eq(expected_url + "?ref=#{ref}")
42
43
  schema
43
44
  end
44
45
 
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Slodd::Http do
5
+ let(:url) { double }
6
+ let(:schema_file) { double(read: "the schema") }
7
+ subject { described_class.new(url: url) }
8
+
9
+ describe "#schema" do
10
+ it "downloads the schema from the url given" do
11
+ expect(subject).to receive(:open).with(url).and_return(schema_file)
12
+ expect(subject.schema).to eq "the schema"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Slodd::Local do
5
+ subject { described_class.new(path: schema_path) }
6
+
7
+ describe "#schema" do
8
+ it "reads the file" do
9
+ expect(subject.schema).to eq File.read(schema_path)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Slodd::Runner do
5
+ subject { described_class }
6
+
7
+ describe "#run" do
8
+ before do
9
+ Slodd::Config.path = schema_path
10
+ Slodd::Config.databases = "slodd_test"
11
+ end
12
+
13
+ after do
14
+ Slodd::Config.reset
15
+ end
16
+
17
+ it "creates the database ready for testing" do
18
+ output = capture_stdout do
19
+ subject.run!
20
+ end
21
+
22
+ expect(output).to match(/create_database\(slodd_test\)/)
23
+
24
+ expect(Test.count).to eq 0
25
+ Test.create(name: "James")
26
+
27
+ expect(Test.count).to eq 1
28
+ expect(Test.last.name).to eq "James"
29
+ expect(`mysql -uroot -e "show databases;"`).to match(/slodd_test/)
30
+
31
+ subject.run!
32
+
33
+ expect(Test.count).to eq 0
34
+ end
35
+
36
+ context "with multiple databases" do
37
+ before do
38
+ Slodd::Config.databases = "slodd_test slodd_test_2"
39
+ end
40
+
41
+ it "creates both databases" do
42
+ output = capture_stdout do
43
+ subject.run!
44
+ end
45
+
46
+ expect(output).to match(/create_database\(slodd_test\)/)
47
+ expect(output).to match(/create_database\(slodd_test_2\)/)
48
+
49
+ databases = `mysql -uroot -e "show databases;"`
50
+ expect(databases).to match(/slodd_test/)
51
+ expect(databases).to match(/slodd_test_2/)
52
+ end
53
+ end
54
+
55
+ context "when something is failing" do
56
+ before do
57
+ allow(ActiveRecord::Base).to receive(:establish_connection)
58
+ .and_raise(Mysql2::Error, "mysql error")
59
+ end
60
+
61
+ it "ouputs a usefull message to stderr" do
62
+ message = capture_stderr do
63
+ subject.run!
64
+ end
65
+ expect(message).to match(/mysql error/)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ class Test < ActiveRecord::Base; end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,51 @@
1
- require 'slodd'
1
+ # encoding: utf-8
2
+ require "simplecov"
3
+ SimpleCov.minimum_coverage 100
4
+ SimpleCov.start
5
+
6
+ require "slodd"
7
+ require "stringio"
2
8
 
3
9
  RSpec.configure do |config|
4
10
  config.treat_symbols_as_metadata_keys_with_true_values = true
5
11
  config.run_all_when_everything_filtered = true
6
12
  config.filter_run :focus
7
- config.order = 'random'
13
+ config.order = "random"
14
+ end
15
+
16
+ module Slodd
17
+ module Config
18
+ def self.reset
19
+ defaults
20
+ self.github = nil
21
+ self.password = nil
22
+ self.url = nil
23
+ self.token = nil
24
+ self.ref = nil
25
+ end
26
+ end
27
+ end
28
+
29
+ def capture_stderr
30
+ old_stderr = $stderr
31
+ fake_stderr = StringIO.new
32
+ $stderr = fake_stderr
33
+ yield
34
+ fake_stderr.string
35
+ ensure
36
+ $stderr = old_stderr
37
+ end
38
+
39
+ def capture_stdout
40
+ old_stdout = $stdout
41
+ fake_stdout = StringIO.new
42
+ $stdout = fake_stdout
43
+ yield
44
+ fake_stdout.string
45
+ ensure
46
+ $stdout = old_stdout
8
47
  end
9
48
 
49
+ def schema_path
50
+ File.join(File.dirname(__FILE__), "support", "schema.rb")
51
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ ActiveRecord::Schema.define(version: 1) do
3
+ create_table "tests", force: true do |t|
4
+ t.string "name"
5
+ end
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slodd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,6 +75,38 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rubocop
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
78
110
  description: Schema Loading On Dependent Databases
79
111
  email:
80
112
  - ed.robinson@reevoo.com
@@ -85,6 +117,7 @@ extensions: []
85
117
  extra_rdoc_files: []
86
118
  files:
87
119
  - .gitignore
120
+ - .rubocop.yml
88
121
  - .travis.yml
89
122
  - Gemfile
90
123
  - README.md
@@ -101,8 +134,12 @@ files:
101
134
  - slodd.gemspec
102
135
  - spec/lib/config_spec.rb
103
136
  - spec/lib/gitub_spec.rb
137
+ - spec/lib/http_spec.rb
138
+ - spec/lib/local_spec.rb
139
+ - spec/lib/runner_spec.rb
104
140
  - spec/spec_helper.rb
105
- homepage: ''
141
+ - spec/support/schema.rb
142
+ homepage: https://github.com/errm/slodd
106
143
  licenses: []
107
144
  post_install_message:
108
145
  rdoc_options: []
@@ -116,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
153
  version: '0'
117
154
  segments:
118
155
  - 0
119
- hash: -2439965077187413609
156
+ hash: -4230072505507451069
120
157
  required_rubygems_version: !ruby/object:Gem::Requirement
121
158
  none: false
122
159
  requirements:
@@ -125,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
162
  version: '0'
126
163
  segments:
127
164
  - 0
128
- hash: -2439965077187413609
165
+ hash: -4230072505507451069
129
166
  requirements: []
130
167
  rubyforge_project:
131
168
  rubygems_version: 1.8.25
@@ -135,4 +172,8 @@ summary: Schema Loading On Dependent Databases
135
172
  test_files:
136
173
  - spec/lib/config_spec.rb
137
174
  - spec/lib/gitub_spec.rb
175
+ - spec/lib/http_spec.rb
176
+ - spec/lib/local_spec.rb
177
+ - spec/lib/runner_spec.rb
138
178
  - spec/spec_helper.rb
179
+ - spec/support/schema.rb