From 8fe5164bfec3ef55cac93a2b35b42a8f7d862660 Mon Sep 17 00:00:00 2001 From: App Generator <51070104+app-generator@users.noreply.github.com> Date: Sat, 31 Dec 2022 07:20:01 +0200 Subject: [PATCH] Release v1.0.9 - DB Management Improvement --- .env | 13 ++++++++- CHANGELOG.md | 6 ++++ apps/__init__.py | 13 ++++++++- apps/config.py | 76 +++++++++++++++++++++++++++++++----------------- env.sample | 22 +++++++------- requirements.txt | 1 + 6 files changed, 93 insertions(+), 38 deletions(-) diff --git a/.env b/.env index 5c8316e..5f23896 100644 --- a/.env +++ b/.env @@ -5,10 +5,21 @@ DEBUG=True FLASK_APP=run.py FLASK_ENV=development +# If not provided, a random one is generated +# SECRET_KEY= + # Used for CDN (in production) # No Slash at the end ASSETS_ROOT=/static/assets -# LOCAL 5001 FLask +# If DB credentials (if NOT provided, or wrong values SQLite is used) +# DB_ENGINE=mysql +# DB_HOST=localhost +# DB_NAME=appseed_db +# DB_USERNAME=appseed_db_usr +# DB_PASS=pass +# DB_PORT=3306 + +# LOCAL 5001 Flask # GITHUB_ID = # GITHUB_SECRET = diff --git a/CHANGELOG.md b/CHANGELOG.md index c01f640..0a19fff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [1.0.9] 2022-12-31 +### Changes + +- `DB Management` Improvement + - `Silent fallback` to **SQLite** + ## [1.0.8] 2022-09-07 ### Improvements diff --git a/apps/__init__.py b/apps/__init__.py index 123eb62..c3647e4 100644 --- a/apps/__init__.py +++ b/apps/__init__.py @@ -28,7 +28,18 @@ def configure_database(app): @app.before_first_request def initialize_database(): - db.create_all() + try: + db.create_all() + except Exception as e: + + print('> Error: DBMS Exception: ' + str(e) ) + + # fallback to SQLite + basedir = os.path.abspath(os.path.dirname(__file__)) + app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3') + + print('> Fallback to SQLite ') + db.create_all() @app.teardown_request def shutdown_session(exception=None): diff --git a/apps/config.py b/apps/config.py index 2cd3be2..0756965 100644 --- a/apps/config.py +++ b/apps/config.py @@ -3,32 +3,68 @@ Copyright (c) 2019 - present AppSeed.us """ -import os +import os, random, string class Config(object): basedir = os.path.abspath(os.path.dirname(__file__)) - # Set up the App SECRET_KEY - # SECRET_KEY = config('SECRET_KEY' , default='S#perS3crEt_007') - SECRET_KEY = os.getenv('SECRET_KEY', 'S#perS3crEt_007') - - # This will create a file in FOLDER - SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3') - SQLALCHEMY_TRACK_MODIFICATIONS = False - # Assets Management - ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets') + ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets') + # Set up the App SECRET_KEY + SECRET_KEY = os.getenv('SECRET_KEY', None) + if not SECRET_KEY: + SECRET_KEY = ''.join(random.choice( string.ascii_lowercase ) for i in range( 32 )) + + # Social AUTH context SOCIAL_AUTH_GITHUB = False - GITHUB_ID = os.getenv('GITHUB_ID') - GITHUB_SECRET = os.getenv('GITHUB_SECRET') + GITHUB_ID = os.getenv('GITHUB_ID' , None) + GITHUB_SECRET = os.getenv('GITHUB_SECRET', None) # Enable/Disable Github Social Login if GITHUB_ID and GITHUB_SECRET: - SOCIAL_AUTH_GITHUB = True - + SOCIAL_AUTH_GITHUB = True + + SQLALCHEMY_TRACK_MODIFICATIONS = False + + DB_ENGINE = os.getenv('DB_ENGINE' , None) + DB_USERNAME = os.getenv('DB_USERNAME' , None) + DB_PASS = os.getenv('DB_PASS' , None) + DB_HOST = os.getenv('DB_HOST' , None) + DB_PORT = os.getenv('DB_PORT' , None) + DB_NAME = os.getenv('DB_NAME' , None) + + USE_SQLITE = True + + # try to set up a Relational DBMS + if DB_ENGINE and DB_NAME and DB_USERNAME: + + try: + + # Relational DBMS: PSQL, MySql + SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format( + DB_ENGINE, + DB_USERNAME, + DB_PASS, + DB_HOST, + DB_PORT, + DB_NAME + ) + + USE_SQLITE = False + + except Exception as e: + + print('> Error: DBMS Exception: ' + str(e) ) + print('> Fallback to SQLite ') + + if USE_SQLITE: + + # This will create a file in FOLDER + SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3') + class ProductionConfig(Config): DEBUG = False @@ -37,21 +73,9 @@ class ProductionConfig(Config): REMEMBER_COOKIE_HTTPONLY = True REMEMBER_COOKIE_DURATION = 3600 - # PostgreSQL database - SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format( - os.getenv('DB_ENGINE' , 'mysql'), - os.getenv('DB_USERNAME' , 'appseed_db_usr'), - os.getenv('DB_PASS' , 'pass'), - os.getenv('DB_HOST' , 'localhost'), - os.getenv('DB_PORT' , 3306), - os.getenv('DB_NAME' , 'appseed_db') - ) - - class DebugConfig(Config): DEBUG = True - # Load all possible configurations config_dict = { 'Production': ProductionConfig, diff --git a/env.sample b/env.sample index 1450d95..47bc1da 100644 --- a/env.sample +++ b/env.sample @@ -1,21 +1,23 @@ -# True for development, False for production +# True in development, False in production DEBUG=True -# Flask ENV FLASK_APP=run.py -SECRET_KEY=YOUR_SUPER_KEY +FLASK_ENV=development -# Used for CDN (in production) -# No Slash at the end -ASSETS_ROOT=/static/assets +# If not provided, a random one is generated +# SECRET_KEY= -# If DEBUG=False (production mode) +# If DB credentials (if NOT provided, or wrong values SQLite is used) # DB_ENGINE=mysql -# DB_NAME=appseed_db # DB_HOST=localhost -# DB_PORT=3306 +# DB_NAME=appseed_db # DB_USERNAME=appseed_db_usr -# DB_PASS= +# DB_PASS=pass +# DB_PORT=3306 + +# Used for CDN (in production) +# No Slash at the end +ASSETS_ROOT=/static/assets # SOCIAL AUTH Github # GITHUB_ID=YOUR_GITHUB_ID diff --git a/requirements.txt b/requirements.txt index fdc0e19..8b101b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ blinker==1.4 pyOpenSSL # flask_mysqldb +# psycopg2-binary