diff --git a/subscribie/__init__.py b/subscribie/__init__.py index 33a639a3..83b61c63 100644 --- a/subscribie/__init__.py +++ b/subscribie/__init__.py @@ -44,7 +44,7 @@ import click from jinja2 import Template from .models import PaymentProvider, Person, Company, Module, Plan, PriceList -from .bootstrap_app import migrate_database +from .bootstrap_app import migrate_database, set_app_default_settings load_dotenv(verbose=True) @@ -67,10 +67,14 @@ def create_app(test_config=None): with app.app_context(): database.init_app(app) # Initialize flask_migrate using Migrate + # Note: Migrate configures the flask_migrate addon- it does not + # *perform* a migration Migrate(app, database) - # Trigger a programatic database migration during application boot + + # Perform a programatic database migration during application boot # Note: flask_migrate calls database migrations 'upgrades'. migrate_database() + set_app_default_settings() if test_config is not None: app.config.update(test_config) diff --git a/subscribie/blueprints/admin/__init__.py b/subscribie/blueprints/admin/__init__.py index 0126be6f..db45d516 100644 --- a/subscribie/blueprints/admin/__init__.py +++ b/subscribie/blueprints/admin/__init__.py @@ -1202,17 +1202,9 @@ def connect_tawk_manually(): @login_required def add_custom_code(): setting = Setting.query.first() - if setting is None: - setting = Setting() - database.session.add(setting) - if request.method == "POST": custom_code = request.form.get("code", None) if custom_code is not None: - setting = Setting.query.first() - if setting is None: - setting = Setting() - database.session.add(setting) setting.custom_code = custom_code database.session.commit() flash("Custom code added") diff --git a/subscribie/bootstrap_app.py b/subscribie/bootstrap_app.py index c2096a00..486e3351 100644 --- a/subscribie/bootstrap_app.py +++ b/subscribie/bootstrap_app.py @@ -3,6 +3,8 @@ from .logger import logger # noqa: F401 import logging from flask import current_app +from subscribie.database import database +from subscribie.models import Setting log = logging.getLogger(__name__) @@ -13,3 +15,24 @@ def migrate_database(): upgrade( directory=Path(current_app.config["SUBSCRIBIE_REPO_DIRECTORY"] + "/migrations") ) + + +def set_app_default_settings(): + """Pre-populate the Settings model with the column insertion defaults. + + If the Settings model is empty, pre-populate the database with the column + insertion defaults (see model.py -> class Settings). + + Note this does not relate to .env settings. See README.md + The Settings stored in the Settings database model are for + user controllable settings which may be changed at runtime + without an application restart. + + Issue #1262 + https://github.com/Subscribie/subscribie/issues/1262 + """ + setting = Setting.query.first() + if setting is None: + setting = Setting() + database.session.add(setting) + database.session.commit()