Skip to content

Commit

Permalink
rely on psycopg rather than psycopg2
Browse files Browse the repository at this point in the history
  • Loading branch information
zganger committed Oct 8, 2024
1 parent faf801a commit ac1747a
Show file tree
Hide file tree
Showing 11 changed files with 8 additions and 21 deletions.
8 changes: 3 additions & 5 deletions backend/database/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import click
import pandas as pd
import psycopg
import psycopg2.errors
from flask import abort, current_app
from flask.cli import AppGroup, with_appcontext
from flask_sqlalchemy import SQLAlchemy
from psycopg2 import connect
from psycopg2.extensions import connection
from psycopg import connect, connection
from sqlalchemy.exc import ResourceClosedError
from werkzeug.utils import secure_filename

Expand Down Expand Up @@ -123,7 +121,7 @@ def create_database(

try:
cursor.execute(f"CREATE DATABASE {database};")
except (psycopg2.errors.lookup("42P04"), psycopg.errors.DuplicateDatabase):
except psycopg.errors.DuplicateDatabase:
click.echo(f"Database {database!r} already exists.")
else:
click.echo(f"Created database {database!r}.")
Expand Down Expand Up @@ -184,7 +182,7 @@ def delete_database(conn: connection, test_db: bool):

try:
cursor.execute(f"DROP DATABASE {database};")
except psycopg2.errors.lookup("3D000"):
except psycopg.errors.InvalidCatalogName:
click.echo(f"Database {database!r} does not exist.")
else:
click.echo(f"Database {database!r} was deleted.")
4 changes: 2 additions & 2 deletions backend/scraper/notebooks/cpdp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"import numpy as np\n",
"import pandas as pd\n",
"import sqlalchemy\n",
"import psycopg2\n",
"import psycopg\n",
"from IPython.display import display, HTML\n",
"from collections import namedtuple\n",
"from backend.database import db, Incident, Officer, Accusation, Victim\n",
Expand Down Expand Up @@ -350,7 +350,7 @@
" try:\n",
" insert_bulk(dicts, OrmClass)\n",
" except sqlalchemy.exc.IntegrityError as e:\n",
" if isinstance(e.orig, psycopg2.errors.UniqueViolation):\n",
" if isinstance(e.orig, psycopg.errors.UniqueViolation):\n",
" print(f\"Already created {OrmClass.__name__} records\")\n",
" else:\n",
" raise e\n",
Expand Down
1 change: 0 additions & 1 deletion backend/scraper/notebooks/mpv.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"import numpy as np\n",
"import pandas as pd\n",
"from flask_sqlalchemy import SQLAlchemy\n",
"import psycopg2\n",
"from itertools import zip_longest\n",
"from typing import List\n",
"import requests\n",
Expand Down
5 changes: 2 additions & 3 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import psycopg.errors
import psycopg2.errors
import pytest
from backend.api import create_app
from backend.auth import user_manager
Expand Down Expand Up @@ -34,13 +33,13 @@ def database():
host=cfg.POSTGRES_HOST,
port=cfg.PGPORT,
dbname=cfg.POSTGRES_DB,
version=16.3,
version=16.4,
password=cfg.POSTGRES_PASSWORD,
)

try:
janitor.init()
except (psycopg2.errors.lookup("42P04"), psycopg.errors.DuplicateDatabase):
except psycopg.errors.DuplicateDatabase:
pass

yield
Expand Down
2 changes: 1 addition & 1 deletion requirements/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# psycopg2 requires postgres development files in order to compile the
# psycopg requires postgres development files in order to compile the
# requirements, so this image starts with the same image as the database
# containers and installs the same version of python as the api containers

Expand Down
1 change: 0 additions & 1 deletion requirements/dev_unix.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-r _core.in
psycopg2-binary
psycopg-binary
2 changes: 0 additions & 2 deletions requirements/dev_unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ psycopg==3.2.3
# via pytest-postgresql
psycopg-binary==3.2.3
# via -r requirements/dev_unix.in
psycopg2-binary==2.9.9
# via -r requirements/dev_unix.in
ptyprocess==0.7.0
# via
# pexpect
Expand Down
1 change: 0 additions & 1 deletion requirements/dev_windows.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-r _core.in
psycopg2
psycopg
2 changes: 0 additions & 2 deletions requirements/dev_windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ psycopg==3.2.3
# via
# -r requirements/dev_windows.in
# pytest-postgresql
psycopg2==2.9.9
# via -r requirements/dev_windows.in
ptyprocess==0.7.0
# via
# pexpect
Expand Down
1 change: 0 additions & 1 deletion requirements/prod.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-r _core.in
psycopg2-binary==2.9.9
psycopg-binary==3.1.18
2 changes: 0 additions & 2 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ psycopg==3.2.3
# via pytest-postgresql
psycopg-binary==3.2.3
# via -r requirements/prod.in
psycopg2-binary==2.9.9
# via -r requirements/prod.in
ptyprocess==0.7.0
# via
# pexpect
Expand Down

0 comments on commit ac1747a

Please sign in to comment.