+
Skip to content

Change create_all and drop_all to async #135

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
merged 3 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ MySQL, and SQLite. ORM is built with:
Because ORM is built on SQLAlchemy core, you can use Alembic to provide
database migrations.

**ORM is still under development: We recommend pinning any dependencies with `orm~=0.2`**

---

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

# Create the tables
models.create_all()
await models.create_all()

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

Expand Down
4 changes: 2 additions & 2 deletions docs/declaring_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ ORM can create or drop database and tables from models using SQLAlchemy.
You can use the following methods:

```python
models.create_all()
await models.create_all()

models.drop_all()
await models.drop_all()
```

## Data types
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Note(orm.Model):
}

# Create the database and tables
models.create_all()
await models.create_all()

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

Expand Down
13 changes: 3 additions & 10 deletions orm/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import typing

import anyio
import databases
import sqlalchemy
import typesystem
Expand Down Expand Up @@ -28,15 +27,8 @@ def __init__(self, database: databases.Database) -> None:
self.models = {}
self.metadata = sqlalchemy.MetaData()

def create_all(self):
async def create_all(self):
url = self._get_database_url()
anyio.run(self._create_all, url)

def drop_all(self):
url = self._get_database_url()
anyio.run(self._drop_all, url)

async def _create_all(self, url: str):
engine = create_async_engine(url)

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

await engine.dispose()

async def _drop_all(self, url: str):
async def drop_all(self):
url = self._get_database_url()
engine = create_async_engine(url)

for model_cls in self.models.values():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ twine
wheel

# Testing
anyio>=3.0.0,<4
autoflake
black
codecov
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_packages(package):
packages=get_packages(PACKAGE),
package_data={PACKAGE: ["py.typed"]},
data_files=[("", ["LICENSE.md"])],
install_requires=["anyio>=3.0.0,<4", "databases~=0.5", "typesystem==0.3.1"],
install_requires=["databases~=0.5", "typesystem==0.3.1"],
extras_require={
"postgresql": ["asyncpg"],
"mysql": ["aiomysql"],
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest


@pytest.fixture
@pytest.fixture(scope="module")
def anyio_backend():
return ("asyncio", {"debug": True})
6 changes: 3 additions & 3 deletions tests/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class User(orm.Model):


@pytest.fixture(autouse=True, scope="module")
def create_test_database():
models.create_all()
async def create_test_database():
await models.create_all()
yield
models.drop_all()
await models.drop_all()


@pytest.fixture(autouse=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_foreignkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class Person(orm.Model):


@pytest.fixture(autouse=True, scope="module")
def create_test_database():
models.create_all()
async def create_test_database():
await models.create_all()
yield
models.drop_all()
await models.drop_all()


@pytest.fixture(autouse=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class Product(orm.Model):


@pytest.fixture(autouse=True, scope="function")
def create_test_database():
models.create_all()
async def create_test_database():
await models.create_all()
yield
models.drop_all()
await models.drop_all()


@pytest.fixture(autouse=True)
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载