Skip to content

Commit 4c9b0c1

Browse files
committed
DB management ENH - SQLite Fallback in all cases
1 parent 61701a6 commit 4c9b0c1

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

apps/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Copyright (c) 2019 - present AppSeed.us
44
"""
55

6+
import os
7+
68
from flask import Flask
79
from flask_login import LoginManager
810
from flask_sqlalchemy import SQLAlchemy
@@ -28,7 +30,18 @@ def configure_database(app):
2830

2931
@app.before_first_request
3032
def initialize_database():
31-
db.create_all()
33+
try:
34+
db.create_all()
35+
except Exception as e:
36+
37+
print('> Error: DBMS Exception: ' + str(e) )
38+
39+
# fallback to SQLite
40+
basedir = os.path.abspath(os.path.dirname(__file__))
41+
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
42+
43+
print('> Fallback to SQLite ')
44+
db.create_all()
3245

3346
@app.teardown_request
3447
def shutdown_session(exception=None):

apps/config.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,57 @@
33
Copyright (c) 2019 - present AppSeed.us
44
"""
55

6-
import os
6+
import os, random, string
77

88
class Config(object):
99

1010
basedir = os.path.abspath(os.path.dirname(__file__))
1111

12+
# Assets Management
13+
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
14+
1215
# Set up the App SECRET_KEY
13-
# SECRET_KEY = config('SECRET_KEY' , default='S#perS3crEt_007')
14-
SECRET_KEY = os.getenv('SECRET_KEY', 'S#perS3crEt_007')
16+
SECRET_KEY = os.getenv('SECRET_KEY', None)
17+
if not SECRET_KEY:
18+
SECRET_KEY = ''.join(random.choice( string.ascii_lowercase ) for i in range( 32 ))
1519

16-
# This will create a file in <app> FOLDER
17-
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
18-
SQLALCHEMY_TRACK_MODIFICATIONS = False
20+
SQLALCHEMY_TRACK_MODIFICATIONS = False
1921

20-
# Assets Management
21-
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
22+
DB_ENGINE = os.getenv('DB_ENGINE' , None)
23+
DB_USERNAME = os.getenv('DB_USERNAME' , None)
24+
DB_PASS = os.getenv('DB_PASS' , None)
25+
DB_HOST = os.getenv('DB_HOST' , None)
26+
DB_PORT = os.getenv('DB_PORT' , None)
27+
DB_NAME = os.getenv('DB_NAME' , None)
28+
29+
USE_SQLITE = True
30+
31+
# try to set up a Relational DBMS
32+
if DB_ENGINE and DB_NAME and DB_USERNAME:
33+
34+
try:
35+
36+
# Relational DBMS: PSQL, MySql
37+
SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
38+
DB_ENGINE,
39+
DB_USERNAME,
40+
DB_PASS,
41+
DB_HOST,
42+
DB_PORT,
43+
DB_NAME
44+
)
45+
46+
USE_SQLITE = False
47+
48+
except Exception as e:
49+
50+
print('> Error: DBMS Exception: ' + str(e) )
51+
print('> Fallback to SQLite ')
52+
53+
if USE_SQLITE:
54+
55+
# This will create a file in <app> FOLDER
56+
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
2257

2358
class ProductionConfig(Config):
2459
DEBUG = False
@@ -28,15 +63,6 @@ class ProductionConfig(Config):
2863
REMEMBER_COOKIE_HTTPONLY = True
2964
REMEMBER_COOKIE_DURATION = 3600
3065

31-
# PostgreSQL database
32-
SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
33-
os.getenv('DB_ENGINE' , 'mysql'),
34-
os.getenv('DB_USERNAME' , 'appseed_db_usr'),
35-
os.getenv('DB_PASS' , 'pass'),
36-
os.getenv('DB_HOST' , 'localhost'),
37-
os.getenv('DB_PORT' , 3306),
38-
os.getenv('DB_NAME' , 'appseed_db')
39-
)
4066

4167
class DebugConfig(Config):
4268
DEBUG = True

0 commit comments

Comments
 (0)