Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 522d665

Browse files
Version 0.1.3
1 parent aa6686c commit 522d665

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
Databases gives you simple asyncio support for a range of databases.
1616

17-
Currently PostgreSQL and MySQL are supported.
17+
It allows you to make queries using the powerful [SQLAlchemy Core](https://docs.sqlalchemy.org/en/latest/core/)
18+
expression language, and provides support for PostgreSQL, MySQL, and SQLite.
1819

1920
**Requirements**: Python 3.6+
2021

@@ -31,6 +32,7 @@ You can install the required database drivers with:
3132
```shell
3233
$ pip install databases[postgresql]
3334
$ pip install databases[mysql]
35+
$ pip install databases[sqlite]
3436
```
3537

3638
## Getting started
@@ -94,6 +96,9 @@ row = await database.fetch_one(query)
9496
query = notes.select()
9597
async for row in database.iterate(query):
9698
...
99+
100+
# Close all connection in the connection pool
101+
await database.disconnect()
97102
```
98103

99104
## Transactions
@@ -178,3 +183,55 @@ For a lower level API you can explicitly create force-rollback transactions:
178183
async with database.transaction(force_rollback=True):
179184
...
180185
```
186+
187+
## Migrations
188+
189+
Because `databases` uses SQLAlchemy core, you can integrate with [Alembic][alembic]
190+
for database migration support.
191+
192+
```shell
193+
$ pip install alembic
194+
$ alembic init migrations
195+
```
196+
197+
You'll want to set things up so that Alembic references the configured
198+
DATABASE_URL, and uses your table metadata.
199+
200+
In `alembic.ini` remove the following line:
201+
202+
```shell
203+
sqlalchemy.url = driver://user:pass@localhost/dbname
204+
```
205+
206+
In `migrations/env.py`, you need to set the ``'sqlalchemy.url'`` configuration key,
207+
and the `target_metadata` variable. You'll want something like this:
208+
209+
```python
210+
# The Alembic Config object.
211+
config = context.config
212+
213+
# Configure Alembic to use our DATABASE_URL and our table definitions.
214+
# These are just examples - the exact setup will depend on whatever
215+
# framework you're integrating against.
216+
from myapp.settings import DATABASE_URL
217+
from myapp.tables import metadata
218+
219+
config.set_main_option('sqlalchemy.url', str(DATABASE_URL))
220+
target_metadata = metadata
221+
222+
...
223+
```
224+
225+
Note that migrations will use a standard synchronous database driver,
226+
rather than using the async drivers that `databases` provides support for.
227+
228+
This will also be the case if you're using SQLAlchemy's standard tooling, such
229+
as using `metadata.create_all(engine)` to setup the database tables.
230+
231+
**Note for MySQL**:
232+
233+
For MySQL you'll probably need to explicitly specify the
234+
`pymysql` dialect, since the default MySQL dialect does not support Python 3.
235+
236+
If you're using the `databases.DatabaseURL` datatype, you can obtain this using
237+
`DATABASE_URL.replace(dialect="pymysql")`

databases/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from databases.core import Database, DatabaseURL
22

33

4-
__version__ = "0.1.2"
4+
__version__ = "0.1.3"
55
__all__ = ["Database", "DatabaseURL"]

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_packages(package):
3939
version=get_version("databases"),
4040
url="https://github.com/encode/databases",
4141
license="BSD",
42-
description="Databases + asyncio.",
42+
description="Async database support for Python.",
4343
long_description=get_long_description(),
4444
long_description_content_type="text/markdown",
4545
author="Tom Christie",
@@ -50,7 +50,8 @@ def get_packages(package):
5050
install_requires=["sqlalchemy", 'aiocontextvars;python_version<"3.7"'],
5151
extras_require={
5252
"postgresql": ["asyncpg", "psycopg2-binary"],
53-
"mysql": ["aiomysql", "pymysql"]
53+
"mysql": ["aiomysql", "pymysql"],
54+
"sqlite": ["aiosqlite"]
5455
},
5556
classifiers=[
5657
"Development Status :: 3 - Alpha",

0 commit comments

Comments
 (0)