slodd 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
data/bin/slodd CHANGED
@@ -2,45 +2,43 @@
2
2
 
3
3
  require 'slodd'
4
4
  require 'optparse'
5
- options = Hash.new
6
5
 
7
6
  OptionParser.new do |opt|
8
- opt.on('-d', '--database DATABASE', "Database Name") do |d|
9
- options[:database] = d
7
+ opt.on('-d', '--database DATABASES', "Database names") do |d|
8
+ Slodd::Config.databases = d
10
9
  end
11
10
 
12
11
  opt.on('-s', '--schema URI', "URI for schema.rb") do |s|
13
- options[:url] = s
12
+ Slodd::Config.url = s
13
+ end
14
+
15
+ opt.on('-g', '--github USER/REPO', "github repo") do |g|
16
+ Slodd::Config.github = g
17
+ end
18
+
19
+ opt.on('-t', '--token TOKEN', "github token") do |t|
20
+ Slodd::Config.token = t
21
+ end
22
+
23
+ opt.on('-r', '--ref REF', "github ref") do |r|
24
+ Slodd::Config.ref = g
14
25
  end
15
26
 
16
27
  opt.on( '-f', '--file-schema-path PATH', 'Path to schema.rb') do |f|
17
- options[:file] = f
28
+ Slodd::Config.path = f
18
29
  end
19
30
 
20
31
  opt.on('-u', '--username USERNAME', "MySQL Username") do |u|
21
- options[:username] = u
32
+ Slodd::Config.username = u
22
33
  end
23
34
 
24
35
  opt.on('-p', '--password PASSWORD', "MySQL Password") do |p|
25
- options[:password] = p
36
+ Slodd::Config.password = p
26
37
  end
27
38
 
28
39
  opt.on('-h', '--host HOST', "MySQL Hostname") do |h|
29
- options[:host] = h
40
+ Slodd::Config.host = h
30
41
  end
31
42
  end.parse!
32
43
 
33
- puts options[:url]
34
- puts options[:file]
35
-
36
- unless options[:url] || options[:file]
37
- puts "you must specify a file or a url"
38
- exit 1
39
- end
40
-
41
- unless options[:database]
42
- puts "you must specify a database"
43
- exit 1
44
- end
45
-
46
- Slodd::Base.new(options).run!
44
+ Slodd::Runner.run!
@@ -0,0 +1,71 @@
1
+ require "active_support/core_ext"
2
+
3
+ module Slodd
4
+ module Config
5
+ mattr_accessor :path, :github, :username, :password, :host, :url, :token, :ref
6
+ mattr_writer :databases
7
+
8
+ def self.defaults
9
+ self.path = "db/schema.rb"
10
+ self.username = "root"
11
+ self.host = "localhost"
12
+ end
13
+
14
+ defaults
15
+
16
+ def self.databases
17
+ return @@databases.split if defined?(@@databases)
18
+ []
19
+ end
20
+
21
+ def self.database_settings
22
+ settings = { adapter: "mysql2", host: host, username: username }
23
+ password ? settings.merge(password: password) : settings
24
+ end
25
+
26
+ def self.owner
27
+ github.split("/")[0] if github
28
+ end
29
+
30
+ def self.repo
31
+ github.split("/")[1] if github
32
+ end
33
+
34
+ def self.fetcher
35
+ if github
36
+ validate_github
37
+ Github.new(attributes)
38
+ elsif url
39
+ Http.new(attributes)
40
+ elsif path
41
+ Local.new(attributes)
42
+ end
43
+ end
44
+
45
+ def self.validate_github
46
+ fail ArgumentError unless owner && repo && token
47
+ end
48
+
49
+ 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)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,36 @@
1
+ require "open-uri"
2
+
3
+ module Slodd
4
+ class Github
5
+ attr_accessor :owner, :repo, :path, :token, :ref
6
+
7
+ def initialize(attrs)
8
+ self.owner = attrs.fetch(:owner)
9
+ self.repo = attrs.fetch(:repo)
10
+ self.token = attrs.fetch(:token)
11
+ self.path = attrs.fetch(:path)
12
+ self.ref = attrs[:ref]
13
+ end
14
+
15
+ def schema
16
+ @schema ||= open(url, headers).read
17
+ end
18
+
19
+ private
20
+
21
+ def url
22
+ "https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}#{branch}"
23
+ end
24
+
25
+ def branch
26
+ "?ref=#{ref}" if ref.present?
27
+ end
28
+
29
+ def headers
30
+ {
31
+ "Accept" => "application/vnd.github.3.raw",
32
+ "Authorization" => "token #{token}",
33
+ }
34
+ end
35
+ end
36
+ end
data/lib/slodd/http.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "open-uri"
2
+
3
+ module Slodd
4
+ class Http
5
+ attr_accessor :url
6
+
7
+ def initialize(attrs)
8
+ self.url = attrs.fetch(:url)
9
+ end
10
+
11
+ def schema
12
+ open(url).read
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module Slodd
2
+ class Local
3
+ attr_accessor :path
4
+
5
+ def initialize(attrs)
6
+ self.path = attrs.fetch(:path)
7
+ end
8
+
9
+ def schema
10
+ File.read(path)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,42 @@
1
+ require "active_record"
2
+ require "mysql2"
3
+
4
+ module Slodd
5
+ class Runner
6
+ def self.run!
7
+ new.run!
8
+ end
9
+
10
+ def initialize
11
+ self.schema = Config.fetcher.schema
12
+ end
13
+
14
+ def run!
15
+ Config.databases.each do |database|
16
+ create_database(database)
17
+ eval(schema)
18
+ end
19
+ end
20
+
21
+ private
22
+ def create_database(database)
23
+ options = {charset: 'utf8', collation: 'utf8_unicode_ci'}
24
+
25
+ begin
26
+ ActiveRecord::Base.establish_connection database_settings
27
+ ActiveRecord::Base.connection.drop_database database
28
+ ActiveRecord::Base.connection.create_database database, options
29
+ ActiveRecord::Base.establish_connection database_settings.merge(database: database)
30
+ 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
+ end
34
+ end
35
+
36
+ def database_settings
37
+ Config.database_settings
38
+ end
39
+
40
+ attr_accessor :schema
41
+ end
42
+ end
data/lib/slodd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Slodd
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/slodd.rb CHANGED
@@ -1,44 +1,10 @@
1
1
  require "slodd/version"
2
- require "rubygems"
3
- require "active_record"
4
- require "open-uri"
5
- require "mysql2"
6
2
 
7
- module Slodd
8
- class Base
9
- def initialize(options)
10
- @url = options[:url]
11
- @schema = options[:file]
12
- @database_settings = {:adapter => "mysql2", :host => "localhost",:username => "root"}.merge options.reject {|key,| [:url,:file].include?(key) } || {}
13
- end
14
-
15
- def run!
16
- create_database
17
- unless @schema
18
- eval(open(@url){|f| f.read })
19
- else
20
- load(@schema)
21
- end
22
- end
23
-
24
- private
3
+ require 'slodd/github'
4
+ require 'slodd/local'
5
+ require 'slodd/http'
6
+ require 'slodd/config'
7
+ require 'slodd/runner'
25
8
 
26
- def create_database
27
- options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
28
-
29
- create_db = lambda do |config|
30
- ActiveRecord::Base.establish_connection config.merge(:database => nil)
31
- ActiveRecord::Base.connection.drop_database config[:database]
32
- ActiveRecord::Base.connection.create_database config[:database], options
33
- ActiveRecord::Base.establish_connection config
34
- end
35
-
36
- begin
37
- create_db.call @database_settings
38
- rescue Mysql2::Error => sqlerr
39
- $stderr.puts sqlerr.error
40
- $stderr.puts "Couldn't create database for #{@database_settings.inspect}, charset: utf8, collation: utf8_unicode_ci"
41
- end
42
- end
43
- end
9
+ module Slodd
44
10
  end
@@ -0,0 +1,191 @@
1
+ require "spec_helper"
2
+
3
+ describe Slodd::Config do
4
+ subject { described_class }
5
+
6
+ after(:each) do
7
+ subject.reset
8
+ end
9
+
10
+ describe "attributes" do
11
+ describe ".path" do
12
+ it "has a default" do
13
+ expect(subject.path).to eq "db/schema.rb"
14
+ end
15
+
16
+ it "can be overridden" do
17
+ subject.path = "dingdong/schema.rb"
18
+ expect(subject.path).to eq "dingdong/schema.rb"
19
+ end
20
+ end
21
+
22
+ describe "github attrs" do
23
+ before do
24
+ subject.github = "errm/awesome_rails_app"
25
+ end
26
+
27
+ describe ".owner" do
28
+ specify { expect(subject.owner).to eq "errm" }
29
+ end
30
+
31
+ describe ".repo" do
32
+ specify { expect(subject.repo).to eq "awesome_rails_app" }
33
+ end
34
+ end
35
+
36
+ describe ".username" do
37
+ it "has a default" do
38
+ expect(subject.username).to eq "root"
39
+ end
40
+
41
+ it "can be overridden" do
42
+ subject.username = "mysqlusr"
43
+ expect(subject.username).to eq "mysqlusr"
44
+ end
45
+ end
46
+
47
+ describe ".password" do
48
+ it "defaults to nil" do
49
+ expect(subject.password).to be_nil
50
+ end
51
+
52
+ it "can be overridden" do
53
+ subject.password = "mysqlpass"
54
+ expect(subject.password).to eq "mysqlpass"
55
+ end
56
+ end
57
+
58
+ describe ".url" do
59
+ it "defaults to nil" do
60
+ expect(subject.url).to be_nil
61
+ end
62
+
63
+ it "can be overridden" do
64
+ subject.url = "http://some.site.com/schema.rb"
65
+ expect(subject.url).to eq "http://some.site.com/schema.rb"
66
+ end
67
+ end
68
+
69
+ describe ".fetcher" do
70
+ it "defaults to Local" do
71
+ expect(subject.fetcher.class).to eq Slodd::Local
72
+ end
73
+
74
+ it "returns Github if details are supplied" do
75
+ subject.github = "errm/awesome_rails_app"
76
+ subject.token = "oauth token"
77
+ expect(subject.fetcher.class).to eq Slodd::Github
78
+ end
79
+
80
+ it "rasies an argument error if no github token is supplied" do
81
+ subject.github = "errm/awesome_rails_app"
82
+ expect { subject.fetcher }.to raise_error(ArgumentError)
83
+ end
84
+
85
+ it "rasies an argument error if the github string is malformed" do
86
+ subject.github = "errm"
87
+ subject.token = "oauth token"
88
+ expect { subject.fetcher }.to raise_error(ArgumentError)
89
+ end
90
+
91
+ it "returns Http if a url is supplied" do
92
+ subject.url = "http://some.url/schema.rb"
93
+ expect(subject.fetcher.class).to eq Slodd::Http
94
+ end
95
+ end
96
+
97
+ describe ".attributes" do
98
+ context "Github" do
99
+ it "returns all the arguments needed by github" do
100
+ subject.github = "errm/awesome_rails_app"
101
+ subject.token = "oauth token"
102
+ subject.ref = "my-awesome-branch"
103
+ attrs = {
104
+ owner: "errm",
105
+ repo: "awesome_rails_app",
106
+ token: "oauth token",
107
+ path: "db/schema.rb",
108
+ ref: "my-awesome-branch",
109
+ }
110
+ expect(subject.attributes).to eq attrs
111
+ end
112
+ end
113
+
114
+ context "Local" do
115
+ it "returns all the arguments needed by Local" do
116
+ attrs = {
117
+ path: "db/schema.rb",
118
+ }
119
+ expect(subject.attributes).to eq attrs
120
+ end
121
+ end
122
+
123
+ context "Http" do
124
+ it "returns all the arguments needed by Http" do
125
+ subject.url = "http://some.url.com/db/schema.rb"
126
+ attrs = {
127
+ path: "db/schema.rb",
128
+ url: "http://some.url.com/db/schema.rb",
129
+ }
130
+ expect(subject.attributes).to eq attrs
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ describe ".databases" do
137
+ it "defaults to empty array" do
138
+ expect(subject.databases).to eq []
139
+ end
140
+
141
+ it "is an array of names" do
142
+ subject.databases = "db1 db2 db3"
143
+ expect(subject.databases).to eq %w[db1 db2 db3]
144
+ end
145
+
146
+ it "is an array of one name" do
147
+ subject.databases = "db1"
148
+ expect(subject.databases).to eq ["db1"]
149
+ end
150
+ end
151
+
152
+ describe ".database_settings" do
153
+ context "without a password" do
154
+
155
+ it "returns the settings" do
156
+ settings = {
157
+ adapter: "mysql2",
158
+ host: "localhost",
159
+ username: "root",
160
+ }
161
+ expect(subject.database_settings).to eq settings
162
+ end
163
+
164
+ end
165
+
166
+ context "with a password" do
167
+ it "returns the settings" do
168
+ subject.password = "brian"
169
+ settings = {
170
+ adapter: "mysql2",
171
+ host: "localhost",
172
+ username: "root",
173
+ password: "brian",
174
+ }
175
+ expect(subject.database_settings).to eq settings
176
+ end
177
+ end
178
+ end
179
+
180
+ describe ".host" do
181
+ it "defaults to localhost" do
182
+ expect(subject.host).to eq "localhost"
183
+ end
184
+
185
+ it "it can be overridden" do
186
+ subject.host = "db1.something.com"
187
+ expect(subject.host).to eq "db1.something.com"
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ describe Slodd::Github do
4
+ let(:owner) { "errm" }
5
+ let(:repo) { "awesome_rails_app" }
6
+ let(:path) { "database/schema.rb" }
7
+ let(:token) { "secret-oauth-token" }
8
+ let(:ref) { nil }
9
+
10
+ let(:schema) { double(read: nil) }
11
+
12
+ subject do
13
+ described_class.new(
14
+ owner: owner,
15
+ repo: repo,
16
+ path: path,
17
+ token: token,
18
+ ref: ref
19
+ )
20
+ end
21
+
22
+ describe "#schema" do
23
+ it "hits the correct url" do
24
+ allow(subject).to receive(:open) do |url, _|
25
+ expect(url).to eq(
26
+ "https://api.github.com/repos/#{owner}/#{repo}/contents/#{path}"
27
+ )
28
+ schema
29
+ end
30
+
31
+ subject.schema
32
+ end
33
+
34
+ context "with a ref" do
35
+ let(:ref) { "exciting-feature-branch" }
36
+
37
+ it "hits a url including the ref param" do
38
+ 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
+ schema
43
+ end
44
+
45
+ subject.schema
46
+ end
47
+ end
48
+
49
+ it "uses the correct token" do
50
+ allow(subject).to receive(:open) do |_, headers|
51
+ expect(headers["Authorization"]).to eq "token #{token}"
52
+ schema
53
+ end
54
+
55
+ subject.schema
56
+ end
57
+
58
+ it "requests the raw mediatype" do
59
+ allow(subject).to receive(:open) do |_, headers|
60
+ expect(headers["Accept"]).to eq "application/vnd.github.3.raw"
61
+ schema
62
+ end
63
+
64
+ subject.schema
65
+ end
66
+
67
+ it "returns the schema" do
68
+ allow(subject).to receive(:open).and_return(schema)
69
+ allow(schema).to receive(:read).and_return("the schema")
70
+ expect(subject.schema).to eq "the schema"
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,9 @@
1
+ require 'slodd'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ config.order = 'random'
8
+ end
9
+
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.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-12 00:00:00.000000000 Z
12
+ date: 2014-01-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -84,12 +84,21 @@ extensions: []
84
84
  extra_rdoc_files: []
85
85
  files:
86
86
  - .gitignore
87
+ - .travis.yml
87
88
  - Gemfile
88
89
  - Rakefile
89
90
  - bin/slodd
90
91
  - lib/slodd.rb
92
+ - lib/slodd/config.rb
93
+ - lib/slodd/github.rb
94
+ - lib/slodd/http.rb
95
+ - lib/slodd/local.rb
96
+ - lib/slodd/runner.rb
91
97
  - lib/slodd/version.rb
92
98
  - slodd.gemspec
99
+ - spec/lib/config_spec.rb
100
+ - spec/lib/gitub_spec.rb
101
+ - spec/spec_helper.rb
93
102
  homepage: ''
94
103
  licenses: []
95
104
  post_install_message:
@@ -104,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
113
  version: '0'
105
114
  segments:
106
115
  - 0
107
- hash: -1602909029375330421
116
+ hash: 2304308098594840791
108
117
  required_rubygems_version: !ruby/object:Gem::Requirement
109
118
  none: false
110
119
  requirements:
@@ -113,11 +122,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
122
  version: '0'
114
123
  segments:
115
124
  - 0
116
- hash: -1602909029375330421
125
+ hash: 2304308098594840791
117
126
  requirements: []
118
127
  rubyforge_project:
119
128
  rubygems_version: 1.8.25
120
129
  signing_key:
121
130
  specification_version: 3
122
131
  summary: Schema Loading On Dependent Databases
123
- test_files: []
132
+ test_files:
133
+ - spec/lib/config_spec.rb
134
+ - spec/lib/gitub_spec.rb
135
+ - spec/spec_helper.rb