11import os
22from 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+
418def 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
5168class 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