rails-default-database 1.0.9 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +5 -0
- data/lib/rails-default-database.rake +1 -1
- data/lib/rails-default-database.rb +34 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f90d52092727be87d95e3bc549871a60f3d1058e
|
4
|
+
data.tar.gz: 9253ba1b2ecfa937e25c9292a6a1285173080c1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56c33a78428a9049739153dc5e54116be58d33cf079d237fa2b9005f6df6ff51d0a077fbdc92d80a2004d80edf2a8e2bb76cd9f4a0624286521f96bebf4cab54
|
7
|
+
data.tar.gz: 9fdd61a51444933c5a76f63a6073658124b05d4be949847495edfd159f79ab60ff15b6c56a1e9bebf6945554ab369ca7e616ffc9772fcf3cd0efa0c2e6d1dd1e
|
data/README.markdown
CHANGED
@@ -11,6 +11,11 @@ You can still override the defaults by creating `config/database.yml`.
|
|
11
11
|
Use `rake db:config` to create `config/database.yml` with the defaults
|
12
12
|
that would have been assumed.
|
13
13
|
|
14
|
+
The default database name is based on the name of the root directory of your
|
15
|
+
application. This can be overridden by setting
|
16
|
+
`config.database_name = 'foo_%s'` in `config/application.rb`, with `%s` being
|
17
|
+
a placeholder for the current environment name.
|
18
|
+
|
14
19
|
As in standard Rails, the `DATABASE_URL` environment variable takes
|
15
20
|
precedence when defined. However, in the test environment, Rails Default
|
16
21
|
Database will append `_test` to the database name (after stripping an optional
|
@@ -2,6 +2,6 @@ desc 'Write out config/database.yml from rails-default-database'
|
|
2
2
|
task 'db:config' do
|
3
3
|
contents = Rails.configuration.database_configuration.to_yaml
|
4
4
|
File.open(Rails.root.join('config/database.yml'), 'w') do |f|
|
5
|
-
f.puts contents.sub(/^---\n/, '')
|
5
|
+
f.puts contents.sub(/^---\n/, '').gsub(/%i/, '')
|
6
6
|
end
|
7
7
|
end
|
@@ -1,5 +1,12 @@
|
|
1
1
|
Rails::Application::Configuration.class_eval do
|
2
2
|
|
3
|
+
def environments_for_database_configuration
|
4
|
+
['development', 'test', 'production'] |
|
5
|
+
root.join('config', 'environments').children.map do |e|
|
6
|
+
e.basename('.rb').to_s if e.extname == '.rb'
|
7
|
+
end.compact.sort
|
8
|
+
end
|
9
|
+
|
3
10
|
def database_configuration_with_default
|
4
11
|
config =
|
5
12
|
begin
|
@@ -8,22 +15,35 @@ Rails::Application::Configuration.class_eval do
|
|
8
15
|
end || {}
|
9
16
|
|
10
17
|
if url = ENV['DATABASE_URL'].presence
|
11
|
-
config
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
(environments_for_database_configuration | config.keys).each do |k|
|
19
|
+
config[k] = {'url' => url.gsub('%s', k)}
|
20
|
+
if k == 'test'
|
21
|
+
config[k]['url'] =
|
22
|
+
config[k]['url'].sub(/(?:_(?:#{environments_for_database_configuration.join('|')})(?:\d*|%i))?(?=\?|$)/, "_test")
|
23
|
+
end
|
17
24
|
end
|
18
25
|
config
|
19
26
|
else
|
20
27
|
default_database_configuration.merge(config)
|
28
|
+
end.tap do |c|
|
29
|
+
if ENV['TEST_DATABASE_URL'].present?
|
30
|
+
c['test'] = {'url' => ENV['TEST_DATABASE_URL']}
|
31
|
+
end
|
32
|
+
%w(url database).each do |key|
|
33
|
+
if value = c['test'] && c['test'][key]
|
34
|
+
c['test'][key] = value.sub(/%i/, ENV['TEST_ENV_NUMBER'].to_s)
|
35
|
+
end
|
36
|
+
end
|
21
37
|
end
|
22
38
|
end
|
23
39
|
|
40
|
+
attr_writer :database_name
|
41
|
+
def database_name
|
42
|
+
@database_name || File.basename(root).gsub(/[^[:alnum:]]+/, '_') + '_%s'
|
43
|
+
end
|
44
|
+
|
24
45
|
def default_database_configuration
|
25
|
-
|
26
|
-
driver = %w(pg mysql mysql2 sqlite3).detect do |a|
|
46
|
+
driver = %w(pg mysql2 mysql sqlite3).detect do |a|
|
27
47
|
begin
|
28
48
|
require a
|
29
49
|
true
|
@@ -47,11 +67,14 @@ Rails::Application::Configuration.class_eval do
|
|
47
67
|
else
|
48
68
|
{'adapter' => driver}
|
49
69
|
end
|
50
|
-
defaults['database'] ||=
|
70
|
+
defaults['database'] ||= database_name
|
51
71
|
|
52
|
-
|
72
|
+
environments_for_database_configuration.inject({}) do |h, env|
|
73
|
+
database = defaults['database']
|
74
|
+
database += '_%s' if env == 'test' && database !~ /%s/
|
75
|
+
database = database.gsub('%s', "#{env}#{'%i' if env == 'test'}")
|
53
76
|
h[env] = defaults.merge(
|
54
|
-
'database' =>
|
77
|
+
'database' => database
|
55
78
|
)
|
56
79
|
h
|
57
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-default-database
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Pope
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|