Skip to content

Commit 63631e2

Browse files
byrootmscoutermarsh
andcommitted
Make schema_dump, query_cache, replica and database_tasks configurable via DATABASE_URL
Fix: rails#50745 I went a bit farther and handled all the boolean configs, not just `schema_cache`. Co-Authored-By: Mike Coutermarsh <[email protected]>
1 parent fc7befc commit 63631e2

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Make `schema_dump`, `query_cache`, `replica` and `database_tasks` configurable via `DATABASE_URL`
2+
3+
This wouldn't always work previously because boolean values would be interpreted as strings.
4+
5+
e.g. `DATABASE_URL=postgres://localhost/foo?schema_dump=false` now properly disable dumping the schema
6+
cache.
7+
8+
*Mike Coutermarsh*, *Jean Boussier*
9+
110
* Introduce `ActiveRecord::Transactions::ClassMethods#set_callback`
211

312
It is identical to `ActiveSupport::Callbacks::ClassMethods#set_callback`

activerecord/lib/active_record/database_configurations/url_config.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,29 @@ def initialize(env_name, name, url, configuration_hash = {})
4141
super(env_name, name, configuration_hash)
4242

4343
@url = url
44-
@configuration_hash = @configuration_hash.merge(build_url_hash).freeze
44+
@configuration_hash = @configuration_hash.merge(build_url_hash)
45+
46+
if @configuration_hash[:schema_dump] == "false"
47+
@configuration_hash[:schema_dump] = false
48+
end
49+
50+
if @configuration_hash[:query_cache] == "false"
51+
@configuration_hash[:query_cache] = false
52+
end
53+
54+
to_boolean!(@configuration_hash, :replica)
55+
to_boolean!(@configuration_hash, :database_tasks)
56+
57+
@configuration_hash.freeze
4558
end
4659

4760
private
61+
def to_boolean!(configuration_hash, key)
62+
if configuration_hash[key].is_a?(String)
63+
configuration_hash[key] = configuration_hash[key] != "false"
64+
end
65+
end
66+
4867
# Return a Hash that can be merged into the main config that represents
4968
# the passed in url
5069
def build_url_hash
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper"
4+
5+
module ActiveRecord
6+
class DatabaseConfigurations
7+
class UrlConfigTest < ActiveRecord::TestCase
8+
def test_schema_dump_parsing
9+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?schema_dump=false", {})
10+
assert_nil config.schema_dump
11+
12+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?schema_dump=db/foo_schema.rb", {})
13+
assert_equal "db/foo_schema.rb", config.schema_dump
14+
15+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo", {})
16+
assert_equal "schema.rb", config.schema_dump
17+
end
18+
19+
def test_query_cache_parsing
20+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?query_cache=false", {})
21+
assert_equal false, config.query_cache
22+
23+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?query_cache=42", {})
24+
assert_equal "42", config.query_cache
25+
end
26+
27+
def test_replica_parsing
28+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo", {})
29+
assert_nil config.replica?
30+
31+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?replica=true", {})
32+
assert_equal true, config.replica?
33+
34+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?replica=false", {})
35+
assert_equal false, config.replica?
36+
37+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?replica=random", {})
38+
assert_equal true, config.replica?
39+
end
40+
41+
def test_database_tasks_parsing
42+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo", {})
43+
assert_equal true, config.database_tasks?
44+
45+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?database_tasks=random", {})
46+
assert_equal true, config.database_tasks?
47+
48+
config = UrlConfig.new("default_env", "primary", "postgres://localhost/foo?database_tasks=false", {})
49+
assert_equal false, config.database_tasks?
50+
end
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)