database_url 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +12 -0
- data/README.md +17 -6
- data/database_url.gemspec +3 -2
- data/lib/database_url.rb +38 -13
- data/lib/database_url/version.rb +1 -1
- data/spec/database_url_spec.rb +79 -35
- data/spec/spec_helper.rb +2 -0
- metadata +34 -28
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ba2ea5185f9833e0f0b30d7e4440c138019b6427
|
4
|
+
data.tar.gz: 87bd701c1f3eea559518ca379f6361d795f68e0d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e016566a71450259e626674d6725ca26dcb41651a929ea342a42023eeb3c4c50c7044536cb154c1e7b7f0078fa839f6d2e270c045decf098c7b6a99376408837
|
7
|
+
data.tar.gz: 34cb07b00a0673467d9c747a3251efe2da3bffbde41eca00f6989ee3b04b8b3cd006d5b8a7fdb13d3c66f5646e90517d234a33e1e7f088d778887aa13263da02
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
0.1.0 / 2014-07-12
|
2
|
+
|
3
|
+
* Breaking changes
|
4
|
+
|
5
|
+
* replaced .active_record_config(url = nil) with .to_active_record_hash(url = nil)
|
6
|
+
* replaced .database_url(hash) with .to_active_record_url(hash)
|
7
|
+
|
8
|
+
* Enhancements
|
9
|
+
|
10
|
+
* .to_sequel_hash(url = nil)
|
11
|
+
* .to_sequel_url(hash)
|
12
|
+
|
1
13
|
0.0.1 / 2013-09-19
|
2
14
|
|
3
15
|
Initial release!
|
data/README.md
CHANGED
@@ -4,29 +4,40 @@ Convert back and forth between Heroku-style `ENV['DATABASE_URL']` and Rails/Acti
|
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
+
### Methods
|
8
|
+
|
9
|
+
DatabaseUrl.to_active_record_hash(url = nil)
|
10
|
+
DatabaseUrl.to_active_record_url(hash)
|
11
|
+
DatabaseUrl.to_sequel_hash(url = nil)
|
12
|
+
DatabaseUrl.to_sequel_url(hash)
|
13
|
+
|
7
14
|
### Get `DATABASE_URL` from config hash
|
8
15
|
|
9
16
|
You pass it a hash (string or symbol keys, doesn't matter) and it gives you a URL:
|
10
17
|
|
11
18
|
>> c = YAML.load_file('/path/to/config/database.yml')
|
12
19
|
=> {"development"=>{"adapter"=>"mysql2", "database"=>"myapp_development", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "test"=>{"adapter"=>"mysql2", "database"=>"myapp_test", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "production"=>{"adapter"=>"mysql2", "database"=>"myapp", "username"=>"myapp", "password"=>"XXX", "encoding"=>"utf8"}, "cucumber"=>{"adapter"=>"mysql2", "database"=>"myapp_cucumber", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}}
|
13
|
-
>> DatabaseUrl.
|
20
|
+
>> DatabaseUrl.to_active_record_url c['development']
|
14
21
|
=> "mysql2://root:password@127.0.0.1/myapp_development?encoding=utf8"
|
15
22
|
|
16
23
|
### Get config hash from `DATABASE_URL`
|
17
24
|
|
18
|
-
|
19
|
-
|
20
|
-
>> DatabaseUrl.active_record_config
|
25
|
+
>> DatabaseUrl.to_active_record_hash('postgres://uuu:xxx@127.0.0.1:1234/abc')
|
21
26
|
=> {:adapter=>"postgres", :host=>"127.0.0.1", :port=>1234, :database=>"abc", :user=>"uuu", :password=>"xxx"}
|
22
27
|
|
23
|
-
|
28
|
+
If you omit the argument, it will try `ENV['DATABASE_URL']`
|
24
29
|
|
25
30
|
## Query parameters supported
|
26
31
|
|
27
32
|
* `encoding`
|
28
33
|
* `pool`
|
29
34
|
|
35
|
+
## TODO
|
36
|
+
|
37
|
+
* all query params
|
38
|
+
* automatically pull from config/database.yml
|
39
|
+
* automatically pull from ActiveRecord::Base.connection_config
|
40
|
+
|
30
41
|
## Copyright
|
31
42
|
|
32
|
-
Copyright
|
43
|
+
Copyright 2014 Seamus Abshere
|
data/database_url.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = DatabaseUrl::VERSION
|
9
9
|
spec.authors = ["Seamus Abshere"]
|
10
10
|
spec.email = ["seamus@abshere.net"]
|
11
|
-
spec.description = %q{Convert back and forth between Heroku-style ENV['DATABASE_URL'] and
|
12
|
-
spec.summary = %q{Convert back and forth between Heroku-style ENV['DATABASE_URL'] and
|
11
|
+
spec.description = %q{Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Sequel/ActiveRecord-style config hashes.}
|
12
|
+
spec.summary = %q{Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Sequel/ActiveRecord-style config hashes.}
|
13
13
|
spec.homepage = "https://github.com/seamusabshere/database_url"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "pry"
|
24
25
|
end
|
data/lib/database_url.rb
CHANGED
@@ -5,38 +5,56 @@ require 'cgi'
|
|
5
5
|
|
6
6
|
module DatabaseUrl
|
7
7
|
class << self
|
8
|
-
def
|
8
|
+
def to_active_record_hash(url = nil)
|
9
|
+
to_hash ACTIVE_RECORD_FIELD_MAP, url
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_active_record_url(hash)
|
13
|
+
to_url ACTIVE_RECORD_FIELD_MAP, hash
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_sequel_hash(url = nil)
|
17
|
+
to_hash SEQUEL_FIELD_MAP, url
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_sequel_url(hash)
|
21
|
+
to_url SEQUEL_FIELD_MAP, hash
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def to_hash(field_map, url = nil)
|
9
27
|
if url.nil?
|
10
28
|
url = ENV.fetch 'DATABASE_URL'
|
11
29
|
end
|
12
30
|
uri = URI.parse url
|
13
|
-
|
31
|
+
memo = {
|
14
32
|
adapter: uri.scheme,
|
15
33
|
host: uri.host,
|
16
34
|
database: File.basename(uri.path),
|
17
35
|
}
|
18
36
|
if uri.port
|
19
|
-
|
37
|
+
memo[:port] = uri.port
|
20
38
|
end
|
21
39
|
if uri.user
|
22
|
-
|
40
|
+
memo[:user] = uri.user
|
23
41
|
end
|
24
42
|
if uri.password
|
25
|
-
|
43
|
+
memo[:password] = uri.password
|
26
44
|
end
|
27
45
|
query = CGI.parse uri.query
|
28
46
|
if query.has_key?('encoding')
|
29
|
-
|
47
|
+
memo[:encoding] = query['encoding'][0]
|
30
48
|
end
|
31
|
-
if query
|
32
|
-
|
49
|
+
if (pool = query['pool'] || query['max_connections']) and pool.length > 0 # CGI.parse creates a Hash that defaults to []
|
50
|
+
memo[field_map.fetch('pool').to_sym] = pool[0].to_i
|
33
51
|
end
|
34
|
-
|
52
|
+
memo
|
35
53
|
end
|
36
54
|
|
37
|
-
def
|
55
|
+
def to_url(field_map, hash)
|
38
56
|
# stringify keys
|
39
|
-
c =
|
57
|
+
c = hash.inject({}) { |memo, (k, v)| memo[k.to_s] = v; memo }
|
40
58
|
userinfo = if c.has_key?('user') or c.has_key?('username') or c.has_key?('password')
|
41
59
|
username = c.values_at('user', 'username').compact.first
|
42
60
|
[ username, c['password'] ].join ':'
|
@@ -45,8 +63,8 @@ module DatabaseUrl
|
|
45
63
|
if c.has_key?('encoding')
|
46
64
|
query['encoding'] = c['encoding']
|
47
65
|
end
|
48
|
-
if c
|
49
|
-
query['pool'] =
|
66
|
+
if pool = c['pool'] || c['max_connections']
|
67
|
+
query[field_map.fetch('pool')] = pool.to_i
|
50
68
|
end
|
51
69
|
query = if query.length > 0
|
52
70
|
URI.encode_www_form query
|
@@ -69,4 +87,11 @@ module DatabaseUrl
|
|
69
87
|
end
|
70
88
|
|
71
89
|
DEFAULT_HOST = '127.0.0.1'
|
90
|
+
|
91
|
+
SEQUEL_FIELD_MAP = {
|
92
|
+
'pool' => 'max_connections',
|
93
|
+
}
|
94
|
+
ACTIVE_RECORD_FIELD_MAP = {
|
95
|
+
'pool' => 'pool',
|
96
|
+
}
|
72
97
|
end
|
data/lib/database_url/version.rb
CHANGED
data/spec/database_url_spec.rb
CHANGED
@@ -5,43 +5,87 @@ describe DatabaseUrl do
|
|
5
5
|
ENV.delete 'DATABASE_URL'
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
ENV['DATABASE_URL']
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
8
|
+
describe 'ActiveRecord' do
|
9
|
+
it "builds config hashes using ENV['DATABASE_URL']" do
|
10
|
+
ENV['DATABASE_URL'] = "postgres://uuu:xxx@127.0.0.1:1234/abc?encoding=latin1&pool=9"
|
11
|
+
DatabaseUrl.to_active_record_hash.should == {
|
12
|
+
adapter: 'postgres',
|
13
|
+
host: '127.0.0.1',
|
14
|
+
port: 1234,
|
15
|
+
database: 'abc',
|
16
|
+
user: 'uuu',
|
17
|
+
password: 'xxx',
|
18
|
+
encoding: 'latin1',
|
19
|
+
pool: 9,
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
it "builds URL from config hash" do
|
24
|
+
DatabaseUrl.to_active_record_url({
|
25
|
+
adapter: 'postgreq',
|
26
|
+
host: '127.0.0.2',
|
27
|
+
port: 1235,
|
28
|
+
database: 'abd',
|
29
|
+
user: 'uu1',
|
30
|
+
password: 'xx1',
|
31
|
+
encoding: 'utf8',
|
32
|
+
pool: 15,
|
33
|
+
}).should == "postgreq://uu1:xx1@127.0.0.2:1235/abd?encoding=utf8&pool=15"
|
34
|
+
end
|
21
35
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
36
|
+
it "builds config hashes given a URL" do
|
37
|
+
ENV['DATABASE_URL'] = "postgres://uuu:xxx@127.0.0.1:1234/abc" # should be ignored!
|
38
|
+
DatabaseUrl.to_active_record_hash("postgres://uu2:xxx@127.0.0.3:1236/abc?encoding=latin1").should == {
|
39
|
+
adapter: 'postgres',
|
40
|
+
host: '127.0.0.3',
|
41
|
+
port: 1236,
|
42
|
+
database: 'abc',
|
43
|
+
user: 'uu2',
|
44
|
+
password: 'xxx',
|
45
|
+
encoding: 'latin1',
|
46
|
+
}
|
47
|
+
end
|
33
48
|
end
|
34
49
|
|
35
|
-
|
36
|
-
ENV['DATABASE_URL']
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
50
|
+
describe 'Sequel' do
|
51
|
+
it "builds config hashes using ENV['DATABASE_URL']" do
|
52
|
+
ENV['DATABASE_URL'] = "postgres://uuu:xxx@127.0.0.1:1234/abc?encoding=latin1&pool=9"
|
53
|
+
DatabaseUrl.to_sequel_hash.should == {
|
54
|
+
adapter: 'postgres',
|
55
|
+
host: '127.0.0.1',
|
56
|
+
port: 1234,
|
57
|
+
database: 'abc',
|
58
|
+
user: 'uuu',
|
59
|
+
password: 'xxx',
|
60
|
+
encoding: 'latin1',
|
61
|
+
max_connections: 9,
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
it "builds URL from config hash" do
|
66
|
+
DatabaseUrl.to_sequel_url({
|
67
|
+
adapter: 'postgreq',
|
68
|
+
host: '127.0.0.2',
|
69
|
+
port: 1235,
|
70
|
+
database: 'abd',
|
71
|
+
user: 'uu1',
|
72
|
+
password: 'xx1',
|
73
|
+
encoding: 'utf8',
|
74
|
+
pool: 15,
|
75
|
+
}).should == "postgreq://uu1:xx1@127.0.0.2:1235/abd?encoding=utf8&max_connections=15"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "builds config hashes given a URL" do
|
79
|
+
ENV['DATABASE_URL'] = "postgres://uuu:xxx@127.0.0.1:1234/abc" # should be ignored!
|
80
|
+
DatabaseUrl.to_sequel_hash("postgres://uu2:xxx@127.0.0.3:1236/abc?encoding=latin1").should == {
|
81
|
+
adapter: 'postgres',
|
82
|
+
host: '127.0.0.3',
|
83
|
+
port: 1236,
|
84
|
+
database: 'abc',
|
85
|
+
user: 'uu2',
|
86
|
+
password: 'xxx',
|
87
|
+
encoding: 'latin1',
|
88
|
+
}
|
89
|
+
end
|
46
90
|
end
|
47
91
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,75 +1,82 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Seamus Abshere
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-07-12 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.3'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '1.3'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
|
-
|
63
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
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: Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Sequel/ActiveRecord-style
|
70
|
+
config hashes.
|
64
71
|
email:
|
65
72
|
- seamus@abshere.net
|
66
73
|
executables: []
|
67
74
|
extensions: []
|
68
75
|
extra_rdoc_files: []
|
69
76
|
files:
|
70
|
-
- .gitignore
|
71
|
-
- .rspec
|
72
|
-
- .travis.yml
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
73
80
|
- CHANGELOG
|
74
81
|
- Gemfile
|
75
82
|
- LICENSE.txt
|
@@ -83,29 +90,28 @@ files:
|
|
83
90
|
homepage: https://github.com/seamusabshere/database_url
|
84
91
|
licenses:
|
85
92
|
- MIT
|
93
|
+
metadata: {}
|
86
94
|
post_install_message:
|
87
95
|
rdoc_options: []
|
88
96
|
require_paths:
|
89
97
|
- lib
|
90
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
-
none: false
|
92
99
|
requirements:
|
93
|
-
- -
|
100
|
+
- - ">="
|
94
101
|
- !ruby/object:Gem::Version
|
95
102
|
version: '0'
|
96
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
104
|
requirements:
|
99
|
-
- -
|
105
|
+
- - ">="
|
100
106
|
- !ruby/object:Gem::Version
|
101
107
|
version: '0'
|
102
108
|
requirements: []
|
103
109
|
rubyforge_project:
|
104
|
-
rubygems_version:
|
110
|
+
rubygems_version: 2.2.2
|
105
111
|
signing_key:
|
106
|
-
specification_version:
|
107
|
-
summary: Convert back and forth between Heroku-style ENV['DATABASE_URL'] and
|
108
|
-
config
|
112
|
+
specification_version: 4
|
113
|
+
summary: Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Sequel/ActiveRecord-style
|
114
|
+
config hashes.
|
109
115
|
test_files:
|
110
116
|
- spec/database_url_spec.rb
|
111
117
|
- spec/spec_helper.rb
|