Skip to content

Commit

Permalink
Merge 91b2a64 into 98c37cd
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Dec 22, 2018
2 parents 98c37cd + 91b2a64 commit 7b9ad8d
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 84 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ matrix:
env: TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_{}"
install: pip install -r requirements-pypy.txt
script: green
- python: 3.6
env: TEST_RUNNER=py.test
install: pip install -r requirements-dev.txt
script: py.test
- python: 3.6
env: TEST_RUNNER=nose2
install: pip install -r requirements-dev.txt
script: "nose2 --plugin tortoise.contrib.test.nose2 --db-module tortoise.tests.testmodels --db-url sqlite://:memory:"
dist: xenial
sudo: required
addons:
postgresql: "9.4"
services:
- postgresql
- mysql
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.10.12
-------
- More type annotations for easier auto-completion
- Removed known broken connection pooling

0.10.11
-------
- Pre-build some query & filters statically, 15-30% speed up for smaller queries.
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
checkfiles = tortoise/ examples/ setup.py conftest.py
mypy_flags = --warn-unused-configs --warn-redundant-casts --ignore-missing-imports --allow-untyped-decorators --no-implicit-optional
py_warn = PYTHONWARNINGS=default PYTHONASYNCIODEBUG=1 PYTHONDEBUG=x PYTHONDEVMODE=dev

help:
@echo "Tortoise ORM development makefile"
Expand Down Expand Up @@ -38,15 +39,15 @@ lint: deps

test: deps
coverage erase
coverage run -p --concurrency=multiprocessing `which green`
$(py_warn) coverage run -p --concurrency=multiprocessing `which green`
coverage combine
coverage report

testall: deps
coverage erase
TORTOISE_TEST_DB=sqlite://:memory: coverage run -p --concurrency=multiprocessing `which green`
TORTOISE_TEST_DB=postgres://postgres:@127.0.0.1:5432/test_\{\} coverage run -p --concurrency=multiprocessing `which green`
TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_\{\}" coverage run -p --concurrency=multiprocessing `which green`
-$(py_warn) TORTOISE_TEST_DB=sqlite://:memory: coverage run -p --concurrency=multiprocessing `which green`
-$(py_warn) TORTOISE_TEST_DB=postgres://postgres:@127.0.0.1:5432/test_\{\} coverage run -p --concurrency=multiprocessing `which green`
-$(py_warn) TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_\{\}" coverage run -p --concurrency=multiprocessing `which green`
coverage combine
coverage report

Expand Down
18 changes: 10 additions & 8 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# pip-compile --output-file requirements-dev.txt requirements-dev.in
#
aenum==2.0.8 # via pypika
aiocontextvars==0.1.2 ; python_version < "3.7"
aiocontextvars==0.2.1 ; python_version < "3.7"
aiomysql==0.0.19
aiosqlite==0.8.0
alabaster==0.7.12 # via sphinx
asn1crypto==0.24.0 # via cryptography
astroid==2.0.4 # via pylint
astroid==2.1.0 # via pylint
asyncpg==0.18.2
asynctest==0.12.2
atomicwrites==1.2.1 # via pytest
Expand All @@ -23,10 +23,11 @@ chardet==3.0.4 # via requests
ciso8601==2.1.1
click==7.0 # via pip-tools
cloud-sptheme==1.9.4
colorama==0.4.0 # via green
colorama==0.4.1 # via green
contextvars==2.3 # via aiocontextvars
coverage==4.5.2 # via coveralls, green, nose2
coveralls==1.5.1
cryptography==2.4.1 # via pymysql
cryptography==2.4.2 # via pymysql
docopt==0.6.2 # via coveralls
docutils==0.14
filelock==3.0.10 # via tox
Expand All @@ -37,6 +38,7 @@ gitpython==2.1.11 # via bandit
green==2.13.0
idna==2.7 # via cryptography, requests
imagesize==1.1.0 # via sphinx
immutables==0.6 # via contextvars
isort==4.3.4 # via flake8-isort, pylint
jinja2==2.10 # via sphinx
lazy-object-proxy==1.3.1 # via astroid
Expand All @@ -54,12 +56,12 @@ py==1.7.0 # via pytest, tox
pycodestyle==2.4.0 # via flake8
pycparser==2.19 # via cffi
pyflakes==2.0.0 # via flake8
pygments==2.2.0
pylint==2.1.1
pygments==2.3.0
pylint==2.2.1
pymysql==0.9.2 # via aiomysql
pyparsing==2.3.0 # via packaging
pypika==0.18.3
pytest==4.0.0
pytest==4.0.1
pytz==2018.7 # via babel
pyyaml==3.13
requests==2.20.1 # via coveralls, sphinx
Expand All @@ -79,4 +81,4 @@ unidecode==1.0.23 # via green
urllib3==1.24.1 # via requests
virtualenv==16.1.0 # via tox
wrapt==1.10.11 # via astroid
yapf==0.24.0
yapf==0.25.0
8 changes: 5 additions & 3 deletions requirements-pypy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
# pip-compile --output-file requirements-pypy.txt requirements-pypy.in
#
aenum==2.0.8 # via pypika
aiocontextvars==0.1.2 ; python_version < "3.7"
aiocontextvars==0.2.1 ; python_version < "3.7"
aiomysql==0.0.19
aiosqlite==0.8.0
asn1crypto==0.24.0 # via cryptography
asynctest==0.12.2
cffi==1.11.5 # via cryptography
ciso8601==2.1.1
colorama==0.4.0 # via green
colorama==0.4.1 # via green
contextvars==2.3 # via aiocontextvars
coverage==4.5.2 # via green
cryptography==2.4.1 # via pymysql
cryptography==2.4.2 # via pymysql
green==2.13.0
idna==2.7 # via cryptography
immutables==0.6 # via contextvars
pycparser==2.19 # via cffi
pymysql==0.9.2 # via aiomysql
pypika==0.18.3
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pypika>=0.16.0
ciso8601>=2.0
aiocontextvars==0.1.2;python_version<"3.7"
aiocontextvars>=0.2;python_version<"3.7"
aiosqlite>=0.7.0
6 changes: 5 additions & 1 deletion tortoise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import importlib
import json
import logging
import os
from copy import deepcopy
from inspect import isclass
Expand All @@ -16,6 +17,8 @@
from tortoise.transactions import current_transaction_map
from tortoise.utils import generate_schema_for_client

logger = logging.getLogger(__name__)

try:
from contextvars import ContextVar
except ImportError:
Expand Down Expand Up @@ -167,7 +170,8 @@ async def _init_connections(cls, connections_config: dict, create_db: bool) -> N
await connection.db_create()
await connection.create_connection(with_db=True)
cls._connections[name] = connection
current_transaction_map[name] = ContextVar(name, default=None)
logger.debug('Creating context var')
current_transaction_map[name] = ContextVar(name, default=connection)

@classmethod
def _init_apps(cls, apps_config: dict) -> None:
Expand Down
14 changes: 8 additions & 6 deletions tortoise/backends/asyncpg/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,32 @@ def __init__(self, connection_name: str, connection) -> None:
self._connection = connection
self.log = logging.getLogger('db_client')
self._transaction_class = self.__class__
self._old_context_value = None
self.connection_name = connection_name
self.transaction = None
self._context = None

def acquire_connection(self) -> ConnectionWrapper:
return ConnectionWrapper(self._connection)

async def start(self):
self.transaction = self._connection.transaction()
await self.transaction.start()
current_transaction = current_transaction_map[self.connection_name]
self._old_context_value = current_transaction.get()
current_transaction.set(self)
self.log.debug('context set to %s', self)
self._context = current_transaction_map[self.connection_name].get()
current_transaction_map[self.connection_name].set(self)

async def commit(self):
try:
await self.transaction.commit()
except asyncpg.exceptions._base.InterfaceError as exc:
raise TransactionManagementError(exc)
current_transaction_map[self.connection_name].set(self._old_context_value)
current_transaction_map[self.connection_name].set(self._context)
self.log.debug('context reset')

async def rollback(self):
try:
await self.transaction.rollback()
except asyncpg.exceptions._base.InterfaceError as exc:
raise TransactionManagementError(exc)
current_transaction_map[self.connection_name].set(self._old_context_value)
current_transaction_map[self.connection_name].set(self._context)
self.log.debug('context reset')
33 changes: 31 additions & 2 deletions tortoise/backends/base/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@
'username': 'user',
'password': 'password',
},
'defaults': {
'port': 5432,
'min_size': 1,
'max_size': 5,
},
'cast': {
'min_size': int,
'max_size': int,
'max_queries': int,
'max_inactive_connection_lifetime': float,
},
},
'sqlite': {
'engine': 'tortoise.backends.sqlite',
'skip_first_char': False,
'vmap': {
'path': 'file_path',
},
'defaults': {},
'cast': {},
},
'mysql': {
'engine': 'tortoise.backends.mysql',
Expand All @@ -34,6 +47,18 @@
'username': 'user',
'password': 'password',
},
'defaults': {
'port': 3306,
'minsize': 1,
'maxsize': 5,
},
'cast': {
'minsize': int,
'maxsize': int,
'connect_timeout': float,
'echo': bool,
'no_delay': bool,
},
},
} # type: Dict[str, Dict[str, Any]]

Expand All @@ -53,8 +78,11 @@ def expand_db_url(db_url: str, testing: bool = False) -> dict:
raise ConfigurationError('No path specified for DB_URL')

params = {} # type: dict
for key, val in db['defaults'].items():
params[key] = val
for key, val in urlparse.parse_qs(url.query).items():
params[key] = val[-1]
cast = db['cast'].get(key, str)
params[key] = cast(val[-1])

if testing:
path = path.replace('\\{', '{').replace('\\}', '}')
Expand All @@ -67,7 +95,8 @@ def expand_db_url(db_url: str, testing: bool = False) -> dict:
params[vmap['hostname']] = str(url.hostname or '')
try:
if vmap.get('port'):
params[vmap['port']] = str(url.port or '')
if url.port:
params[vmap['port']] = int(url.port)
except ValueError:
raise ConfigurationError('Port is not an integer')
if vmap.get('username'):
Expand Down
Loading

0 comments on commit 7b9ad8d

Please sign in to comment.