Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up database generators implementation #51765

Merged
merged 2 commits into from May 13, 2024

Conversation

andrewn617
Copy link
Contributor

@andrewn617 andrewn617 commented May 8, 2024

Motivation / Background

A lot of complexity has been added to the app generator and change generator for devcontainers, specifically around database setup. My goal here is to simplify this.

Detail

First, I remove support for al the java and proprietary databases from these generators. Going forward, these generators will just support our core databases: sqlite3, mysql, postgresql and trilogy.

Then, I extracted all the database info into an object - gems, docker dependencies, and devcontainer setup. Then we just have to do Database.build(options[:database]) to get all the needed database info for a generator. This reduces the huge amount of case statements littering the code.

A good side effect, is now the ChangeGenerator can just create its own database object. Previously, the ChangeGenerator was using the Devcontainer mixin to update the devcontainer database config. But that is sketchy because the change generator does not know everything about the app, such as if the app uses redis. So it could be used to generate incorrect devcontainer config if someone started using those methods. Now that logic is separate from the database logic and the change generator only knows how to modify the DB setup.

This also allowed me to get rid of the hardcoded list of all the db features and service names which we need to iterate over in the change generator to delete the old db config.

Additional information

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.

@rails-bot rails-bot bot added the railties label May 8, 2024
@andrewn617 andrewn617 force-pushed the refactor-devcontainer-implementation branch 4 times, most recently from 8d3febe to 297c0a6 Compare May 8, 2024 15:16
def devcontainer_db_service_yaml(**options)
return unless service = devcontainer_database.service

{ devcontainer_database.name => service }.to_yaml(**options)[4..-1]
Copy link
Member

Choose a reason for hiding this comment

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

What does 4 and 1 means here?

Copy link
Contributor Author

@andrewn617 andrewn617 May 8, 2024

Choose a reason for hiding this comment

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

It's the leading --- that separate the body of the doc from directives. I don't think to_yaml takes an option to remove it.

if db_service
compose_config["services"].merge!(db_service)
compose_config["volumes"] = { db_volume_name_for_devcontainer => nil }.merge(compose_config["volumes"] || {})
if devcontainer_database.service
Copy link
Member

Choose a reason for hiding this comment

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

Can we go a step further and also refactor all the other places that have different behavior depending on the databsae in the generator? There is no reason why this object should be devcontaienr specific. We need to find the gem for the database, the docker image for the database, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I can do that. The only slightly annoying thing is including all the other db that aren't available for devcontainer. I will work on it when I find some time this week.

Copy link
Member

Choose a reason for hiding this comment

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

We can drop those databases. We already decided we would do, but never had a reason to. Now we have that reason.

case database
when "mysql" then "default-libmysqlclient-dev"
when "postgresql" then "libpq-dev"
class Database
Copy link
Member

Choose a reason for hiding this comment

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

Can we move this entire class to railties/lib/rails/generators/database.rb? There is a lot of duplication still on that file that we can remove now that we have a abstraction to the database on generators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is still WIP - sorry got distracted by something else in the middle of working on it this morn. I'll ping you when it is ready for another look

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca Alright ready for another review

@andrewn617 andrewn617 force-pushed the refactor-devcontainer-implementation branch from 148304a to b6ad9b9 Compare May 13, 2024 17:41
@andrewn617 andrewn617 changed the title Refactoring Devcontainer implementation Clean up database generators implementation May 13, 2024
@andrewn617 andrewn617 force-pushed the refactor-devcontainer-implementation branch from b6ad9b9 to 1d9c431 Compare May 13, 2024 17:55
…ters from the new and db:system:change commands. The supported options are sqlite3, mysql, postgresql and trilogy.
Extract all the DB information (gems, dockerfile packages, devcontainer etc.) into an object. This let's us remove the growing number of case statements in this code.
@andrewn617 andrewn617 force-pushed the refactor-devcontainer-implementation branch from 1d9c431 to c1bc7ee Compare May 13, 2024 18:08
@rafaelfranca rafaelfranca merged commit cb1c789 into rails:main May 13, 2024
3 checks passed
@rafaelfranca rafaelfranca deleted the refactor-devcontainer-implementation branch May 13, 2024 18:19
rafaelfranca added a commit that referenced this pull request May 13, 2024
…tation

Clean up database generators implementation
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.

None yet

2 participants