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

Run db commands on test as well as development databases #247

Merged
merged 8 commits into from
Oct 26, 2024

Conversation

timriley
Copy link
Member

@timriley timriley commented Oct 25, 2024

It's a frustrating experience if users have to remember to run each of their hanami db commands again with -e test after running them on their development database.

To improve this, update a number of commands to automatically re-run on the test database after completing their work on the development database:

  • db create
  • db drop
  • db migrate
  • db prepare
  • db structure load

The re-run on test will only take place if these commands operate with the development environment. Running the commands in other environments (e.g. production or test) will run on the database in those environments only.

To achieve this "re-run on test" behaviour, at the end of these db commands, we invoke another process to call the relevant hanami db command again, but with the HANAMI_ENV=test environment variable set. For this re-run, all given CLI flags are preserved, but -e and --env are of course stripped.

The reason we have to take this approach is because right now there's no straightforward way to re-boot the Hanami app in a different environment, or stand up the database subsystem itself in a different environment.

I'd like to for us to make one of these possible (certainly the latter feels reasonable) in the future, but to make the user experience as good as possible for 2.2, we're taking a pragmatic approach here.

Resolves #215

@timriley timriley marked this pull request as ready for review October 26, 2024 09:32
Comment on lines +169 to +171
# Only invoke a new process if we've been called as `hanami`. This avoids awkward
# failures when testing commands via RSpec, for which the $0 is "/full/path/to/rspec".
return unless $0.end_with?("hanami")
Copy link
Member Author

@timriley timriley Oct 26, 2024

Choose a reason for hiding this comment

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

This is a bit hacky, but as commented here and in spec/support/hanami_cli_environment.rb, it was necessary because of our current testing approach. Given that I think we'll be able to switch to a cleaner overall approach in the future, I was comfortable with a few hacks like this being employed here.

@timriley timriley merged commit 712a1ad into main Oct 26, 2024
6 checks passed
@timriley timriley deleted the auto-apply-db-commands-on-test branch October 26, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When running database commands in development env, apply them to the test database too
1 participant