diff --git a/activerecord/lib/active_record/database_configurations/database_config.rb b/activerecord/lib/active_record/database_configurations/database_config.rb index 0a60c33e6a7a4..d6f0958117486 100644 --- a/activerecord/lib/active_record/database_configurations/database_config.rb +++ b/activerecord/lib/active_record/database_configurations/database_config.rb @@ -99,6 +99,10 @@ def schema_cache_path def use_metadata_table? raise NotImplementedError end + + def seeds? + raise NotImplementedError + end end end end diff --git a/activerecord/lib/active_record/database_configurations/hash_config.rb b/activerecord/lib/active_record/database_configurations/hash_config.rb index 2539f4c72153a..7a9b05bd30a20 100644 --- a/activerecord/lib/active_record/database_configurations/hash_config.rb +++ b/activerecord/lib/active_record/database_configurations/hash_config.rb @@ -130,6 +130,14 @@ def primary? # :nodoc: Base.configurations.primary?(name) end + # Determines whether the db:prepare task should seed the database from db/seeds.rb. + # + # If the `seeds` key is present in the config, `seeds?` will return its value. Otherwise, it + # will return `true` for the primary database and `false` for all other configs. + def seeds? + configuration_hash.fetch(:seeds, primary?) + end + # Determines whether to dump the schema/structure files and the filename that # should be used. # diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index a968c9c15ff98..2b8ff3ad4b340 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -180,7 +180,7 @@ def prepare_all each_current_configuration(env) do |db_config| database_initialized = initialize_database(db_config) - seed = true if database_initialized + seed = true if database_initialized && db_config.seeds? end each_current_environment(env) do |environment| diff --git a/activerecord/test/cases/database_configurations/hash_config_test.rb b/activerecord/test/cases/database_configurations/hash_config_test.rb index 82fb442631ab0..175a41f60327b 100644 --- a/activerecord/test/cases/database_configurations/hash_config_test.rb +++ b/activerecord/test/cases/database_configurations/hash_config_test.rb @@ -179,6 +179,32 @@ def test_inspect_does_not_show_secrets config = HashConfig.new("default_env", "primary", { adapter: "abstract", password: "hunter2" }) assert_equal "#", config.inspect end + + def test_seeds_defaults_to_primary + config = HashConfig.new("default_env", "primary", { adapter: "abstract" }) + assert_equal true, config.seeds? + + config = HashConfig.new("default_env", "primary", { adapter: "abstract", seeds: false }) + assert_equal false, config.seeds? + + config = HashConfig.new("default_env", "primary", { adapter: "abstract", seeds: true }) + assert_equal true, config.seeds? + + config = HashConfig.new("default_env", "secondary", { adapter: "abstract" }) + config.stub(:primary?, false) do # primary? will return nil without proper Base.configurations + assert_equal false, config.seeds? + end + + config = HashConfig.new("default_env", "secondary", { adapter: "abstract", seeds: false }) + config.stub(:primary?, false) do # primary? will return nil without proper Base.configurations + assert_equal false, config.seeds? + end + + config = HashConfig.new("default_env", "secondary", { adapter: "abstract", seeds: true }) + config.stub(:primary?, false) do # primary? will return nil without proper Base.configurations + assert_equal true, config.seeds? + end + end end end end diff --git a/guides/source/active_record_migrations.md b/guides/source/active_record_migrations.md index 0f039f5f38331..cba5ac8a1cba5 100644 --- a/guides/source/active_record_migrations.md +++ b/guides/source/active_record_migrations.md @@ -1267,15 +1267,15 @@ only perform the necessary tasks once. load the schema, run any pending migrations, dump the updated schema, and finally load the seed data. See the [Seeding Data documentation](#migrations-and-seed-data) for more details. -* If both the database and tables exist but the seed data has not been loaded, - the command will only load the seed data. -* If the database, tables, and seed data are all in place, the command will do - nothing. - -NOTE: Once the database, tables, and seed data are all established, the command -will not try to reload the seed data, even if the previously loaded seed data or -the existing seed file have been altered or deleted. To reload the seed data, -you can manually run `bin/rails db:seed`. +* If the database and tables exist, the command will do nothing. + +Once the database and tables exist, the `db:prepare` task will not try to reload +the seed data, even if the previously loaded seed data or the existing seed file +have been altered or deleted. To reload the seed data, you can manually run +`bin/rails db:seed`. + +NOTE: This task will only load seeds if one of the databases or tables created +is a primary database for the environment or is configured with `seeds: true`. ### Resetting the Database