Skip to content

Commit

Permalink
Merge e472e10 into 1149a21
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Jul 23, 2018
2 parents 1149a21 + e472e10 commit 26ea38a
Show file tree
Hide file tree
Showing 31 changed files with 434 additions and 214 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
branch = True
source =
tortoise
omit =
tortoise/contrib/pylint/*
tortoise/tests/*
[report]
show_missing = True

3 changes: 3 additions & 0 deletions .green
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ verbose = 2
omit-patterns = tortoise/contrib/pylint/*,tortoise/tests/*
clear-omit = true
run-coverage = true
quiet-coverage= true
initializer = tortoise.contrib.test.initializer
finalizer = tortoise.contrib.test.finalizer
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ language: python
python:
- 3.6
- 3.5
- 3.7
env:
- TORTOISE_TEST_DB="sqlite:///tmp/test-{}.sqlite"
- TORTOISE_TEST_DB="postgres://postgres:@127.0.0.1:5432/test_{}"
dist: trusty
sudo: false
dist: xenial
sudo: required
addons:
postgresql: "9.4"
services:
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ as argument

0.8.0
-----
- Added postgres ``JSONField``
- Added PostgreSQL ``JSONField``

0.7.0
-----
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
checkfiles = tortoise/ examples/ setup.py
mypy_flags = --ignore-missing-imports --allow-untyped-decorators
mypy_flags = --warn-unused-configs --warn-redundant-casts --ignore-missing-imports --allow-untyped-decorators

help:
@echo "Tortoise-ORM development makefile"
Expand Down Expand Up @@ -37,6 +37,8 @@ lint: deps

test: deps
green
coverage run -a -m tortoise.tests.inittest
coverage report

ci: check test

Expand Down
4 changes: 3 additions & 1 deletion docs/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ Further, let's take a look at created fields for models
id = fields.IntField(pk=True)
This code defines integer primary key for table. Sadly, currently only simple integer pk is supported.
This code defines integer primary key for table.
If you don't define a primary key, we will create a Integer primary key with name of ``id`` for you.
Sadly, currently only simple integer primary key is supported.

.. code-block:: python3
Expand Down
34 changes: 18 additions & 16 deletions docs/roadmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ For ``v1.0`` that involves:
* Refactored ``init`` framework
* Refactored Fields Schema generation
* Add MySQL support

* Transaction framework
* Simpler ``with atomic`` blocks to manage isolation
* Performance work:
* Speed up test runner

Mid-term
========

Here we have all the features that is sligtly further our:
Here we have all the features that is slightly further our:

* Performance work:
* Subqueries
* Sub queries
* Benchmark suite
* Bulk operations
* Minimising overhead of building query set
* Minimising overhead of creating objects
* Minimizing overhead of building query set
* Minimizing overhead of creating objects
* ...

* Convenience/Ease-Of-Use work:
Expand All @@ -43,8 +46,7 @@ Here we have all the features that is sligtly further our:
* Make it easier to do simple aggregations
* Expand annotation framework to add statistical functions

* Atomicity framework
* Simpler ``with atomic`` blocks to manage isolation
* Transaction framework
* Ability to set ACID conformance expectations

* Migrations
Expand All @@ -54,20 +56,20 @@ Here we have all the features that is sligtly further our:
* Ability to get a the Models for that exact time of the migration, to ensure safe & consistent data migrations
* Cross-DB support

* Serialisation support
* Serialization support
* Take inspiration from ``attrs`` and ``marshmallow``
* Provide sane default serialisers that will work as-is for CRUD
* Provide sane default serializers that will work as-is for CRUD
* Provide sane default schema generators
* Make default serialisers support some validation
* Make default serialisers support data conversion
* Make default serialisers somewhat customiseable
* Provide clean way to replace serialisers with custom solution
* Make default serializers support some validation
* Make default serializers support data conversion
* Make default serializers somewhat customizable
* Provide clean way to replace serializers with custom solution
* Define strategy to work with ``ManyToMany`` relationships

* Enhanced test support
* Better performance for test runner
(use atomicity/snapshots instead of always rebuilding database)
* ``hypothesis`` strategy builer
(use transactions/snapshots instead of always rebuilding database)
* ``hypothesis`` strategy builder

* Fields
* Expand on standard provided fields
Expand All @@ -81,4 +83,4 @@ Here we have all the features that is sligtly further our:
Long-term
=========

Become the de-facto Python asyncio ORM.
Become the de facto Python AsyncIO ORM.
2 changes: 1 addition & 1 deletion docs/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ You can do it like this:
await generate_schema(client)
Here we create connection to database with default asyncpg client and then we init models. Be sure that you have your models imported in the app. Usually that's the case, because you use your models across you app, but if you have only local imports of it, tortoise won't be able to find them and init them with connection to db.
Here we create connection to database with default ``asyncpg`` client and then we ``init()`` models. Be sure that you have your models imported in the app. Usually that's the case, because you use your models across you app, but if you have only local imports of it, tortoise won't be able to find them and init them with connection to db.
``generate_schema`` generates schema on empty database, you shouldn't run it on every app init, run it just once, maybe out of your main code.

28 changes: 14 additions & 14 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ aenum==2.0.8 # via pypika
aiocontextvars==0.1.2 ; python_version < "3.7"
aiosqlite==0.3.0
alabaster==0.7.11 # via sphinx
astroid==1.6.5 # via pylint
asyncpg==0.16.0
astroid==2.0.1 # via pylint
asyncpg==0.17.0
asynctest==0.12.2
babel==2.6.0 # via sphinx
bandit==1.4.0
Expand All @@ -26,8 +26,8 @@ docutils==0.14
first==2.0.1 # via pip-tools
flake8-isort==2.5
flake8==3.5.0 # via flake8-isort
gitdb2==2.0.3 # via gitpython
gitpython==2.1.10 # via bandit
gitdb2==2.0.4 # via gitpython
gitpython==2.1.11 # via bandit
green==2.12.1
idna==2.7 # via requests
imagesize==1.0.0 # via sphinx
Expand All @@ -36,31 +36,31 @@ jinja2==2.10 # via sphinx
lazy-object-proxy==1.3.1 # via astroid
markupsafe==1.0 # via jinja2
mccabe==0.6.1 # via flake8, pylint
mypy==0.610
mypy==0.620
packaging==17.1 # via sphinx, tox
pbr==4.1.0 # via stevedore
pbr==4.1.1 # via stevedore
pip-tools==2.0.2
pluggy==0.6.0 # via tox
py==1.5.4 # via tox
pycodestyle==2.3.1 # via flake8
pyflakes==1.6.0 # via flake8
pygments==2.2.0
pylint==1.9.2
pylint==2.0.0
pyparsing==2.2.0 # via packaging
pypika==0.14.8
pypika==0.14.9
pytz==2018.5 # via babel
pyyaml==3.13 # via bandit
requests==2.19.1 # via coveralls, sphinx
six==1.11.0 # via astroid, bandit, packaging, pip-tools, pylint, sphinx, stevedore, tox
smmap2==2.0.3 # via gitdb2
six==1.11.0 # via astroid, bandit, packaging, pip-tools, sphinx, stevedore, tox
smmap2==2.0.4 # via gitdb2
snowballstemmer==1.2.1 # via sphinx
sphinx==1.7.5
sphinx==1.7.6
sphinxcontrib-websupport==1.1.0 # via sphinx
stevedore==1.28.0 # via bandit
stevedore==1.29.0 # via bandit
termstyle==0.1.11 # via green
testfixtures==6.2.0 # via flake8-isort
tox==3.1.0
typed-ast==1.1.0 # via mypy
tox==3.1.2
typed-ast==1.1.0 # via astroid, mypy
unidecode==1.0.22 # via green
urllib3==1.23 # via requests
virtualenv==16.0.0 # via tox
Expand Down
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,24 @@ def requirements():
long_description=open('README.rst', 'r').read(),
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Development Status :: 5 - Production/Stable',
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: PL/SQL',
'Framework :: AsyncIO',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Database',
'Operating System :: POSIX',
'Operating System :: MacOS :: MacOS X',
],
keywords=('sql mysql postgres psql '
'sqlite aiosqlite asyncpg '
'relational database rdbms '
'orm object mapper'),
'orm object mapper '
'async asyncio aio'),

# Dependent packages (distributions)
install_requires=requirements(),
Expand Down
6 changes: 3 additions & 3 deletions tortoise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
from inspect import isclass
from typing import Any, Dict # noqa
from typing import Any, Dict, Optional # noqa

from tortoise import fields
from tortoise.exceptions import ConfigurationError # noqa
Expand Down Expand Up @@ -197,8 +197,8 @@ def _get_config_from_config_file(cls, config_file):
@classmethod
async def init(
cls,
config: dict = None,
config_file: str = None,
config: Optional[dict] = None,
config_file: Optional[str] = None,
_create_db: bool = False
) -> None:
"""
Expand Down
22 changes: 11 additions & 11 deletions tortoise/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
class Aggregate:
aggregation_func = AggregateFunction

def __init__(self, field):
def __init__(self, field) -> None:
self.field = field

def _resolve_field_for_model(self, field, model):
def _resolve_field_for_model(self, field: str, model) -> dict:
field_split = field.split('__')
if not field_split[1:]:
aggregation = {
'joins': [],
'field': None,
}
aggregation_joins = [] # type: list
if field_split[0] in model._meta.fetch_fields:
related_field = model._meta.fields_map[field_split[0]]
join = (Table(model._meta.table), field_split[0], related_field)
aggregation['joins'].append(join)
aggregation['field'] = self.aggregation_func(
aggregation_joins.append(join)
aggregation_field = self.aggregation_func(
Table(related_field.type._meta.table).id
)
else:
aggregation['field'] = self.aggregation_func(
aggregation_field = self.aggregation_func(
getattr(Table(model._meta.table), field_split[0])
)
return aggregation
return {
'joins': aggregation_joins,
'field': aggregation_field,
}
else:
if field_split[0] not in model._meta.fetch_fields:
raise ConfigurationError('{} not resolvable'.format(field))
Expand All @@ -45,7 +45,7 @@ def _resolve_field_for_model(self, field, model):
aggregation['joins'].append(join)
return aggregation

def resolve_for_model(self, model):
def resolve_for_model(self, model) -> dict:
aggregation = self._resolve_field_for_model(self.field, model)
aggregation['joins'] = reversed(aggregation['joins'])
return aggregation
Expand Down
32 changes: 13 additions & 19 deletions tortoise/backends/asyncpg/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tortoise.backends.base.client import (BaseDBAsyncClient, BaseTransactionWrapper,
ConnectionWrapper, SingleConnectionWrapper)
from tortoise.exceptions import (ConfigurationError, DBConnectionError, IntegrityError,
OperationalError)
OperationalError, TransactionManagementError)
from tortoise.transactions import current_connection


Expand Down Expand Up @@ -165,37 +165,31 @@ async def start(self):
current_connection.set(self)

async def commit(self):
await self.transaction.commit()
try:
await self.transaction.commit()
except asyncpg.exceptions._base.InterfaceError as exc:
raise TransactionManagementError(exc)
if self._pool:
await self._pool.release(self._connection)
self._connection = None
current_connection.set(self._old_context_value)

async def rollback(self):
await self.transaction.rollback()
try:
await self.transaction.rollback()
except asyncpg.exceptions._base.InterfaceError as exc:
raise TransactionManagementError(exc)
if self._pool:
await self._pool.release(self._connection)
self._connection = None
current_connection.set(self._old_context_value)

async def __aenter__(self):
self._old_context_value = current_connection.get()
current_connection.set(self)
if not self._connection:
self._connection = await self._get_connection()
self.transaction = self._connection.transaction()
await self.transaction.start()
await self.start()
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
current_connection.set(self._old_context_value)
if exc_type:
await self.transaction.rollback()
if self._pool:
await self._pool.release(self._connection)
self._connection = None
return False
await self.transaction.commit()
if self._pool:
await self._pool.release(self._connection)
self._connection = None
await self.rollback()
else:
await self.commit()
4 changes: 1 addition & 3 deletions tortoise/backends/base/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def generate_config(
app_label: str,
testing: bool = False,
) -> dict:
if not app_label:
app_label = 'models_{}'.format(uuid.uuid4().hex)
url = urlparse.urlparse(db_url)
if url.scheme not in DB_LOOKUP:
raise ConfigurationError('Unknown DB scheme: {}'.format(url.scheme))
Expand All @@ -58,7 +56,7 @@ def generate_config(
path = path.format(uuid.uuid4().hex)

vars = {} # type: dict
vars.update(db['vars']) # type: ignore
vars.update(db['vars'])
params[vars['path']] = path
if vars.get('hostname'):
params[vars['hostname']] = str(url.hostname or '')
Expand Down

0 comments on commit 26ea38a

Please sign in to comment.