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

Update DB commands to support multiple gateways per slice #232

Merged
merged 9 commits into from
Sep 22, 2024

Conversation

timriley
Copy link
Member

@timriley timriley commented Sep 18, 2024

Update the CLI for the DB gateway support introduced in hanami/hanami#1452:

Add --gateway option to the following CLI commands:

  • db structure dump
  • db structure load
  • db create
  • db drop
  • db migrate
  • db version
  • generate migration

These commands will operate on an individual gateway when the --gateway is given. Otherwise, they will operate on all the gateways for a given app or slice (or the app and all slices) based on the other args given, as before.

The above commands work with the following new file structures:

  • Migrations for gateways in config/db/[gateway_name]_migrate/ directories
  • Structure files for gateways at config/db/[gateway_name]_structure.sql

The db prepare and db seed commands do not support a --gateway option. This is because these commands are intended to operate on an app or slice as a whole. For example, there is only one db/seeds.rb file for a slice, and it should seed the tables across the slice's gateways as appropriate.

@timriley timriley marked this pull request as ready for review September 21, 2024 12:33
Comment on lines +25 to +41
def call(name:, slice: nil, gateway: nil)
if slice
generator.call(
key: name,
namespace: slice,
base_path: fs.join("slices", inflector.underscore(slice)),
gateway: gateway
)
else
generator.call(
key: name,
namespace: app.namespace,
base_path: "app",
gateway: gateway
)
end
end
Copy link
Member Author

Choose a reason for hiding this comment

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

@cllns a note for you — to be able to pass this extra gateway: gateway arg when calling the generator, I had to copy the entire #call implementation from the base generate command class. It would be good if we could allow individual generate command classes to be able to provide extra args to the generator without having to do this. An opportunity for a refactor in the future?

Comment on lines -91 to -95
def table_exists?(slice, table_name)
slice["db.gateway"].connection
.fetch("PRAGMA table_info(#{table_name})")
.to_a.any?
end
Copy link
Member Author

@timriley timriley Sep 21, 2024

Choose a reason for hiding this comment

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

Being able to replace these with database-specific methods with a single e.g. Hanami.app["db.gateway"].connection.tables.include?(:posts) invocation is so much nicer. I wish I had explored Sequel's API a little more before I wrote these methods in the first place!

@timriley timriley merged commit 9caebfb into main Sep 22, 2024
6 checks passed
@timriley timriley deleted the multiple-gateways branch September 22, 2024 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant