Skip to content

Remove databases package from config docs #2092

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

Merged
merged 17 commits into from
Dec 16, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ that is not committed to source control.
**app.py**:

```python
import databases

from sqlalchemy import create_engine
from starlette.applications import Starlette
from starlette.config import Config
from starlette.datastructures import CommaSeparatedStrings, Secret
Expand All @@ -17,11 +16,12 @@ from starlette.datastructures import CommaSeparatedStrings, Secret
config = Config(".env")

DEBUG = config('DEBUG', cast=bool, default=False)
DATABASE_URL = config('DATABASE_URL', cast=databases.DatabaseURL)
DATABASE_URL = config('DATABASE_URL')
SECRET_KEY = config('SECRET_KEY', cast=Secret)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=CommaSeparatedStrings)

app = Starlette(debug=DEBUG)
engine = create_engine(DATABASE_URL)
...
```

Expand All @@ -31,7 +31,7 @@ app = Starlette(debug=DEBUG)
# Don't commit this to source control.
# Eg. Include ".env" in your `.gitignore` file.
DEBUG=True
DATABASE_URL=postgresql://localhost/myproject
DATABASE_URL=postgresql://user:password@localhost:5432/database
SECRET_KEY=43n080musdfjt54t-09sdgr
ALLOWED_HOSTS=127.0.0.1, localhost
```
Expand Down Expand Up @@ -69,9 +69,7 @@ in their representations.
```python
>>> from myproject import settings
>>> settings.DATABASE_URL
DatabaseURL('postgresql://admin:**********@192.168.0.8/my-application')
>>> str(settings.DATABASE_URL)
'postgresql://admin:[email protected]/my-application'
'postgresql://user:password@localhost:5432/database'
```

## CommaSeparatedStrings
Expand Down Expand Up @@ -119,6 +117,7 @@ You can namespace the environment variables by setting `env_prefix` argument.

```python title="myproject/settings.py"
import os

from starlette.config import Config

os.environ['APP_DEBUG'] = 'yes'
Expand All @@ -145,7 +144,6 @@ application logic separated:
**myproject/settings.py**:

```python
import databases
from starlette.config import Config
from starlette.datastructures import Secret

Expand All @@ -155,9 +153,7 @@ DEBUG = config('DEBUG', cast=bool, default=False)
TESTING = config('TESTING', cast=bool, default=False)
SECRET_KEY = config('SECRET_KEY', cast=Secret)

DATABASE_URL = config('DATABASE_URL', cast=databases.DatabaseURL)
if TESTING:
DATABASE_URL = DATABASE_URL.replace(database='test_' + DATABASE_URL.database)
DATABASE_URL = config('DATABASE_URL')
```

**myproject/tables.py**:
Expand All @@ -180,6 +176,7 @@ from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.routing import Route

from myproject import settings


Expand Down Expand Up @@ -225,13 +222,14 @@ def setup_test_database():
"""
Create a clean test database every time the tests are run.
"""
url = str(settings.DATABASE_URL)
engine = create_engine(url)
assert not database_exists(url), 'Test database already exists. Aborting tests.'
create_database(url) # Create the test database.
metadata.create_all(engine) # Create the tables.
yield # Run the tests.
drop_database(url) # Drop the test database.
engine = create_engine(settings.DATABASE_URL)
assert not database_exists(settings.DATABASE_URL), 'Test database already exists. Aborting tests.'
create_database(settings.DATABASE_URL) # Create the test database.
metadata.create_all(engine) # Create the tables.

yield # Run the tests.

drop_database(settings.DATABASE_URL) # Drop the test database.


@pytest.fixture()
Expand Down