vendredi 1 janvier 2021

Python Flask MySQL database schema for each environment

It is my first time working with a development, production and testing environment.

I have created three instances of my Flask application's database schema in MySQLWorkbench.

enter image description here

An environment variable FLASK_CONFIG must be set to any of the three environments.

This will determine which database my Flask app connects to.

My question simply is whether this is the right approach and, if not, what is a better solution?

Here is my code.

def create_app(config_name):
    """
        PARAM
        config_name    'development', 'production', or 'testing'
    """

    instance_path = os.path.join(os.path.dirname(__file__), 'instance')

    app = Flask(__name__, instance_path=instance_path)

    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')

    app.config['SECRET_KEY'] = 'a7e6f8131a635119ca1ce2c82c7206e2c07072a9dd264189'

    if(config_name=='development'):
        database = "cbl_dev"
    elif(config_name=='production'):
        database = "cbl_prod"
    elif(config_name=='testing'):
        database = "cbl_test"
    else:
        print("Invalid value: '{}' provided for env variable FLASK_CONFIG".format(config_name))

    db = mysql.connector.connect(
        host="localhost",
        user="root",
        password="12345678",
        database=database
    )

Aucun commentaire:

Enregistrer un commentaire