diff --git a/bricktracker/sql.py b/bricktracker/sql.py index 57a8468..18c6c94 100644 --- a/bricktracker/sql.py +++ b/bricktracker/sql.py @@ -81,6 +81,12 @@ class BrickSQL(object): 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 # Uncomment manually because this is ultra verbose # self.connection.set_trace_callback(print) @@ -90,7 +96,7 @@ class BrickSQL(object): setattr(g, G_STATS, self.stats) 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 version=self.version, required=__database_version__, @@ -265,10 +271,6 @@ class BrickSQL(object): 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 def raw_execute( self, @@ -284,7 +286,7 @@ class BrickSQL(object): # Upgrade the database def upgrade(self) -> None: - if self.needs_upgrade(): + if self.upgrade_needed(): for pending in BrickSQLMigrationList().pending(self.version): logger.info('Applying migration {version}'.format( version=pending.version) @@ -293,6 +295,14 @@ class BrickSQL(object): self.executescript(pending.get_query()) 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 @staticmethod def clean_query(query: str, /) -> str: diff --git a/bricktracker/views/admin.py b/bricktracker/views/admin.py index b97e0fa..3546c98 100644 --- a/bricktracker/views/admin.py +++ b/bricktracker/views/admin.py @@ -41,7 +41,7 @@ admin_page = Blueprint('admin', __name__, url_prefix='/admin') def admin() -> str: database_counters: list[BrickCounter] = [] database_exception: Exception | None = None - database_needs_upgrade: bool = False + database_upgrade_needed: bool = False database_version: int = -1 nil_minifigure_name: str = '' nil_minifigure_url: str = '' @@ -51,10 +51,10 @@ def admin() -> str: # This view needs to be protected against SQL errors try: database = BrickSQL(failsafe=True) - database_needs_upgrade = database.needs_upgrade() + database_upgrade_needed = database.upgrade_needed() database_version = database.version - if not database_needs_upgrade: + if not database_upgrade_needed: database_counters = BrickSQL().count_records() except Exception as e: database_exception = e @@ -96,7 +96,7 @@ def admin() -> str: database_counters=database_counters, database_error=request.args.get('error'), database_exception=database_exception, - database_needs_upgrade=database_needs_upgrade, + database_upgrade_needed=database_upgrade_needed, database_version=database_version, instructions=BrickInstructionsList(), nil_minifigure_name=nil_minifigure_name, @@ -317,9 +317,12 @@ def update_themes() -> Response: @admin_page.route('/upgrade-database', methods=['GET']) @login_required @exception_handler(__file__, post_redirect='admin.admin') -def upgrade_database() -> str: +def upgrade_database() -> str | Response: database = BrickSQL(failsafe=True) + if not database.upgrade_needed(): + return redirect(url_for('admin.admin')) + return render_template( 'admin.html', upgrade_database=True, diff --git a/templates/admin/database.html b/templates/admin/database.html index f92374a..9e397ef 100644 --- a/templates/admin/database.html +++ b/templates/admin/database.html @@ -4,7 +4,7 @@
Your database needs to be upgraded.