1414
1515Databases 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)
9496query = notes.select()
9597async 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:
178183async 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") `
0 commit comments