From 70d03f0b389ca668e0ac16f8799c3db94ed32f8f Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Tue, 24 Mar 2026 14:09:44 +0100 Subject: [PATCH 1/2] fix: Detect and sync missing columns even when schema version is unchanged ensureTableForRegisterSchema previously skipped sync when the stored schema version matched. If columns were added to a schema after the version was stored (without a proper sync), the table would be missing columns indefinitely. Now performs a fast sanity check comparing required columns against actual table columns. If any are missing, forces a sync regardless of the version hash. Closes #974 --- lib/Db/MagicMapper/MagicTableHandler.php | 34 ++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/Db/MagicMapper/MagicTableHandler.php b/lib/Db/MagicMapper/MagicTableHandler.php index 2e95920bd..76c52b2f6 100644 --- a/lib/Db/MagicMapper/MagicTableHandler.php +++ b/lib/Db/MagicMapper/MagicTableHandler.php @@ -111,19 +111,37 @@ public function ensureTableForRegisterSchema(Register $register, Schema $schema, if (($tableExists === true) && ($force === false)) { // Table exists and not forcing update - check if schema changed. if ($this->magicMapper->hasRegisterSchemaChanged(register: $register, schema: $schema) === false) { - $this->logger->debug( - message: '[MagicTableHandler] Table exists and schema unchanged, skipping', + // Sanity check: verify all schema columns exist in the table. + // The version hash can be stale if it was stored without a full sync. + $requiredColumns = $this->magicMapper->buildTableColumnsFromSchema(schema: $schema); + $currentColumns = $this->magicMapper->getExistingTableColumns(tableName: $tableName); + $missingColumns = array_diff_key($requiredColumns, array_flip($currentColumns)); + + if (empty($missingColumns) === true) { + $this->logger->debug( + message: '[MagicTableHandler] Table exists and schema unchanged, skipping', + context: [ + 'file' => __FILE__, + 'line' => __LINE__, + 'tableName' => $tableName, + 'cacheKey' => $cacheKey, + ] + ); + return true; + } + + $this->logger->warning( + message: '[MagicTableHandler] Schema version unchanged but columns missing, forcing sync', context: [ - 'file' => __FILE__, - 'line' => __LINE__, - 'tableName' => $tableName, - 'cacheKey' => $cacheKey, + 'file' => __FILE__, + 'line' => __LINE__, + 'tableName' => $tableName, + 'missingColumns' => array_keys($missingColumns), ] ); - return true; } - // Schema changed, update table. + // Schema changed or columns missing, update table. $result = $this->syncTableForRegisterSchema(register: $register, schema: $schema); return $result['success'] ?? true; } From 75de28ae96e044cdd0ba9c2d5f172ad1311ffe35 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Wed, 25 Mar 2026 16:28:22 +0100 Subject: [PATCH 2/2] fix: add missing end-if comments in MagicTableHandler --- lib/Db/MagicMapper/MagicTableHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Db/MagicMapper/MagicTableHandler.php b/lib/Db/MagicMapper/MagicTableHandler.php index 76c52b2f6..a5f326331 100644 --- a/lib/Db/MagicMapper/MagicTableHandler.php +++ b/lib/Db/MagicMapper/MagicTableHandler.php @@ -139,12 +139,12 @@ public function ensureTableForRegisterSchema(Register $register, Schema $schema, 'missingColumns' => array_keys($missingColumns), ] ); - } + }//end if // Schema changed or columns missing, update table. $result = $this->syncTableForRegisterSchema(register: $register, schema: $schema); return $result['success'] ?? true; - } + }//end if // Create new table or recreate if forced. if (($tableExists === true) && ($force === true)) {