Skip to content

Commit

Permalink
build(deps): switch to sqlalchemy 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Mar 16, 2021
1 parent 2194ffb commit 8aa58f3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
35 changes: 27 additions & 8 deletions databases/backends/aiopg.py
Expand Up @@ -7,11 +7,11 @@
import aiopg
from aiopg.sa.engine import APGCompiler_psycopg2
from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
from sqlalchemy.engine.cursor import CursorResultMetaData
from sqlalchemy.engine.interfaces import Dialect, ExecutionContext
from sqlalchemy.engine.result import ResultMetaData, RowProxy
from sqlalchemy.engine.result import Row
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement
from sqlalchemy.types import TypeEngine

from databases.core import DatabaseURL
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
Expand Down Expand Up @@ -119,9 +119,15 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[typing.Mapping]:
try:
await cursor.execute(query, args)
rows = await cursor.fetchall()
metadata = ResultMetaData(context, cursor.description)
metadata = CursorResultMetaData(context, cursor.description)
return [
RowProxy(metadata, row, metadata._processors, metadata._keymap)
Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
]
finally:
Expand All @@ -136,8 +142,14 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
row = await cursor.fetchone()
if row is None:
return None
metadata = ResultMetaData(context, cursor.description)
return RowProxy(metadata, row, metadata._processors, metadata._keymap)
metadata = CursorResultMetaData(context, cursor.description)
return Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
finally:
cursor.close()

Expand Down Expand Up @@ -169,9 +181,15 @@ async def iterate(
cursor = await self._connection.cursor()
try:
await cursor.execute(query, args)
metadata = ResultMetaData(context, cursor.description)
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
yield RowProxy(metadata, row, metadata._processors, metadata._keymap)
yield Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
finally:
cursor.close()

Expand All @@ -196,6 +214,7 @@ def _compile(
compiled._result_columns,
compiled._ordered_columns,
compiled._textual_ordered_columns,
compiled._loose_column_name_matching,
)
else:
args = {}
Expand Down
35 changes: 27 additions & 8 deletions databases/backends/mysql.py
Expand Up @@ -5,11 +5,11 @@

import aiomysql
from sqlalchemy.dialects.mysql import pymysql
from sqlalchemy.engine.cursor import CursorResultMetaData
from sqlalchemy.engine.interfaces import Dialect, ExecutionContext
from sqlalchemy.engine.result import ResultMetaData, RowProxy
from sqlalchemy.engine.result import Row
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement
from sqlalchemy.types import TypeEngine

from databases.core import LOG_EXTRA, DatabaseURL
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
Expand Down Expand Up @@ -107,9 +107,15 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[typing.Mapping]:
try:
await cursor.execute(query, args)
rows = await cursor.fetchall()
metadata = ResultMetaData(context, cursor.description)
metadata = CursorResultMetaData(context, cursor.description)
return [
RowProxy(metadata, row, metadata._processors, metadata._keymap)
Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
]
finally:
Expand All @@ -124,8 +130,14 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
row = await cursor.fetchone()
if row is None:
return None
metadata = ResultMetaData(context, cursor.description)
return RowProxy(metadata, row, metadata._processors, metadata._keymap)
metadata = CursorResultMetaData(context, cursor.description)
return Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
finally:
await cursor.close()

Expand Down Expand Up @@ -159,9 +171,15 @@ async def iterate(
cursor = await self._connection.cursor()
try:
await cursor.execute(query, args)
metadata = ResultMetaData(context, cursor.description)
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
yield RowProxy(metadata, row, metadata._processors, metadata._keymap)
yield Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
finally:
await cursor.close()

Expand All @@ -186,6 +204,7 @@ def _compile(
compiled._result_columns,
compiled._ordered_columns,
compiled._textual_ordered_columns,
compiled._loose_column_name_matching,
)
else:
args = {}
Expand Down
35 changes: 27 additions & 8 deletions databases/backends/sqlite.py
Expand Up @@ -4,11 +4,11 @@

import aiosqlite
from sqlalchemy.dialects.sqlite import pysqlite
from sqlalchemy.engine.cursor import CursorResultMetaData
from sqlalchemy.engine.interfaces import Dialect, ExecutionContext
from sqlalchemy.engine.result import ResultMetaData, RowProxy
from sqlalchemy.engine.result import Row
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement
from sqlalchemy.types import TypeEngine

from databases.core import LOG_EXTRA, DatabaseURL
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
Expand Down Expand Up @@ -92,9 +92,15 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[typing.Mapping]:

async with self._connection.execute(query, args) as cursor:
rows = await cursor.fetchall()
metadata = ResultMetaData(context, cursor.description)
metadata = CursorResultMetaData(context, cursor.description)
return [
RowProxy(metadata, row, metadata._processors, metadata._keymap)
Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
]

Expand All @@ -106,8 +112,14 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
row = await cursor.fetchone()
if row is None:
return None
metadata = ResultMetaData(context, cursor.description)
return RowProxy(metadata, row, metadata._processors, metadata._keymap)
metadata = CursorResultMetaData(context, cursor.description)
return Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)

async def execute(self, query: ClauseElement) -> typing.Any:
assert self._connection is not None, "Connection is not acquired"
Expand All @@ -129,9 +141,15 @@ async def iterate(
assert self._connection is not None, "Connection is not acquired"
query, args, context = self._compile(query)
async with self._connection.execute(query, args) as cursor:
metadata = ResultMetaData(context, cursor.description)
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
yield RowProxy(metadata, row, metadata._processors, metadata._keymap)
yield Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)

def transaction(self) -> TransactionBackend:
return SQLiteTransaction(self)
Expand All @@ -158,6 +176,7 @@ def _compile(
compiled._result_columns,
compiled._ordered_columns,
compiled._textual_ordered_columns,
compiled._loose_column_name_matching,
)

query_message = compiled.string.replace(" \n", " ").replace("\n", " ")
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,7 +1,7 @@
# Notes...
# The JSONField tests require sqlalchemy 1.3+. Other cases work at lower versions.
# The aiocontextvars package is only required as a backport for Python 3.6.
sqlalchemy>=1.3.0
sqlalchemy>=1.4.0
aiocontextvars;python_version<"3.7"

# Async database drivers
Expand Down
6 changes: 5 additions & 1 deletion tests/test_databases.py
Expand Up @@ -337,7 +337,11 @@ async def test_result_values_allow_duplicate_names(database_url):
row = await database.fetch_one(query=query)

assert list(row.keys()) == ["id", "id"]
assert list(row.values()) == [1, 2]
try:
values = row.values()
except AttributeError:
values = row._mapping.values()
assert list(values) == [1, 2]


@pytest.mark.parametrize("database_url", DATABASE_URLS)
Expand Down

0 comments on commit 8aa58f3

Please sign in to comment.