rails-default-database 1.0.7 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.markdown +13 -2
- data/lib/rails-default-database.rake +1 -1
- data/lib/rails-default-database.rb +46 -8
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5131765f88d06c672a6f77edebfc8832036fa0ee1100b4e4f89bbd2a6a9bbfe5
|
4
|
+
data.tar.gz: 1148dea899f6d075a0ad14f655b6d30b7e32cfc19705752fb43bea92433143b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64d4d192d3f0d81de6874fab97ab41455bca27fb7b443e4c91b19b63d0a911e9b08d88c3e68ef4099a2fd585c9ecf615278893cc2be18128e093ebd895d1e773
|
7
|
+
data.tar.gz: a0f594baf1fc95e3ac2c43da3ddaa9b55948282198db4cb49d9838901b84c6890f8dab4ddf9f2034ce47fd5cccd8b22c91c5e855e28f6f9174c84a90f2e5d762
|
data/README.markdown
CHANGED
@@ -11,7 +11,18 @@ 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
|
-
|
15
|
-
|
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
|
+
|
19
|
+
As in standard Rails, the `DATABASE_URL` environment variable takes
|
20
|
+
precedence when defined. However, in the test environment, Rails Default
|
21
|
+
Database will append `_test` to the database name (after stripping an optional
|
22
|
+
existing environment suffix), ensuring the development (or production!)
|
23
|
+
database is never clobbered. This enables storing your database configuration
|
24
|
+
in [`.env`](https://github.com/bkeepers/dotenv), if you so choose.
|
25
|
+
|
26
|
+
## License
|
16
27
|
|
17
28
|
Copyright (c) Tim Pope. MIT License.
|
@@ -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,18 +1,50 @@
|
|
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
|
6
13
|
database_configuration_without_default
|
7
14
|
rescue Errno::ENOENT, RuntimeError
|
8
15
|
end || {}
|
9
16
|
|
10
|
-
|
17
|
+
if url = ENV['DATABASE_URL'].presence
|
18
|
+
(environments_for_database_configuration | config.keys).each do |k|
|
19
|
+
config[k] = {'url' => url.gsub('%s', k)}
|
20
|
+
if k == 'test' && ENV['TEST_DATABASE_URL'] != 'default' && ENV['CI'] != 'true'
|
21
|
+
config[k]['url'] =
|
22
|
+
config[k]['url'].sub(/(?:_(?:#{environments_for_database_configuration.join('|')})(?:\d*|%i))?(?=\?|$)/, "_test")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
config
|
26
|
+
else
|
27
|
+
default_database_configuration.merge(config)
|
28
|
+
end.tap do |c|
|
29
|
+
url = ENV['TEST_DATABASE_URL'].presence
|
30
|
+
if url && url != 'default'
|
31
|
+
c['test'] = {'url' => url}
|
32
|
+
end
|
33
|
+
%w(url database).each do |key|
|
34
|
+
if value = c['test'] && c['test'][key]
|
35
|
+
c['test'][key] = value.sub(/%i/, ENV['TEST_ENV_NUMBER'].to_s)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_writer :database_name
|
42
|
+
def database_name
|
43
|
+
@database_name || File.basename(root).gsub(/[^[:alnum:]]+/, '_') + '_%s'
|
11
44
|
end
|
12
45
|
|
13
46
|
def default_database_configuration
|
14
|
-
|
15
|
-
driver = %w(pg mysql mysql2 sqlite3).detect do |a|
|
47
|
+
driver = %w(pg mysql2 mysql sqlite3).detect do |a|
|
16
48
|
begin
|
17
49
|
require a
|
18
50
|
true
|
@@ -36,17 +68,23 @@ Rails::Application::Configuration.class_eval do
|
|
36
68
|
else
|
37
69
|
{'adapter' => driver}
|
38
70
|
end
|
39
|
-
defaults['database'] ||=
|
71
|
+
defaults['database'] ||= database_name
|
40
72
|
|
41
|
-
|
73
|
+
environments_for_database_configuration.inject({}) do |h, env|
|
74
|
+
database = defaults['database']
|
75
|
+
database += '_%s' if env == 'test' && database !~ /%s/
|
76
|
+
database = database.gsub('%s', "#{env}#{'%i' if env == 'test'}")
|
42
77
|
h[env] = defaults.merge(
|
43
|
-
'database' =>
|
78
|
+
'database' => database
|
44
79
|
)
|
45
80
|
h
|
46
81
|
end
|
47
82
|
end
|
48
83
|
|
49
|
-
|
84
|
+
unless method_defined?(:database_configuration_without_default)
|
85
|
+
alias database_configuration_without_default database_configuration
|
86
|
+
alias database_configuration database_configuration_with_default
|
87
|
+
end
|
50
88
|
end
|
51
89
|
|
52
90
|
class RailsDefaultDatabaseRailtie < Rails::Railtie
|
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.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Pope
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -53,8 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
|
-
|
57
|
-
rubygems_version: 2.2.2
|
56
|
+
rubygems_version: 3.1.4
|
58
57
|
signing_key:
|
59
58
|
specification_version: 4
|
60
59
|
summary: Make database.yml optional in Rails
|