Repository navigation
Use Hanami::DB::DatabaseURL in db commands #463
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
Draft
parndt
wants to merge
6
commits into
main
Choose a base branch
from
database-url-helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b10ff8
Use Hanami::DB::DatabaseURL in db command utils
parndt eb3e95d
Raise clearer errors for unsupported or invalid database URLs
parndt 9871c5f
Raise a clear error when hanami-db is too old
parndt 3a1dfe1
Use hanami-cli's hanami-db in the database E2E app
parndt 87415da
Remove the hanami-db version check from db commands
parndt a51ffc5
Add PR number to changelog entry for database URL errors
parndt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "uri" | ||
| require "delegate" | ||
|
|
||
| module Hanami | ||
|
|
@@ -23,35 +22,57 @@ def sql | |
| end | ||
| end | ||
|
|
||
| DATABASE_CLASS_RESOLVER = Hash.new { |_, key| | ||
| raise "#{key} is not a supported db scheme" | ||
| }.update( | ||
| "sqlite" => -> { | ||
| DATABASE_CLASS_RESOLVER = { | ||
| sqlite: -> { | ||
| require_relative("sqlite") | ||
| Sqlite | ||
| }, | ||
| "postgres" => -> { | ||
| postgres: -> { | ||
| require_relative("postgres") | ||
| Postgres | ||
| }, | ||
| "postgresql" => -> { | ||
| require_relative("postgres") | ||
| Postgres | ||
| }, | ||
| "mysql" => -> { | ||
| require_relative("mysql") | ||
| Mysql | ||
| }, | ||
| "mysql2" => -> { | ||
| mysql: -> { | ||
| require_relative("mysql") | ||
| Mysql | ||
| } | ||
| ).freeze | ||
| }.freeze | ||
|
|
||
| # Matches a URL's scheme, including any JDBC subprotocol, e.g. "jdbc:sqlserver" | ||
| DATABASE_SCHEME_MATCHER = /\A(?:jdbc:)?[a-z][a-z0-9+.-]*(?=:)/i | ||
| private_constant :DATABASE_SCHEME_MATCHER | ||
|
|
||
| def self.database_class(database_url) | ||
| database_scheme = URI(database_url.delete_prefix("jdbc:")).scheme | ||
| DATABASE_CLASS_RESOLVER[database_scheme].call | ||
| # Required here rather than at the top of this file, since every app command loads | ||
| # this file, including in apps that do not use hanami-db. | ||
| require "hanami-db" | ||
|
|
||
| adapter = Hanami::DB::DatabaseURL.adapter(database_url) | ||
|
|
||
| DATABASE_CLASS_RESOLVER.fetch(adapter) { | ||
| scheme = database_scheme(database_url) | ||
|
|
||
| raise Hanami::CLI::InvalidDatabaseURLError.new(scheme) unless parseable?(database_url) | ||
|
|
||
| raise Hanami::CLI::UnsupportedDatabaseSchemeError.new(scheme) | ||
|
Comment on lines
+54
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if these errors should actually be defined in hanami-db? That would be in keeping with the spirit of pushing all common logic down into that gem. And if we're creating new things now, we may as well start them there? |
||
| }.call | ||
| end | ||
|
|
||
| # Returns false when the URL cannot be parsed. Avoids raising from a `rescue`, since | ||
| # the URI error (whose message includes the URL) would become the new error's cause. | ||
| def self.parseable?(database_url) | ||
| Hanami::DB::DatabaseURL.uri(database_url) | ||
| true | ||
| rescue URI::Error | ||
| false | ||
| end | ||
| private_class_method :parseable? | ||
|
|
||
| # Returns the URL's scheme for use in error messages. Unlike parsing the URL, this | ||
| # cannot fail, and never exposes the credentials the URL may contain. | ||
| def self.database_scheme(database_url) | ||
| database_url.to_s[DATABASE_SCHEME_MATCHER] || "(none)" | ||
| end | ||
| private_class_method :database_scheme | ||
|
|
||
| def self.from_slice(slice:, system_call:) | ||
| provider = slice.container.providers[:db] | ||
|
|
@@ -87,10 +108,10 @@ def database_url | |
| slice.container.providers[:db].source.database_urls.fetch(gateway_name) | ||
| end | ||
|
|
||
| # JDBC URLs (required to connect via JRuby) nest the real URL after "jdbc:", e.g. | ||
| # "jdbc:postgresql://localhost/app", so parse that nested URL instead. | ||
| # Parses the URL nested in JDBC URLs (required to connect via JRuby), e.g. | ||
| # "jdbc:postgresql://localhost/app". | ||
| def database_uri | ||
| @database_uri ||= URI(database_url.delete_prefix("jdbc:")) | ||
| @database_uri ||= Hanami::DB::DatabaseURL.uri(database_url) | ||
| end | ||
|
|
||
| # JDBC drivers expect the user and password as query params, e.g. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
spec/unit/hanami/cli/commands/app/db/utils/database_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "hanami/cli/commands/app/db/utils/database" | ||
|
|
||
| RSpec.describe Hanami::CLI::Commands::App::DB::Utils::Database do | ||
| describe ".database_class" do | ||
| { | ||
| "sqlite://db/app.sqlite3" => "Sqlite", | ||
| "jdbc:sqlite:db/app.sqlite3" => "Sqlite", | ||
| "postgres://localhost/app" => "Postgres", | ||
| "postgresql://localhost/app" => "Postgres", | ||
| "jdbc:postgresql://localhost/app" => "Postgres", | ||
| "mysql://localhost/app" => "Mysql", | ||
| "mysql2://localhost/app" => "Mysql", | ||
| "jdbc:mysql://localhost/app" => "Mysql" | ||
| }.each do |url, class_name| | ||
| it "returns #{class_name} for #{url}" do | ||
| expect(described_class.database_class(url).name).to eq "Hanami::CLI::Commands::App::DB::Utils::#{class_name}" | ||
| end | ||
| end | ||
|
|
||
| it "raises an error for unsupported schemes, without including the URL's credentials" do | ||
| expect { described_class.database_class("oracle://user:secret@localhost/app") } | ||
| .to raise_error(Hanami::CLI::UnsupportedDatabaseSchemeError, "`oracle' is not a supported db scheme") | ||
| end | ||
|
|
||
| it "includes the JDBC subprotocol in the error for unsupported JDBC URLs" do | ||
| expect { described_class.database_class("jdbc:sqlserver://localhost;user=sa;password=secret") } | ||
| .to raise_error(Hanami::CLI::UnsupportedDatabaseSchemeError, "`jdbc:sqlserver' is not a supported db scheme") | ||
| end | ||
|
|
||
| it "raises an error for unsupported JDBC subprotocols" do | ||
| expect { described_class.database_class("jdbc:postgres://localhost/app") } | ||
| .to raise_error(Hanami::CLI::UnsupportedDatabaseSchemeError, "`jdbc:postgres' is not a supported db scheme") | ||
| end | ||
|
|
||
| it "raises an error for URLs that cannot be parsed, without including the URL's credentials" do | ||
| expect { described_class.database_class("postgres://user:se cret@localhost/app") } | ||
| .to raise_error(Hanami::CLI::InvalidDatabaseURLError) { |error| | ||
| expect(error.message).to start_with "`postgres' database URL could not be parsed" | ||
| expect(error.message).not_to include "cret" | ||
| expect(error.cause).to be_nil | ||
| } | ||
| end | ||
|
|
||
| it "raises an error for URLs without a scheme" do | ||
| expect { described_class.database_class("db/app.sqlite3") } | ||
| .to raise_error(Hanami::CLI::UnsupportedDatabaseSchemeError, "`(none)' is not a supported db scheme") | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs on regexps are good. Thank you!