Skip to content

Commit d7201f2

Browse files
authored
Merge pull request rails#53379 from flavorjones/flavorjones-distinguish-seeded-databases
`db:prepare` no longer loads seed when non-primary db is created
2 parents effe4bf + 01cc805 commit d7201f2

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

activerecord/lib/active_record/database_configurations/database_config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def schema_cache_path
9999
def use_metadata_table?
100100
raise NotImplementedError
101101
end
102+
103+
def seeds?
104+
raise NotImplementedError
105+
end
102106
end
103107
end
104108
end

activerecord/lib/active_record/database_configurations/hash_config.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ def primary? # :nodoc:
130130
Base.configurations.primary?(name)
131131
end
132132

133+
# Determines whether the db:prepare task should seed the database from db/seeds.rb.
134+
#
135+
# If the `seeds` key is present in the config, `seeds?` will return its value. Otherwise, it
136+
# will return `true` for the primary database and `false` for all other configs.
137+
def seeds?
138+
configuration_hash.fetch(:seeds, primary?)
139+
end
140+
133141
# Determines whether to dump the schema/structure files and the filename that
134142
# should be used.
135143
#

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def prepare_all
180180
each_current_configuration(env) do |db_config|
181181
database_initialized = initialize_database(db_config)
182182

183-
seed = true if database_initialized
183+
seed = true if database_initialized && db_config.seeds?
184184
end
185185

186186
each_current_environment(env) do |environment|

activerecord/test/cases/database_configurations/hash_config_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ def test_inspect_does_not_show_secrets
179179
config = HashConfig.new("default_env", "primary", { adapter: "abstract", password: "hunter2" })
180180
assert_equal "#<ActiveRecord::DatabaseConfigurations::HashConfig env_name=default_env name=primary adapter_class=ActiveRecord::ConnectionAdapters::AbstractAdapter>", config.inspect
181181
end
182+
183+
def test_seeds_defaults_to_primary
184+
config = HashConfig.new("default_env", "primary", { adapter: "abstract" })
185+
assert_equal true, config.seeds?
186+
187+
config = HashConfig.new("default_env", "primary", { adapter: "abstract", seeds: false })
188+
assert_equal false, config.seeds?
189+
190+
config = HashConfig.new("default_env", "primary", { adapter: "abstract", seeds: true })
191+
assert_equal true, config.seeds?
192+
193+
config = HashConfig.new("default_env", "secondary", { adapter: "abstract" })
194+
config.stub(:primary?, false) do # primary? will return nil without proper Base.configurations
195+
assert_equal false, config.seeds?
196+
end
197+
198+
config = HashConfig.new("default_env", "secondary", { adapter: "abstract", seeds: false })
199+
config.stub(:primary?, false) do # primary? will return nil without proper Base.configurations
200+
assert_equal false, config.seeds?
201+
end
202+
203+
config = HashConfig.new("default_env", "secondary", { adapter: "abstract", seeds: true })
204+
config.stub(:primary?, false) do # primary? will return nil without proper Base.configurations
205+
assert_equal true, config.seeds?
206+
end
207+
end
182208
end
183209
end
184210
end

guides/source/active_record_migrations.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,15 +1267,15 @@ only perform the necessary tasks once.
12671267
load the schema, run any pending migrations, dump the updated schema, and
12681268
finally load the seed data. See the [Seeding Data
12691269
documentation](#migrations-and-seed-data) for more details.
1270-
* If both the database and tables exist but the seed data has not been loaded,
1271-
the command will only load the seed data.
1272-
* If the database, tables, and seed data are all in place, the command will do
1273-
nothing.
1274-
1275-
NOTE: Once the database, tables, and seed data are all established, the command
1276-
will not try to reload the seed data, even if the previously loaded seed data or
1277-
the existing seed file have been altered or deleted. To reload the seed data,
1278-
you can manually run `bin/rails db:seed`.
1270+
* If the database and tables exist, the command will do nothing.
1271+
1272+
Once the database and tables exist, the `db:prepare` task will not try to reload
1273+
the seed data, even if the previously loaded seed data or the existing seed file
1274+
have been altered or deleted. To reload the seed data, you can manually run
1275+
`bin/rails db:seed`.
1276+
1277+
NOTE: This task will only load seeds if one of the databases or tables created
1278+
is a primary database for the environment or is configured with `seeds: true`.
12791279

12801280
### Resetting the Database
12811281

0 commit comments

Comments
 (0)