Skip to content

Commit 08cf2b0

Browse files
Database fixes for external use
1 parent fde623e commit 08cf2b0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN apt-get update \
88
&& apt-get install -y --no-install-recommends \
99
build-essential \
1010
default-libmysqlclient-dev \
11+
libpq-dev \
1112
pkg-config \
1213
gcc \
1314
python3-dev \
@@ -27,6 +28,7 @@ RUN apt-get update \
2728
&& apt-get install -y --no-install-recommends \
2829
gosu \
2930
default-mysql-client \
31+
libpq5 \
3032
&& rm -rf /var/lib/apt/lists/*
3133

3234
# Copy installed packages from builder stage

config.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import os
22
from datetime import timedelta
33

4+
def normalize_database_url():
5+
"""Normalize DATABASE_URL to use correct driver for psycopg3"""
6+
database_url = os.environ.get('DATABASE_URL') or 'sqlite:///subscriptions.db'
7+
8+
# Convert postgresql:// or postgres:// to postgresql+psycopg:// for psycopg3
9+
if database_url.startswith('postgresql://') or database_url.startswith('postgres://'):
10+
# Replace the scheme to explicitly use psycopg3
11+
if database_url.startswith('postgresql://'):
12+
database_url = database_url.replace('postgresql://', 'postgresql+psycopg://', 1)
13+
elif database_url.startswith('postgres://'):
14+
database_url = database_url.replace('postgres://', 'postgresql+psycopg://', 1)
15+
16+
return database_url
17+
418
def get_engine_options():
519
"""Get database engine options based on DATABASE_URL"""
620
database_url = os.environ.get('DATABASE_URL') or 'sqlite:///subscriptions.db'
@@ -18,14 +32,17 @@ def get_engine_options():
1832
}
1933
elif 'postgresql' in database_url.lower() or 'postgres' in database_url.lower():
2034
# PostgreSQL-specific settings (psycopg3 compatible)
35+
# Uses psycopg3 with optimized connection pooling
2136
return {
2237
'pool_size': 10,
2338
'max_overflow': 20,
2439
'pool_timeout': 30,
2540
'pool_recycle': 3600,
2641
'pool_pre_ping': True,
2742
'connect_args': {
28-
'connect_timeout': 10
43+
'connect_timeout': 10,
44+
# psycopg3 specific options can be added here
45+
'prepare_threshold': 0, # Disable prepared statements by default
2946
}
3047
}
3148
elif 'mysql' in database_url.lower() or 'mariadb' in database_url.lower():
@@ -50,7 +67,7 @@ def get_engine_options():
5067

5168
class Config:
5269
SECRET_KEY = os.environ.get('SECRET_KEY') or 'mad-hatter-secret-key-change-me'
53-
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///subscriptions.db'
70+
SQLALCHEMY_DATABASE_URI = normalize_database_url()
5471
SQLALCHEMY_TRACK_MODIFICATIONS = False
5572

5673
# Database connection pool settings

0 commit comments

Comments
 (0)