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

Remove databases package from config docs #2092

Merged
merged 17 commits into from
Dec 16, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update
aminalaee committed Apr 18, 2023
commit d055e7adf502f93e9520f4d7ddc221512526d8cb
33 changes: 9 additions & 24 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -4,9 +4,7 @@ following [the twelve-factor pattern][twelve-factor].
Configuration should be stored in environment variables, or in a ".env" file
that is not committed to source control.

**app.py**:

```python
```python title="app.py"
from sqlalchemy import create_engine
from starlette.applications import Starlette
from starlette.config import Config
@@ -25,9 +23,7 @@ engine = create_engine(DATABASE_URL)
...
```

**.env**:

```shell
```shell title=".env"
# Don't commit this to source control.
# Eg. Include ".env" in your `.gitignore` file.
DEBUG=True
@@ -94,12 +90,10 @@ is set *after* the point that it has already been read by the configuration.
If you're using `pytest`, then you can setup any initial environment in
`tests/conftest.py`.

**tests/conftest.py**:

```python
```python title="tests/conftest.py"
from starlette.config import environ

environ['TESTING'] = 'TRUE'
environ['DEBUG'] = 'TRUE'
```

## Reading prefixed environment variables
@@ -132,24 +126,19 @@ we can start to structure an application.
First, let's keep our settings, our database table definitions, and our
application logic separated:

**myproject/settings.py**:

```python
```python title="myproject/settings.py"
from starlette.config import Config
from starlette.datastructures import Secret

config = Config(".env")

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')
```

**myproject/tables.py**:

```python
```python title="myproject/tables.py"
import sqlalchemy

# Database table definitions.
@@ -160,9 +149,7 @@ organisations = sqlalchemy.Table(
)
```

**myproject/app.py**

```python
```python title="myproject/app.py"
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
@@ -192,16 +179,14 @@ Now let's deal with our test configuration.
We'd like to create a new test database every time the test suite runs,
and drop it once the tests complete. We'd also like to ensure

**tests/conftest.py**:

```python
```python title="tests/conftest.py"
from starlette.config import environ
from starlette.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy_utils import create_database, database_exists, drop_database

# This line would raise an error if we use it after 'settings' has been imported.
environ['TESTING'] = 'TRUE'
environ['DEBUG'] = 'TRUE'

from myproject import settings
from myproject.app import app