Rework upgrade needed and check upgrade too far as an error for the database

This commit is contained in:
Gregoo 2025-01-22 11:50:31 +01:00
parent a3e50e9b3c
commit b475bfe8d4
3 changed files with 25 additions and 12 deletions

View File

@ -81,6 +81,12 @@ class BrickSQL(object):
error=str(e) error=str(e)
)) ))
if self.upgrade_too_far():
raise DatabaseException('Your database version ({version}) is too far ahead for this version of the application. Expected at most {required}'.format( # noqa: E501
version=self.version,
required=__database_version__,
))
# Debug: Attach the debugger # Debug: Attach the debugger
# Uncomment manually because this is ultra verbose # Uncomment manually because this is ultra verbose
# self.connection.set_trace_callback(print) # self.connection.set_trace_callback(print)
@ -90,7 +96,7 @@ class BrickSQL(object):
setattr(g, G_STATS, self.stats) setattr(g, G_STATS, self.stats)
if not failsafe: if not failsafe:
if self.needs_upgrade(): if self.upgrade_needed():
raise DatabaseException('Your database need to be upgraded from version {version} to version {required}'.format( # noqa: E501 raise DatabaseException('Your database need to be upgraded from version {version} to version {required}'.format( # noqa: E501
version=self.version, version=self.version,
required=__database_version__, required=__database_version__,
@ -265,10 +271,6 @@ class BrickSQL(object):
return template.render(**context) return template.render(**context)
# Tells whether the database needs upgrade
def needs_upgrade(self) -> bool:
return self.version < __database_version__
# Raw execute the query without any options # Raw execute the query without any options
def raw_execute( def raw_execute(
self, self,
@ -284,7 +286,7 @@ class BrickSQL(object):
# Upgrade the database # Upgrade the database
def upgrade(self) -> None: def upgrade(self) -> None:
if self.needs_upgrade(): if self.upgrade_needed():
for pending in BrickSQLMigrationList().pending(self.version): for pending in BrickSQLMigrationList().pending(self.version):
logger.info('Applying migration {version}'.format( logger.info('Applying migration {version}'.format(
version=pending.version) version=pending.version)
@ -293,6 +295,14 @@ class BrickSQL(object):
self.executescript(pending.get_query()) self.executescript(pending.get_query())
self.execute('schema/set_version', version=pending.version) self.execute('schema/set_version', version=pending.version)
# Tells whether the database needs upgrade
def upgrade_needed(self) -> bool:
return self.version < __database_version__
# Tells whether the database is too far
def upgrade_too_far(self) -> bool:
return self.version > __database_version__
# Clean the query for debugging # Clean the query for debugging
@staticmethod @staticmethod
def clean_query(query: str, /) -> str: def clean_query(query: str, /) -> str:

View File

@ -41,7 +41,7 @@ admin_page = Blueprint('admin', __name__, url_prefix='/admin')
def admin() -> str: def admin() -> str:
database_counters: list[BrickCounter] = [] database_counters: list[BrickCounter] = []
database_exception: Exception | None = None database_exception: Exception | None = None
database_needs_upgrade: bool = False database_upgrade_needed: bool = False
database_version: int = -1 database_version: int = -1
nil_minifigure_name: str = '' nil_minifigure_name: str = ''
nil_minifigure_url: str = '' nil_minifigure_url: str = ''
@ -51,10 +51,10 @@ def admin() -> str:
# This view needs to be protected against SQL errors # This view needs to be protected against SQL errors
try: try:
database = BrickSQL(failsafe=True) database = BrickSQL(failsafe=True)
database_needs_upgrade = database.needs_upgrade() database_upgrade_needed = database.upgrade_needed()
database_version = database.version database_version = database.version
if not database_needs_upgrade: if not database_upgrade_needed:
database_counters = BrickSQL().count_records() database_counters = BrickSQL().count_records()
except Exception as e: except Exception as e:
database_exception = e database_exception = e
@ -96,7 +96,7 @@ def admin() -> str:
database_counters=database_counters, database_counters=database_counters,
database_error=request.args.get('error'), database_error=request.args.get('error'),
database_exception=database_exception, database_exception=database_exception,
database_needs_upgrade=database_needs_upgrade, database_upgrade_needed=database_upgrade_needed,
database_version=database_version, database_version=database_version,
instructions=BrickInstructionsList(), instructions=BrickInstructionsList(),
nil_minifigure_name=nil_minifigure_name, nil_minifigure_name=nil_minifigure_name,
@ -317,9 +317,12 @@ def update_themes() -> Response:
@admin_page.route('/upgrade-database', methods=['GET']) @admin_page.route('/upgrade-database', methods=['GET'])
@login_required @login_required
@exception_handler(__file__, post_redirect='admin.admin') @exception_handler(__file__, post_redirect='admin.admin')
def upgrade_database() -> str: def upgrade_database() -> str | Response:
database = BrickSQL(failsafe=True) database = BrickSQL(failsafe=True)
if not database.upgrade_needed():
return redirect(url_for('admin.admin'))
return render_template( return render_template(
'admin.html', 'admin.html',
upgrade_database=True, upgrade_database=True,

View File

@ -4,7 +4,7 @@
<h5 class="border-bottom">Status</h5> <h5 class="border-bottom">Status</h5>
{% if database_exception %}<div class="alert alert-danger" role="alert">An exception occured while loading this page: {{ database_exception }}</div>{% endif %} {% if database_exception %}<div class="alert alert-danger" role="alert">An exception occured while loading this page: {{ database_exception }}</div>{% endif %}
{% if database_error %}<div class="alert alert-danger" role="alert"><strong>Error:</strong> {{ database_error }}.</div>{% endif %} {% if database_error %}<div class="alert alert-danger" role="alert"><strong>Error:</strong> {{ database_error }}.</div>{% endif %}
{% if database_needs_upgrade %} {% if database_upgrade_needed %}
<div class="alert alert-warning" role="alert"> <div class="alert alert-warning" role="alert">
<p>Your database needs to be upgraded.</p> <p>Your database needs to be upgraded.</p>
<hr> <hr>