-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Kludex
merged 17 commits into
encode:master
from
aminalaee:remove-databases-from-config-docs
Dec 16, 2023
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
49a55ae
Remove databases package from config docs
aminalaee ecb310d
update
aminalaee e113afe
remove unused section
aminalaee c537b8a
Merge branch 'master' into remove-databases-from-config-docs
aminalaee 7c43d99
Merge branch 'master' into remove-databases-from-config-docs
aminalaee d055e7a
update
aminalaee 7010bd6
Merge branch 'master' into remove-databases-from-config-docs
aminalaee a0bdb07
Merge branch 'remove-databases-from-config-docs' of github.com:aminal…
aminalaee 73b5cb6
Merge branch 'master' into remove-databases-from-config-docs
aminalaee fc596b1
Update docs/config.md
aminalaee 3547c2b
add databases URL
aminalaee b6885e7
Merge branch 'master' of github.com:aminalaee/starlette into remove-d…
aminalaee 0ae4d34
Merge branch 'master' of github.com:encode/starlette into remove-data…
aminalaee 38873d9
Update config.md
aminalaee ca2a08f
Merge branch 'master' into remove-databases-from-config-docs
musale 571c8f6
Merge branch 'master' into remove-databases-from-config-docs
Kludex ad4480f
Merge branch 'master' into remove-databases-from-config-docs
Kludex 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
... | ||
``` | ||
|
||
|
@@ -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 | ||
``` | ||
|
@@ -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' | ||
aminalaee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
## CommaSeparatedStrings | ||
|
@@ -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' | ||
|
@@ -145,7 +144,6 @@ application logic separated: | |
**myproject/settings.py**: | ||
|
||
```python | ||
import databases | ||
from starlette.config import Config | ||
from starlette.datastructures import Secret | ||
|
||
|
@@ -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') | ||
aminalaee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
**myproject/tables.py**: | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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) | ||
aminalaee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
aminalaee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@pytest.fixture() | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.