Migrate from psycopg2 to psycopg3 to fix SIGSEGV crashes
This commit is contained in:
2
Procfile
2
Procfile
@@ -1 +1 @@
|
|||||||
web: gunicorn app:app --worker-class gevent --workers=4
|
web: gunicorn app:app --workers=4
|
||||||
22
db.py
22
db.py
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import psycopg2
|
import psycopg
|
||||||
from psycopg2 import pool
|
from psycopg_pool import ConnectionPool
|
||||||
from psycopg2.extras import RealDictCursor
|
from psycopg.rows import dict_row
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
@@ -32,14 +32,11 @@ class DataBase():
|
|||||||
raise Exception("No DATABASE_URL environment variable set")
|
raise Exception("No DATABASE_URL environment variable set")
|
||||||
|
|
||||||
if DataBase._pool is None:
|
if DataBase._pool is None:
|
||||||
db_url = urlparse(os.environ['DATABASE_URL'])
|
# Note: psycopg3 ConnectionPool takes a conninfo string directly, not parsed kwargs
|
||||||
DataBase._pool = pool.ThreadedConnectionPool(
|
DataBase._pool = ConnectionPool(
|
||||||
1, 20, # minconn, maxconn
|
conninfo=os.environ['DATABASE_URL'],
|
||||||
database=db_url.path[1:],
|
min_size=1,
|
||||||
user=db_url.username,
|
max_size=20
|
||||||
password=db_url.password,
|
|
||||||
host=db_url.hostname,
|
|
||||||
port=db_url.port
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def getDB(self):
|
def getDB(self):
|
||||||
@@ -54,7 +51,8 @@ class DataBase():
|
|||||||
|
|
||||||
def execute(self, query, args=(), one=False, commit=False):
|
def execute(self, query, args=(), one=False, commit=False):
|
||||||
conn = self.getDB()
|
conn = self.getDB()
|
||||||
cur = conn.cursor(cursor_factory=RealDictCursor)
|
cur = conn.cursor(row_factory=dict_row)
|
||||||
|
# Convert any custom placeholders from %s to standard %s format used by psycopg3
|
||||||
cur.execute(query, args)
|
cur.execute(query, args)
|
||||||
rv = None
|
rv = None
|
||||||
if cur.description is not None:
|
if cur.description is not None:
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
Flask>=3.0.0
|
Flask>=3.0.0
|
||||||
gunicorn>=21.2.0
|
gunicorn>=21.2.0
|
||||||
gevent>=23.9.1
|
|
||||||
Jinja2>=3.1.0
|
Jinja2>=3.1.0
|
||||||
jinja-partials==0.1.1
|
jinja-partials==0.1.1
|
||||||
psycopg2-binary>=2.9.9
|
psycopg[binary]>=3.0.0
|
||||||
|
psycopg_pool>=3.2.0
|
||||||
flask-htmx>=0.4.0
|
flask-htmx>=0.4.0
|
||||||
python-dateutil==2.8.2
|
python-dateutil==2.8.2
|
||||||
minify-html==0.10.3
|
minify-html==0.10.3
|
||||||
|
|||||||
Reference in New Issue
Block a user