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

Add support for DATABASE_URL #559

Merged
merged 10 commits into from
Dec 23, 2021

Conversation

girardinsamuel
Copy link
Contributor

@girardinsamuel girardinsamuel commented Dec 22, 2021

Fix #307.

Here is a proposal for supporting DATABASE_URL:

Import the helper

from masoniteorm.config import db_url

This helper will return a full config dict that you can directly use in DATABASES config:

DATABASES = {
    "default": "mysql",
    "mysql": db_url(),
}

Here as no url string is given it will automatically use DATABASE_URL env (as often exposed by PaaS service: Heroku...)

You want to use an other env variable ? No problem:

DATABASES = {
    "default": "mysql",
    "mysql": db_url(os.getenv("DB_URL")),
}

Of course you can specify an url directly:

DATABASES = {
    "default": "mysql",
    "mysql": db_url("mysql://root:@localhost:3306/orm"),
}

...and you can specify additional parameters:

DATABASES = {
    "default": "mysql",
    "mysql": db_url("mysql://root:@localhost:3306/orm", log_queries=True, prefix="", options={}),
}

...or you can just merge it with some other options (not related to the url):

DATABASES = {
    "default": "mysql",
    "mysql": {
        **db_url("mysql://root:@localhost:3306/orm"),
        "prefix": "",
        "options": {},
        log_queries=True
     }
}

It support all drivers.

For SQLite it's:

db_url("sqlite://orm.sqlite3")

🎉 🚀

If it's okay, then I could add some unit tests.

# this is a special case, because if we pass this URL into
# urlparse, urlparse will choke trying to interpret "memory"
# as a port number
if url == "sqlite://:memory:":
Copy link
Member

Choose a reason for hiding this comment

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

We could probably check for a couple of string here. Maybe also just :memory: too

Copy link
Member

Choose a reason for hiding this comment

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

Nobody is going to remember this string without referring to the docs

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 we could check just :memory:. What do you mean by couple of strings lol ?

Copy link
Member

Choose a reason for hiding this comment

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

if url in ("sqlite://:memory:", ":memory:")

I am also assuming that sqlite://:memory: is the officially supported string. I just want to be able to remember a string and I'm not going to remember sqlite://:memory: lol

Copy link
Member

Choose a reason for hiding this comment

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

I'll forget one of the 3 : and pull my hair out why its not working

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh okay got it. Actually you can already go the simple waysqlite:// but I can add sqlite://memory also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now you can use those for SQLite in memory:

db_url("sqlite://")
db_url("sqlite://memory")
db_url("sqlite://:memory:")  # the official one

And for a SQLite classic db file:

db_url("sqlite://orm.sqlite3")

Copy link
Member

@josephmancuso josephmancuso left a comment

Choose a reason for hiding this comment

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

Looks great! Great implementation

@girardinsamuel
Copy link
Contributor Author

Looks great! Great implementation

Unit tests have been added ✅ !

@josephmancuso josephmancuso merged commit eec5de6 into MasoniteFramework:2.0 Dec 23, 2021
@girardinsamuel girardinsamuel deleted the feat/307 branch December 23, 2021 11:15
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.

None yet

2 participants