Skip to content

Commit 8fe5164

Browse files
committed
Release v1.0.9 - DB Management Improvement
1 parent f2d00a9 commit 8fe5164

File tree

6 files changed

+93
-38
lines changed

6 files changed

+93
-38
lines changed

.env

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ DEBUG=True
55
FLASK_APP=run.py
66
FLASK_ENV=development
77

8+
# If not provided, a random one is generated
9+
# SECRET_KEY=<YOUR_SUPER_KEY_HERE>
10+
811
# Used for CDN (in production)
912
# No Slash at the end
1013
ASSETS_ROOT=/static/assets
1114

12-
# LOCAL 5001 FLask
15+
# If DB credentials (if NOT provided, or wrong values SQLite is used)
16+
# DB_ENGINE=mysql
17+
# DB_HOST=localhost
18+
# DB_NAME=appseed_db
19+
# DB_USERNAME=appseed_db_usr
20+
# DB_PASS=pass
21+
# DB_PORT=3306
22+
23+
# LOCAL 5001 Flask
1324
# GITHUB_ID = <YOUR_GITHUB_ID>
1425
# GITHUB_SECRET = <YOUR_GITHUB_SECRET>

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## [1.0.9] 2022-12-31
4+
### Changes
5+
6+
- `DB Management` Improvement
7+
- `Silent fallback` to **SQLite**
8+
39
## [1.0.8] 2022-09-07
410
### Improvements
511

apps/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ def configure_database(app):
2828

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

3344
@app.teardown_request
3445
def shutdown_session(exception=None):

apps/config.py

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,68 @@
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-
# 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')
15-
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
19-
2012
# Assets Management
21-
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
13+
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
2214

15+
# Set up the App SECRET_KEY
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 ))
19+
20+
# Social AUTH context
2321
SOCIAL_AUTH_GITHUB = False
2422

25-
GITHUB_ID = os.getenv('GITHUB_ID')
26-
GITHUB_SECRET = os.getenv('GITHUB_SECRET')
23+
GITHUB_ID = os.getenv('GITHUB_ID' , None)
24+
GITHUB_SECRET = os.getenv('GITHUB_SECRET', None)
2725

2826
# Enable/Disable Github Social Login
2927
if GITHUB_ID and GITHUB_SECRET:
30-
SOCIAL_AUTH_GITHUB = True
31-
28+
SOCIAL_AUTH_GITHUB = True
29+
30+
SQLALCHEMY_TRACK_MODIFICATIONS = False
31+
32+
DB_ENGINE = os.getenv('DB_ENGINE' , None)
33+
DB_USERNAME = os.getenv('DB_USERNAME' , None)
34+
DB_PASS = os.getenv('DB_PASS' , None)
35+
DB_HOST = os.getenv('DB_HOST' , None)
36+
DB_PORT = os.getenv('DB_PORT' , None)
37+
DB_NAME = os.getenv('DB_NAME' , None)
38+
39+
USE_SQLITE = True
40+
41+
# try to set up a Relational DBMS
42+
if DB_ENGINE and DB_NAME and DB_USERNAME:
43+
44+
try:
45+
46+
# Relational DBMS: PSQL, MySql
47+
SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
48+
DB_ENGINE,
49+
DB_USERNAME,
50+
DB_PASS,
51+
DB_HOST,
52+
DB_PORT,
53+
DB_NAME
54+
)
55+
56+
USE_SQLITE = False
57+
58+
except Exception as e:
59+
60+
print('> Error: DBMS Exception: ' + str(e) )
61+
print('> Fallback to SQLite ')
62+
63+
if USE_SQLITE:
64+
65+
# This will create a file in <app> FOLDER
66+
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
67+
3268
class ProductionConfig(Config):
3369
DEBUG = False
3470

@@ -37,21 +73,9 @@ class ProductionConfig(Config):
3773
REMEMBER_COOKIE_HTTPONLY = True
3874
REMEMBER_COOKIE_DURATION = 3600
3975

40-
# PostgreSQL database
41-
SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
42-
os.getenv('DB_ENGINE' , 'mysql'),
43-
os.getenv('DB_USERNAME' , 'appseed_db_usr'),
44-
os.getenv('DB_PASS' , 'pass'),
45-
os.getenv('DB_HOST' , 'localhost'),
46-
os.getenv('DB_PORT' , 3306),
47-
os.getenv('DB_NAME' , 'appseed_db')
48-
)
49-
50-
5176
class DebugConfig(Config):
5277
DEBUG = True
5378

54-
5579
# Load all possible configurations
5680
config_dict = {
5781
'Production': ProductionConfig,

env.sample

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# True for development, False for production
1+
# True in development, False in production
22
DEBUG=True
33

4-
# Flask ENV
54
FLASK_APP=run.py
6-
SECRET_KEY=YOUR_SUPER_KEY
5+
FLASK_ENV=development
76

8-
# Used for CDN (in production)
9-
# No Slash at the end
10-
ASSETS_ROOT=/static/assets
7+
# If not provided, a random one is generated
8+
# SECRET_KEY=<YOUR_SUPER_KEY_HERE>
119

12-
# If DEBUG=False (production mode)
10+
# If DB credentials (if NOT provided, or wrong values SQLite is used)
1311
# DB_ENGINE=mysql
14-
# DB_NAME=appseed_db
1512
# DB_HOST=localhost
16-
# DB_PORT=3306
13+
# DB_NAME=appseed_db
1714
# DB_USERNAME=appseed_db_usr
18-
# DB_PASS=<STRONG_PASS>
15+
# DB_PASS=pass
16+
# DB_PORT=3306
17+
18+
# Used for CDN (in production)
19+
# No Slash at the end
20+
ASSETS_ROOT=/static/assets
1921

2022
# SOCIAL AUTH Github
2123
# GITHUB_ID=YOUR_GITHUB_ID

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ blinker==1.4
1616
pyOpenSSL
1717

1818
# flask_mysqldb
19+
# psycopg2-binary

0 commit comments

Comments
 (0)