Skip to content

Skip loading seeds for databases that are managed by Rails - #53354

Closed
flavorjones wants to merge 2 commits into
rails:mainfrom
flavorjones:flavorjones-distinguish-framework-databases
Closed

flavorjones wants to merge 2 commits into
rails:mainfrom
flavorjones:flavorjones-distinguish-framework-databases

Conversation

@flavorjones

Copy link
Copy Markdown
Member

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.

This PR proposes that any databases that are managed by Rails should behave differently in this case, and seeds should not be run when a Solid {Cache,Queue,Cable} database is added to the config and initialized.

Detail

Specifically, this PR adds a new method DatabaseConfig#framework? which defaults to false, but is set to true for the Solid-* databases in Rails's default production database config.

Additional information

If this proposal is merged, the docs for the Solid-* gems should be updated to inform users to add the framework: true value to their database config.

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.

For cases where it may be useful to distinguish between databases that
are fully managed by Rails (e.g., Solid {Cache,Queue,Cable}) and those
that the application developer owns.
@MaxLap

MaxLap commented Oct 17, 2024

Copy link
Copy Markdown
Contributor

The primary database is already special-cased in some place. (Such has for falling back to DATABASE_URL)

In my opinion, if that primary database already exist and has data, then no seed should ever happen automatically again.

If there is no such database in the config, you are in complicated custom territory. Maybe then a "seeded: true" in the config for database of interests would make sense. This way, you avoid annoying everyone with manually putting in a default config they will never have to change.

I guess at that point, the idea is just that primary is the only database with seeded: true as default.

The only thing to decide would then be what happens when there is more than one seeded: true. (I would vote for only auto-seed if all seeded: true database are empty)

@flavorjones

Copy link
Copy Markdown
Member Author

@MaxLap please note that this PR is slightly different from what I suggested in your original issue, please take a look.

I appreciate your suggestion that the only change that's needed is to skip seed unless the database config return true from primary?, and that would be a simpler change. Please do open an alternative PR for comparison if you have time!

However, since the Solid gems introduce (for the first time) databases that are managed by the framework for the application developer, I'd like to get feedback on this approach from someone on the core team. I think that distinction in ownership is worth having a conversation about even if we end up with a simpler solution in this case.

@MaxLap

MaxLap commented Oct 17, 2024

Copy link
Copy Markdown
Contributor

@flavorjones I saw the difference but wanted to chime in with a solution I felt more general. It wasn't meant to be the "answer to the PR/question", sorry if it felt that way.

I completely agree that this needs feedback from core.

@MatheusRich

Copy link
Copy Markdown
Contributor

@flavorjones please add a changelog entry

@flavorjones

Copy link
Copy Markdown
Member Author

@MatheusRich It's not clear to me that a CHANGELOG entry is needed, since this fixes functionality introduced in Rails 8. I'd like to wait for feedback from the core team before adding one.

database_initialized = initialize_database(db_config)

seed = true if database_initialized
seed = true if database_initialized && !db_config.framework?

@rafaelfranca rafaelfranca Oct 18, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is that this seed is a regular boolean when it should not. It isn't taking in consideration that the application can have multiple database. The other problem is that seeds are global and don't specify to which database those seeds are for.

I really think it is a bad idea to run a task that can run the seeds in production. Why are we doing that? Seeds are mostly for development.

Adding a flag to tell there are framework databases can fix the symptoms, but the root cause is that we are running db:prepare in production and it can run seeds when any database is created which can wipe all your data.

Any other primary database added after the application is already in production for a while would wipe the primary database as well, and they should not mark their database as framework: true

@dhh

dhh commented Oct 18, 2024

Copy link
Copy Markdown
Member

Hmm, I think this is a little too indirect. I'd prefer to either just run the seeds for the primary database or have something like seeds: true. Or maybe both! By default, seeds: true will be the case for the primary? and then you could also toggle it on for any other definition.

@rafaelfranca

Copy link
Copy Markdown
Member

Yeah. Seeds are the problem here, I think that boolean needs to be more nuanced than the "true if any database we care about was created, or false otherwise".

@flavorjones

Copy link
Copy Markdown
Member Author

Thanks for the feedback. Will try again in a different PR.

@flavorjones

Copy link
Copy Markdown
Member Author

Next attempt is at #53379

@flavorjones
flavorjones deleted the flavorjones-distinguish-framework-databases branch November 29, 2024 15:33
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.

6 participants