-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·48 lines (36 loc) · 1.46 KB
/
database.py
File metadata and controls
executable file
·48 lines (36 loc) · 1.46 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import urllib.parse
from flask_sqlalchemy import SQLAlchemy
from config import load_config
db = SQLAlchemy()
def init_db(app):
config = load_config()
db_type = config.get('db_type', 'sqlite')
if db_type == 'sqlite':
# Default internal path
db_uri = 'sqlite:////data/scribble.db'
elif db_type == 'postgres':
# postgresql://user:password@host/dbname
user = config.get('db_username')
# URL encode the password to handle special chars like @ safely
pw = urllib.parse.quote_plus(config.get('db_password'))
host = config.get('db_address')
dbname = config.get('db_name')
db_uri = f"postgresql://{user}:{pw}@{host}/{dbname}"
elif db_type == 'mariadb':
# mysql+pymysql://user:password@host/dbname
user = config.get('db_username')
# URL encode the password to handle special chars like @ safely
pw = urllib.parse.quote_plus(config.get('db_password'))
host = config.get('db_address')
dbname = config.get('db_name')
db_uri = f"mysql+pymysql://{user}:{pw}@{host}/{dbname}"
else:
# Fallback
db_uri = 'sqlite:////data/scribble.db'
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
with app.app_context():
# This creates tables if they don't exist
db.create_all()