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

Config db from url #269

Merged
merged 3 commits into from
Nov 10, 2024
Merged

Conversation

jpmartinspt
Copy link
Contributor

Add functionality so one can configure a database from a URL.

Fixes #257

@@ -256,12 +256,17 @@ module Marten
@csrf ||= GlobalSettings::CSRF.new
end

# Allows to configure a specific database connection for the application.
def database(id = DB::Connection::DEFAULT_CONNECTION_NAME, &)
def database(id = DB::Connection::DEFAULT_CONNECTION_NAME, url : String | Nil = nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ellmetha might be missing something with the overloading as it's my first time fiddling with crystal, but I think we have to stick with url as a named parameter here, therefore configuring with config.database url: "postgres://my_user:my_db@localhost:1234/db"


params_map = uri.query_params.to_h

self.checkout_timeout = params_map.delete("checkout_timeout").try &.to_f64 || @checkout_timeout
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you want to raise a specific exception here in case one of these casts fails?

@treagod
Copy link
Contributor

treagod commented Oct 24, 2024

Hi! Before reviewing could you kindly fix the failing GitHub QA check? This should be done after formatting ./src/marten/conf/global_settings/database.cr 😊

@@ -256,12 +256,17 @@ module Marten
@csrf ||= GlobalSettings::CSRF.new
end

# Allows to configure a specific database connection for the application.
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 keep method descriptions? 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely, accidental removal :)

self.port = uri.port
self.user = uri.user
self.password = uri.password
self.name = uri.scheme == "sqlite" ? uri.host : uri.path[1..]?
Copy link
Member

Choose a reason for hiding this comment

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

It's worth noting that sqlite3 is a valid scheme for SQLite databases. We should definitely account for that!


params_map = uri.query_params.to_h

self.checkout_timeout = params_map.delete("checkout_timeout").try &.to_f64 || @checkout_timeout
Copy link
Member

Choose a reason for hiding this comment

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

It would probably be safer to assign these values only when they are present in the query params:

Suggested change
self.checkout_timeout = params_map.delete("checkout_timeout").try &.to_f64 || @checkout_timeout
if params_map.has_key?("checkout_timeout")
self.checkout_timeout = params_map.delete("checkout_timeout").try &.to_f64
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless I'm missing something, the compiler does not like this assignment as it still thinks delete could return a Nil.

Copy link
Contributor Author

@jpmartinspt jpmartinspt Oct 31, 2024

Choose a reason for hiding this comment

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

I could also just deal with the deletion of these keys when setting self.options, and just read the value from the map inside the if clause, if that sounds better.

Copy link
Member

Choose a reason for hiding this comment

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

Unless I'm missing something, the compiler does not like this assignment as it still thinks delete could return a Nil.

Yes, you have to check that you are in the presence a non-nil value. For example:

Suggested change
self.checkout_timeout = params_map.delete("checkout_timeout").try &.to_f64 || @checkout_timeout
if !(v = params_map.delete("checkout_timeout").try &.to_f64).nil?
self.checkout_timeout = v
end

@jpmartinspt
Copy link
Contributor Author

@treagod @ellmetha I believe I've addressed all the comments. Feel free to give the review another pass.

Copy link
Member

@ellmetha ellmetha left a comment

Choose a reason for hiding this comment

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

Thank you for adding this! 🙏

@ellmetha ellmetha merged commit 88cfbdc into martenframework:main Nov 10, 2024
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Make it possible to configure databases through connection strings
3 participants