]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add bind casts for BIT on asyncpg
authorSören Oldag <soeren.oldag@kiwi.ki>
Thu, 26 Oct 2023 13:17:41 +0000 (09:17 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 28 Oct 2023 15:08:16 +0000 (11:08 -0400)
Fixed SQL handling for "insertmanyvalues" when using the
:class:`.postgresql.BIT` datatype with the asyncpg backend.  The
:class:`.postgresql.BIT` on asyncpg apparently requires the use of an
asyncpg-specific `BitString` type which is currently exposed when using
this DBAPI, making it incompatible with other PostgreSQL DBAPIs that all
work with plain bitstrings here.  A future fix in version 2.1 will
normalize this datatype across all PG backends.   Pull request courtesy
Sören Oldag.

Fixes: #10532
Closes: #10533
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10533
Pull-request-sha: 528607d5b5b67c1b9c2edb39176b7e005db371f6

Change-Id: I879f2fc24b5650467b85f170c0def15834f75456

doc/build/changelog/unreleased_20/10532.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/asyncpg.py
test/dialect/postgresql/test_types.py

diff --git a/doc/build/changelog/unreleased_20/10532.rst b/doc/build/changelog/unreleased_20/10532.rst
new file mode 100644 (file)
index 0000000..ba379cf
--- /dev/null
@@ -0,0 +1,13 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 10532
+
+    Fixed SQL handling for "insertmanyvalues" when using the
+    :class:`.postgresql.BIT` datatype with the asyncpg backend.  The
+    :class:`.postgresql.BIT` on asyncpg apparently requires the use of an
+    asyncpg-specific `BitString` type which is currently exposed when using
+    this DBAPI, making it incompatible with other PostgreSQL DBAPIs that all
+    work with plain bitstrings here.  A future fix in version 2.1 will
+    normalize this datatype across all PG backends.   Pull request courtesy
+    Sören Oldag.
+
index 4fa188cb02ebf2e303303b901564d865c3d677c5..ca35bf96075d249d679243e9df922bdd2f4cfc28 100644 (file)
@@ -205,6 +205,7 @@ from .base import PGExecutionContext
 from .base import PGIdentifierPreparer
 from .base import REGCLASS
 from .base import REGCONFIG
+from .types import BIT
 from .types import BYTEA
 from .types import CITEXT
 from ... import exc
@@ -237,6 +238,10 @@ class AsyncpgTime(sqltypes.Time):
     render_bind_cast = True
 
 
+class AsyncpgBit(BIT):
+    render_bind_cast = True
+
+
 class AsyncpgByteA(BYTEA):
     render_bind_cast = True
 
@@ -1016,6 +1021,7 @@ class PGDialect_asyncpg(PGDialect):
         {
             sqltypes.String: AsyncpgString,
             sqltypes.ARRAY: AsyncpgARRAY,
+            BIT: AsyncpgBit,
             CITEXT: CITEXT,
             REGCONFIG: AsyncpgREGCONFIG,
             sqltypes.Time: AsyncpgTime,
index 95bbb16636ec1318489960348ee2a9e33b2f35c8..0a98ef5045ffa23128ab54f69b71780d1171712a 100644 (file)
@@ -46,6 +46,7 @@ from sqlalchemy.dialects.postgresql import array
 from sqlalchemy.dialects.postgresql import array_agg
 from sqlalchemy.dialects.postgresql import asyncpg
 from sqlalchemy.dialects.postgresql import base
+from sqlalchemy.dialects.postgresql import BIT
 from sqlalchemy.dialects.postgresql import BYTEA
 from sqlalchemy.dialects.postgresql import CITEXT
 from sqlalchemy.dialects.postgresql import DATEMULTIRANGE
@@ -6233,7 +6234,8 @@ class PGInsertManyValuesTest(fixtures.TestBase):
     __backend__ = True
 
     @testing.combinations(
-        ("PG BYTEA", BYTEA(), b"7\xe7\x9f"),
+        ("BYTEA", BYTEA(), b"7\xe7\x9f"),
+        ("BIT", BIT(3), "011"),
         argnames="type_,value",
         id_="iaa",
     )
@@ -6266,6 +6268,11 @@ class PGInsertManyValuesTest(fixtures.TestBase):
 
         t.create(connection)
 
+        if type_._type_affinity is BIT and testing.against("+asyncpg"):
+            import asyncpg
+
+            value = asyncpg.BitString(value)
+
         result = connection.execute(
             t.insert().returning(
                 t.c.id,