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

Commit a9915f8

Browse files
authored
Make create_all and drop_all async (#135)
1 parent c49d0fd commit a9915f8

File tree

10 files changed

+19
-27
lines changed

10 files changed

+19
-27
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ MySQL, and SQLite. ORM is built with:
2222
Because ORM is built on SQLAlchemy core, you can use Alembic to provide
2323
database migrations.
2424

25-
**ORM is still under development: We recommend pinning any dependencies with `orm~=0.2`**
26-
2725
---
2826

2927
**Documentation**: [https://www.encode.io/orm](https://www.encode.io/orm)
@@ -70,7 +68,7 @@ class Note(orm.Model):
7068
}
7169

7270
# Create the tables
73-
models.create_all()
71+
await models.create_all()
7472

7573
await Note.objects.create(text="Buy the groceries.", completed=False)
7674

docs/declaring_models.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ ORM can create or drop database and tables from models using SQLAlchemy.
3131
You can use the following methods:
3232

3333
```python
34-
models.create_all()
34+
await models.create_all()
3535

36-
models.drop_all()
36+
await models.drop_all()
3737
```
3838

3939
## Data types

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Note(orm.Model):
6666
}
6767

6868
# Create the database and tables
69-
models.create_all()
69+
await models.create_all()
7070

7171
await Note.objects.create(text="Buy the groceries.", completed=False)
7272

orm/models.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import typing
22

3-
import anyio
43
import databases
54
import sqlalchemy
65
import typesystem
@@ -28,15 +27,8 @@ def __init__(self, database: databases.Database) -> None:
2827
self.models = {}
2928
self.metadata = sqlalchemy.MetaData()
3029

31-
def create_all(self):
30+
async def create_all(self):
3231
url = self._get_database_url()
33-
anyio.run(self._create_all, url)
34-
35-
def drop_all(self):
36-
url = self._get_database_url()
37-
anyio.run(self._drop_all, url)
38-
39-
async def _create_all(self, url: str):
4032
engine = create_async_engine(url)
4133

4234
for model_cls in self.models.values():
@@ -48,7 +40,8 @@ async def _create_all(self, url: str):
4840

4941
await engine.dispose()
5042

51-
async def _drop_all(self, url: str):
43+
async def drop_all(self):
44+
url = self._get_database_url()
5245
engine = create_async_engine(url)
5346

5447
for model_cls in self.models.values():

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ twine
66
wheel
77

88
# Testing
9+
anyio>=3.0.0,<4
910
autoflake
1011
black
1112
codecov

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_packages(package):
5151
packages=get_packages(PACKAGE),
5252
package_data={PACKAGE: ["py.typed"]},
5353
data_files=[("", ["LICENSE.md"])],
54-
install_requires=["anyio>=3.0.0,<4", "databases~=0.5", "typesystem==0.3.1"],
54+
install_requires=["databases~=0.5", "typesystem==0.3.1"],
5555
extras_require={
5656
"postgresql": ["asyncpg"],
5757
"mysql": ["aiomysql"],

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

33

4-
@pytest.fixture
4+
@pytest.fixture(scope="module")
55
def anyio_backend():
66
return ("asyncio", {"debug": True})

tests/test_columns.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class User(orm.Model):
5454

5555

5656
@pytest.fixture(autouse=True, scope="module")
57-
def create_test_database():
58-
models.create_all()
57+
async def create_test_database():
58+
await models.create_all()
5959
yield
60-
models.drop_all()
60+
await models.drop_all()
6161

6262

6363
@pytest.fixture(autouse=True)

tests/test_foreignkey.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ class Person(orm.Model):
7676

7777

7878
@pytest.fixture(autouse=True, scope="module")
79-
def create_test_database():
80-
models.create_all()
79+
async def create_test_database():
80+
await models.create_all()
8181
yield
82-
models.drop_all()
82+
await models.drop_all()
8383

8484

8585
@pytest.fixture(autouse=True)

tests/test_models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class Product(orm.Model):
3333

3434

3535
@pytest.fixture(autouse=True, scope="function")
36-
def create_test_database():
37-
models.create_all()
36+
async def create_test_database():
37+
await models.create_all()
3838
yield
39-
models.drop_all()
39+
await models.drop_all()
4040

4141

4242
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)