Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip Fix #1262 remove before_app_first_request for migrate_database #1263

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ wheel
Flask>=2,<3
flask_cors
Flask-Reuploaded==0.3.2
Flask-WTF==1.0.0
Flask-WTF==1.2.1
email-validator==1.1.3
Flask-Mail>=0.9.1
requests==2.31.0
Expand Down
21 changes: 18 additions & 3 deletions subscribie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
import click
from jinja2 import Template
from .models import PaymentProvider, Person, Company, Module, Plan, PriceList
from .bootstrap_app import (
migrate_database,
set_app_default_settings,
set_plans_default_category,
)

load_dotenv(verbose=True)

Expand All @@ -63,6 +68,19 @@ def create_app(test_config=None):
app.config.update(os.environ)
app.config["PERMANENT_SESSION_LIFETIME"] = PERMANENT_SESSION_LIFETIME

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)

# Perform a programatic database migration during application boot
# Note: flask_migrate calls database migrations 'upgrades'.
migrate_database()
set_app_default_settings()
set_plans_default_category()

if test_config is not None:
app.config.update(test_config)

Expand Down Expand Up @@ -152,9 +170,6 @@ def register_modules():
app.add_url_rule("/", "index", views.__getattribute__("choose"))

with app.app_context():
database.init_app(app)
Migrate(app, database)

try:
payment_provider = PaymentProvider.query.first()
if payment_provider is None:
Expand Down
8 changes: 0 additions & 8 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
55 changes: 55 additions & 0 deletions subscribie/bootstrap_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pathlib import Path
from flask_migrate import upgrade
from .logger import logger # noqa: F401
import logging
from flask import current_app
from subscribie.database import database
from subscribie.models import Setting, Plan, Category

log = logging.getLogger(__name__)


def migrate_database():
"""Migrate database when app first boots"""
log.info("Migrating 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()


def set_plans_default_category():
"""Add all plans to a default category if they are not associated with one"""
# Add all plans to one
if Category.query.count() == 0: # If no categories, create default
category = Category()
# Note this string is not translated since is populated
# during bootstrap. category.name titles may be edited in the
# admin dashboard in the 'Manage Categories' section
category.name = "Make your choice"
# Add all plans to this category
plans = Plan.query.all()
for plan in plans:
plan.category = category
database.session.add(category)
database.session.commit()
24 changes: 0 additions & 24 deletions subscribie/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
Markup,
)
from .models import Company, Plan, Integration, Page, Category, Setting, PaymentProvider
from flask_migrate import upgrade
from subscribie.blueprints.style import inject_custom_style
from subscribie.database import database
from subscribie.signals import register_signal_handlers
from subscribie.blueprints.admin.stats import (
get_number_of_active_subscribers,
Expand All @@ -45,15 +43,6 @@
register_signal_handlers()


@bp.before_app_first_request
def migrate_database():
"""Migrate database when app first boots"""
log.info("Migrating database")
upgrade(
directory=Path(current_app.config["SUBSCRIBIE_REPO_DIRECTORY"] + "/migrations")
)


@bp.before_app_request
def on_each_request():
# Detect country code if present in the request from proxy
Expand Down Expand Up @@ -110,19 +99,6 @@ def on_each_request():
f'session fallback_default_country_active is: {session["fallback_default_country_active"]}' # noqa: E501
)

# Add all plans to one
if Category.query.count() == 0: # If no categories, create default
category = Category()
# Note this string is not translated since is populated
# during bootstrap. category.name titles may be edited in the
# admin dashboard in the 'Manage Categories' section
category.name = "Make your choice"
# Add all plans to this category
plans = Plan.query.all()
for plan in plans:
plan.category = category
database.session.add(category)


@bp.before_app_request
def check_if_inside_iframe():
Expand Down
Loading