Skip to content
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

Add support for psycopg3 #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.11.5
hooks:
- id: isort
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ Here's the raw data:

```

pip install DSLR psycopg2 # or psycopg2-binary
pip install DSLR psycopg2 # or psycopg2-binary, or psycopg

```

**Install using pipx**

```

pipx install DSLR[psycopg2] # or psycopg2-binary
pipx install DSLR[psycopg2] # or psycopg2-binary, or psycopg

````

Expand Down
5 changes: 4 additions & 1 deletion dslr/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from datetime import datetime
from typing import List, Optional

from psycopg2 import sql
try:
from psycopg import sql
except ImportError:
from psycopg2 import sql

from .config import settings
from .runner import exec_shell, exec_sql
Expand Down
22 changes: 16 additions & 6 deletions dslr/pg_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Any, List, Optional, Tuple

import psycopg2
import psycopg2.extensions
try:
import psycopg as psycopg
except ImportError:
import psycopg2 as psycopg
import psycopg2.extensions

from dslr.console import console

Expand All @@ -20,17 +23,24 @@ def __init__(self, host, port, user, password, dbname):
self.password = password
self.dbname = dbname

self.conn = psycopg2.connect(
self.conn = psycopg.connect(
host=host,
port=port,
user=user,
password=password,
dbname=dbname,
)
self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

self._set_autocommit()
self.cur = self.conn.cursor()

def _set_autocommit(self):
if hasattr(self.conn, "set_autocommit"):
self.conn.set_autocommit(True) # type: ignore
else:
self.conn.set_isolation_level( # type: ignore
psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
)

def execute(self, sql, data) -> Optional[List[Tuple[Any, ...]]]:
if settings.debug:
console.log(f"SQL: {sql}")
Expand All @@ -40,7 +50,7 @@ def execute(self, sql, data) -> Optional[List[Tuple[Any, ...]]]:

try:
result = self.cur.fetchall()
except psycopg2.ProgrammingError:
except psycopg.ProgrammingError:
result = None

return result
5 changes: 4 additions & 1 deletion dslr/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from collections import namedtuple
from typing import Any, List, Optional, Tuple, Union

from psycopg2 import sql
try:
from psycopg import sql
except ImportError:
from psycopg2 import sql

from dslr.pg_client import PGClient

Expand Down
1,110 changes: 554 additions & 556 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ timeago = "^1.0.15"
tomli = "^2.0.1"
psycopg2 = { version = "^2.9.3", optional = true }
psycopg2-binary = { version = "^2.9.3", optional = true }
psycopg = { version = "^3.1.14", optional = true }

[tool.poetry.extras]
psycopg2 = ["psycopg2"]
psycopg2-binary = ["psycopg2-binary"]
psycopg = ["psycopg"]

[tool.poetry.dev-dependencies]
isort = "^5.10.1"
isort = "^5.11.5"
flake8 = "^4.0.1"
black = "^22.6.0"
ipdb = "^0.13.9"
Expand All @@ -43,6 +45,9 @@ twine = "^4.0.1"
[tool.poetry.scripts]
dslr = "dslr.cli:cli"

[tool.pyright]
reportMissingImports = false

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
27 changes: 18 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ envlist =
black
isort
pyright
py37
py38
py39
py310
py37-psycopg2
py37-psycopg3
py38-psycopg2
py38-psycopg3
py39-psycopg2
py39-psycopg3
py310-psycopg2
py310-psycopg3

[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310, flake8, black, isort, pyright
3.7: py37-psycopg2, py37-psycopg3
3.8: py38-psycopg2, py38-psycopg3
3.9: py39-psycopg2, py39-psycopg3
3.10: py310-psycopg2, py310-psycopg3, flake8, black, isort, pyright

[testenv]
deps = psycopg2-binary
commands =
python -m unittest

[testenv:{py37,py38,py39,py310}-psycopg2]
deps = psycopg2-binary

[testenv:{py37,py38,py39,py310}-psycopg3]
deps = psycopg

[testenv:flake8]
deps = flake8 >=4.0.1, <5.0.0
commands = flake8
Expand Down