|
1 | 1 | import os |
2 | 2 | from datetime import timedelta |
3 | 3 |
|
| 4 | +def get_engine_options(): |
| 5 | + """Get database engine options based on DATABASE_URL""" |
| 6 | + database_url = os.environ.get('DATABASE_URL') or 'sqlite:///subscriptions.db' |
| 7 | + |
| 8 | + if 'sqlite' in database_url.lower(): |
| 9 | + # SQLite-specific settings |
| 10 | + return { |
| 11 | + 'pool_timeout': 10, |
| 12 | + 'pool_recycle': 3600, |
| 13 | + 'pool_pre_ping': True, |
| 14 | + 'connect_args': { |
| 15 | + 'timeout': 20, |
| 16 | + 'check_same_thread': False |
| 17 | + } |
| 18 | + } |
| 19 | + elif 'postgresql' in database_url.lower() or 'postgres' in database_url.lower(): |
| 20 | + # PostgreSQL-specific settings (psycopg3 compatible) |
| 21 | + return { |
| 22 | + 'pool_size': 10, |
| 23 | + 'max_overflow': 20, |
| 24 | + 'pool_timeout': 30, |
| 25 | + 'pool_recycle': 3600, |
| 26 | + 'pool_pre_ping': True, |
| 27 | + 'connect_args': { |
| 28 | + 'connect_timeout': 10 |
| 29 | + } |
| 30 | + } |
| 31 | + elif 'mysql' in database_url.lower() or 'mariadb' in database_url.lower(): |
| 32 | + # MySQL/MariaDB-specific settings |
| 33 | + return { |
| 34 | + 'pool_size': 10, |
| 35 | + 'max_overflow': 20, |
| 36 | + 'pool_timeout': 30, |
| 37 | + 'pool_recycle': 3600, |
| 38 | + 'pool_pre_ping': True, |
| 39 | + 'connect_args': { |
| 40 | + 'charset': 'utf8mb4' |
| 41 | + } |
| 42 | + } |
| 43 | + else: |
| 44 | + # Default settings for other databases |
| 45 | + return { |
| 46 | + 'pool_timeout': 30, |
| 47 | + 'pool_recycle': 3600, |
| 48 | + 'pool_pre_ping': True |
| 49 | + } |
| 50 | + |
4 | 51 | class Config: |
5 | 52 | SECRET_KEY = os.environ.get('SECRET_KEY') or 'mad-hatter-secret-key-change-me' |
6 | 53 | SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///subscriptions.db' |
7 | 54 | SQLALCHEMY_TRACK_MODIFICATIONS = False |
8 | 55 |
|
9 | 56 | # Database connection pool settings |
10 | | - @classmethod |
11 | | - def get_engine_options(cls): |
12 | | - database_url = os.environ.get('DATABASE_URL') or 'sqlite:///subscriptions.db' |
13 | | - |
14 | | - if 'sqlite' in database_url.lower(): |
15 | | - # SQLite-specific settings |
16 | | - return { |
17 | | - 'pool_timeout': 10, |
18 | | - 'pool_recycle': 3600, |
19 | | - 'pool_pre_ping': True, |
20 | | - 'connect_args': { |
21 | | - 'timeout': 20, |
22 | | - 'check_same_thread': False |
23 | | - } |
24 | | - } |
25 | | - elif 'postgresql' in database_url.lower() or 'postgres' in database_url.lower(): |
26 | | - # PostgreSQL-specific settings (psycopg3 compatible) |
27 | | - return { |
28 | | - 'pool_size': 10, |
29 | | - 'max_overflow': 20, |
30 | | - 'pool_timeout': 30, |
31 | | - 'pool_recycle': 3600, |
32 | | - 'pool_pre_ping': True, |
33 | | - 'connect_args': { |
34 | | - 'connect_timeout': 10 |
35 | | - } |
36 | | - } |
37 | | - elif 'mysql' in database_url.lower() or 'mariadb' in database_url.lower(): |
38 | | - # MySQL/MariaDB-specific settings |
39 | | - return { |
40 | | - 'pool_size': 10, |
41 | | - 'max_overflow': 20, |
42 | | - 'pool_timeout': 30, |
43 | | - 'pool_recycle': 3600, |
44 | | - 'pool_pre_ping': True, |
45 | | - 'connect_args': { |
46 | | - 'charset': 'utf8mb4' |
47 | | - } |
48 | | - } |
49 | | - else: |
50 | | - # Default settings for other databases |
51 | | - return { |
52 | | - 'pool_timeout': 30, |
53 | | - 'pool_recycle': 3600, |
54 | | - 'pool_pre_ping': True |
55 | | - } |
56 | | - |
57 | 57 | SQLALCHEMY_ENGINE_OPTIONS = get_engine_options() |
58 | 58 |
|
59 | 59 | # Email configuration |
|
0 commit comments