Skip to content

Support psycopg3 #208

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 6 commits into from
Apr 6, 2023
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
9 changes: 4 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ executors:
type: string
docker:
- image: python:<< parameters.version >>-buster
- image: postgres:11.0
- image: postgres:12.0
environment:
POSTGRES_DB: 'psqlextra'
POSTGRES_USER: 'psqlextra'
Expand Down Expand Up @@ -42,7 +42,6 @@ commands:
environment:
DATABASE_URL: 'postgres://psqlextra:psqlextra@localhost:5432/psqlextra'


jobs:
test-python36:
executor:
Expand Down Expand Up @@ -78,7 +77,7 @@ jobs:
extra: test
- run-tests:
pyversion: 38
djversions: 20,21,22,30,31,32,40
djversions: 20,21,22,30,31,32,40,41,42

test-python39:
executor:
Expand All @@ -90,7 +89,7 @@ jobs:
extra: test
- run-tests:
pyversion: 39
djversions: 21,22,30,31,32,40
djversions: 21,22,30,31,32,40,41,42

test-python310:
executor:
Expand All @@ -102,7 +101,7 @@ jobs:
extra: test
- run-tests:
pyversion: 310
djversions: 21,22,30,31,32,40
djversions: 21,22,30,31,32,40,41,42
- store_test_results:
path: reports
- run:
Expand Down
4 changes: 3 additions & 1 deletion psqlextra/backend/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ def _create_view_model(self, sql: str, model: Model) -> None:
meta = self._view_properties_for_model(model)

with self.connection.cursor() as cursor:
view_sql = cursor.mogrify(*meta.query).decode("utf-8")
view_sql = cursor.mogrify(*meta.query)
if isinstance(view_sql, bytes):
view_sql = view_sql.decode("utf-8")

self.execute(sql % (self.quote_name(model._meta.db_table), view_sql))

Expand Down
3 changes: 2 additions & 1 deletion psqlextra/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ def execute_sql(self, return_id=False):
rows.extend(cursor.fetchall())
except ProgrammingError:
pass
description = cursor.description

# create a mapping between column names and column value
return [
{
column.name: row[column_index]
for column_index, column in enumerate(cursor.description)
for column_index, column in enumerate(description)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this change. How is aliasing it on line 190 changing how this works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the with cursor now cleans up the data on the cursor on exit. So cusror.description ends up being None. So we just need to read it before the with block exits

if row
}
for row in rows
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def run(self):
"build==0.7.0",
"twine==3.7.1",
],
"psycopg3": [
"django>=4.2,<5.0",
"psycopg[binary]>=3.0.0",
],
},
cmdclass={
"lint": create_command(
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def fake_app():
def postgres_server_version(db) -> int:
"""Gets the PostgreSQL server version."""

return connection.cursor().connection.server_version
return connection.cursor().connection.info.server_version


@pytest.fixture(autouse=True)
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py36-dj{20,21,22,30,31,32}, py37-dj{20,21,22,30,31,32}, py38-dj{20,21,22,30,31,32,40, 41}, py39-dj{21,22,30,31,32,40,41}, py310-dj{21,22,30,31,32,40,41}
envlist = py36-dj{20,21,22,30,31,32}, py37-dj{20,21,22,30,31,32}, py38-dj{20,21,22,30,31,32,40,41,42}, py39-dj{21,22,30,31,32,40,41,42}, py310-dj{21,22,30,31,32,40,41,42}

[testenv]
deps =
Expand All @@ -11,6 +11,7 @@ deps =
dj32: Django~=3.2.0
dj40: Django~=4.0.0
dj41: Django~=4.1.0
dj42: .[psycopg3]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means the tests don't run against psycopg2 anymore. I will patch this up after merging this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#209

I just added this. Tests run against psycopg 2.8, 2.9 and 3.0 now with all supported Django versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

.[test]
setenv =
DJANGO_SETTINGS_MODULE=settings
Expand Down