Skip to content

Commit 86aeadc

Browse files
authored
Merge pull request rails#42794 from westonganger/multi_db_config_database_tasks_option
Add database config option `database_tasks: false`
2 parents 13a714f + a77dd10 commit 86aeadc

File tree

7 files changed

+98
-1
lines changed

7 files changed

+98
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
* Add database config option `database_tasks`
2+
3+
If you would like to connect to an external database without any database
4+
mangement tasks such as schema management, migrations, seeds, etc. you can set
5+
the per database config option `database_tasks: false`
6+
7+
```yaml
8+
# config/database.yml
9+
10+
production:
11+
primary:
12+
database: my_database
13+
adapter: mysql2
14+
animals:
15+
database: my_animals_database
16+
adapter: mysql2
17+
database_tasks: false
18+
```
19+
20+
*Weston Ganger*
21+
122
* Fix `ActiveRecord::InternalMetadata` to not be broken by `config.active_record.record_timestamps = false`
223

324
Since the model always create the timestamp columns, it has to set them, otherwise it breaks

activerecord/lib/active_record/database_configurations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def configs_for(env_name: nil, spec_name: nil, name: nil, include_replicas: fals
4848

4949
unless include_replicas
5050
configs = configs.select do |db_config|
51-
!db_config.replica?
51+
db_config.database_tasks?
5252
end
5353
end
5454

activerecord/lib/active_record/database_configurations/hash_config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ def schema_cache_path
113113
def schema_dump
114114
configuration_hash.fetch(:schema_dump, true)
115115
end
116+
117+
def database_tasks? # :nodoc:
118+
!replica? && !!configuration_hash.fetch(:database_tasks, true)
119+
end
116120
end
117121
end
118122
end

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def for_each(databases)
178178
return if database_configs.count == 1
179179

180180
database_configs.each do |db_config|
181+
next unless db_config.database_tasks?
182+
181183
yield db_config.name
182184
end
183185
end

activerecord/test/cases/database_configurations/hash_config_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ def test_schema_dump_value_set_to_false
122122
config = HashConfig.new("default_env", "primary", { schema_dump: false })
123123
assert_equal false, config.schema_dump
124124
end
125+
126+
def test_database_tasks_defaults_to_true
127+
config = HashConfig.new("default_env", "primary", {})
128+
assert_equal true, config.database_tasks?
129+
end
130+
131+
def test_database_tasks_overrides_with_value
132+
config = HashConfig.new("default_env", "primary", database_tasks: false)
133+
assert_equal false, config.database_tasks?
134+
135+
config = HashConfig.new("default_env", "primary", database_tasks: "str")
136+
assert_equal true, config.database_tasks?
137+
end
125138
end
126139
end
127140
end

guides/source/active_record_multiple_databases.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ Note that there is no command for creating the database users, and you'll need t
202202
to support the readonly users for your replicas. If you want to create just the animals
203203
database you can run `bin/rails db:create:animals`.
204204

205+
## Connecting to Databases without Managing Schema and Migrations
206+
207+
If you would like to connect to an external database without any database
208+
mangement tasks such as schema management, migrations, seeds, etc. you can set
209+
the per database config option `database_tasks: false`. By default it is
210+
set to true.
211+
212+
```yaml
213+
production:
214+
primary:
215+
database: my_database
216+
adapter: mysql2
217+
animals:
218+
database: my_animals_database
219+
adapter: mysql2
220+
database_tasks: false
221+
```
222+
205223
## Generators and Migrations
206224
207225
Migrations for multiple databases should live in their own folders prefixed with the

railties/test/application/rake/multi_dbs_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,45 @@ class TwoMigration < ActiveRecord::Migration::Current
10651065

10661066
db_migrate_and_schema_dump_and_load
10671067
end
1068+
1069+
test "when database_tasks is false, then do not run the database tasks on that db" do
1070+
app_file "config/database.yml", <<-YAML
1071+
development:
1072+
primary:
1073+
database: db/default.sqlite3
1074+
adapter: sqlite3
1075+
animals:
1076+
database: db/development_animals.sqlite3
1077+
adapter: sqlite3
1078+
database_tasks: false
1079+
schema_dump: true ### database_tasks should override all sub-settings
1080+
YAML
1081+
1082+
Dir.chdir(app_path) do
1083+
animals_db_exists = lambda{ rails("runner", "puts !!(AnimalsBase.connection rescue false)").strip }
1084+
1085+
generate_models_for_animals
1086+
1087+
assert_equal "true", animals_db_exists.call
1088+
1089+
assert_not File.exist?("db/animals_schema.yml")
1090+
1091+
begin
1092+
assert_raise RuntimeError do
1093+
rails "db:migrate:animals" ### Task not defined
1094+
end
1095+
rescue RuntimeError => e
1096+
assert_includes e.message, "See the list of available tasks"
1097+
end
1098+
1099+
rails "db:schema:dump"
1100+
assert_not File.exist?("db/animals_schema.yml")
1101+
1102+
rails "db:drop"
1103+
assert_equal "true", animals_db_exists.call
1104+
end
1105+
end
1106+
10681107
end
10691108
end
10701109
end

0 commit comments

Comments
 (0)