-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
30 lines (25 loc) · 911 Bytes
/
database.py
File metadata and controls
30 lines (25 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from config import DATABASE_URL
import logging
db = SQLAlchemy()
logger = logging.getLogger(__name__)
def init_db(app: Flask):
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
if not DATABASE_URL.startswith('sqlite'):
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'connect_args': {
'connect_timeout': 5
}
}
else:
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {}
db.init_app(app)
with app.app_context():
try:
db.create_all()
logger.info("Database tables created successfully")
except Exception as e:
logger.error(f"Could not create database tables: {e}")
logger.warning("Application will start but database operations may fail")