Skip to content

Commit

Permalink
db:prepare no longer loads seed when non-primary db is created
Browse files Browse the repository at this point in the history
Introduces a new database config property `seeds` to control whether
seeds are loaded during `db:prepare` which defaults to `true` for
primary database configs and `false otherwise.

Previously, the `db:prepare` task would load seeds whenever a new
database is created, leading to potential loss of data if a database
is added to an existing environment.

Also, update the Active Record Migrations guide to more accurately
describe what the `db:prepare` task does, correcting some
misinformation introduced in rails#49480 along the way.

Closes rails#53348
  • Loading branch information
flavorjones authored and rafaelfranca committed Oct 25, 2024
1 parent 7fac5d1 commit 01cc805
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def schema_cache_path
def use_metadata_table?
raise NotImplementedError
end

def seeds?
raise NotImplementedError
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ def test_inspect_does_not_show_secrets
config = HashConfig.new("default_env", "primary", { adapter: "abstract", password: "hunter2" })
assert_equal "#<ActiveRecord::DatabaseConfigurations::HashConfig env_name=default_env name=primary adapter_class=ActiveRecord::ConnectionAdapters::AbstractAdapter>", 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
18 changes: 9 additions & 9 deletions guides/source/active_record_migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 01cc805

Please sign in to comment.