Skip to content

Conversation

flavorjones
Copy link
Member

@flavorjones flavorjones commented Oct 19, 2024

Motivation / Background

Issue #53348 describes an issue where, after adding Solid Cache to an application, running db:prepare in production re-loaded the seeds, potentially leading to data loss.

Closes #53348

Detail

This PR 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 #49480 along the way.

Additional information

See previous rejected proposal at #53354

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@flavorjones flavorjones force-pushed the flavorjones-distinguish-seeded-databases branch 2 times, most recently from 5b9e742 to 8f08520 Compare October 19, 2024 14:13
@zzak
Copy link
Member

zzak commented Oct 19, 2024

I think this is also related to #53059.

@flavorjones
Copy link
Member Author

flavorjones commented Oct 20, 2024

@zzak I think the issues rhyme, but have a different root cause.

In #53348, Solid Cache still uses Rails to manage the schema (it just doesn't use/need seeds). In #53059 management of both is external to Rails.

@zzak
Copy link
Member

zzak commented Oct 21, 2024

Ahh yeah, thanks for double checking 🙇

@MaxLap
Copy link
Contributor

MaxLap commented Oct 24, 2024

I would suggest the following for prepare_all. This way, if an app has 2 seedable database, one already with data, then the seed would not be run, which is safer.

def prepare_all
  seed_blocked = false
  seed_needed = false

  dump_db_configs = []
  each_current_configuration(env) do |db_config|
    database_initialized = initialize_database(db_config)
    if db_config.seeds?
      if database_initialized
        seed_needed = true
      else
        seed_blocked = true
      end
    end
  end

  each_current_environment(env) do |environment|
    db_configs_with_versions(environment).sort.each do |version, db_configs|
      dump_db_configs |= db_configs
      db_configs.each do |db_config|
        with_temporary_pool(db_config) do
          migrate(version)
        end
      end
    end
  end
  # Dump schema for databases that were migrated.
  if ActiveRecord.dump_schema_after_migration
    dump_db_configs.each do |db_config|
      with_temporary_pool(db_config) do
        dump_schema(db_config)
      end
    end
  end
  load_seed if seed_needed && !seed_blocked
end

I think this PR as a whole is an important change to have for rails 8.0 to avoid this problem happening to anyone adding a Solid* gem.

@flavorjones flavorjones force-pushed the flavorjones-distinguish-seeded-databases branch from 8f08520 to d6451ef Compare October 24, 2024 13:39
@flavorjones
Copy link
Member Author

Rebased.

@MaxLap
Copy link
Contributor

MaxLap commented Oct 24, 2024

Rebased.

Since that was only 17 minutes after my suggestion, I understood that as meaning that you added my suggestion to the PR, but I don't see it. I'm just mentioning in case that is a mistake.

@flavorjones
Copy link
Member Author

@MaxLap I'm not sure I agree that the scenario you're describing above is common enough to warrant the additional complexity. Maybe something that, for posterity, is better brought up in the original issue?

I invite you to please create a new PR if you have strong opinions, I'm not competitive about this and showing working code and tests can drive the overall conversation very well. For now I'm waiting for feedback from core before editing this further.

@rafaelfranca rafaelfranca added this to the 8.0.0 milestone Oct 25, 2024
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
@rafaelfranca rafaelfranca force-pushed the flavorjones-distinguish-seeded-databases branch from d6451ef to 01cc805 Compare October 25, 2024 19:03
@rafaelfranca rafaelfranca merged commit d7201f2 into rails:main Oct 25, 2024
3 checks passed
rafaelfranca added a commit that referenced this pull request Oct 25, 2024
…eded-databases

`db:prepare` no longer loads seed when non-primary db is created
@flavorjones flavorjones deleted the flavorjones-distinguish-seeded-databases branch October 27, 2024 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails 8: db:prepare re-run seeds when SolidCache is added
4 participants