From a41247d4e2c78c6a5f9073eca503edb19eb23fe6 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sun, 27 Sep 2026 15:37:40 +0300 Subject: [PATCH 1/2] fix(session): store session_kv_data.svalue as NVARCHAR(MAX) on MSSQL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On SQL Server the per-key value column (declared as 'mediumtext') was auto-mapped to nvarchar(4000), capping a single session key's value at 4000 characters — far smaller than MySQL's mediumtext (~16 MB). Declare svalue as nvarchar with size -1 for MSSQL so the schema/migration render it as NVARCHAR(MAX) (~2 GB). MySQL keeps mediumtext and SQLite keeps an unbounded text column. The per-key version/optimistic-concurrency model is unchanged (no chunking needed). --- WebFiori/Framework/Session/SessionSchema.php | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/WebFiori/Framework/Session/SessionSchema.php b/WebFiori/Framework/Session/SessionSchema.php index 26f55126e..a5af84fcb 100644 --- a/WebFiori/Framework/Session/SessionSchema.php +++ b/WebFiori/Framework/Session/SessionSchema.php @@ -64,6 +64,24 @@ public static function createSessionDataTable(string $dbType): Table { */ public static function createSessionKvDataTable(string $dbType): Table { $sessionsTable = self::createSessionsTable($dbType); + + // On SQL Server, an unsized/auto-mapped text column resolves to + // nvarchar(4000), which caps a single session key's value at 4000 + // characters. Declare it as nvarchar with size -1 so it renders as + // NVARCHAR(MAX) (~2 GB). MySQL uses mediumtext (~16 MB) and SQLite + // uses an unbounded text column. + if ($dbType === 'mssql') { + $svalue = [ + 'type' => 'nvarchar', + 'size' => -1, + 'comment' => 'The JSON-encoded session value.', + ]; + } else { + $svalue = [ + 'type' => 'mediumtext', + 'comment' => 'The JSON-encoded session value.', + ]; + } $table = TableFactory::create($dbType, 'session_kv_data', [ 's_id' => [ 'type' => 'varchar', @@ -77,10 +95,7 @@ public static function createSessionKvDataTable(string $dbType): Table { 'primary' => true, 'comment' => 'The session key name.', ], - 'svalue' => [ - 'type' => 'mediumtext', - 'comment' => 'The JSON-encoded session value.', - ], + 'svalue' => $svalue, 'version' => [ 'type' => 'int', 'default' => 1, From 1517dd381ea7a4c0441b6d060edfca581c6e4519 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sun, 27 Sep 2026 15:39:18 +0300 Subject: [PATCH 2/2] test(session): assert svalue is NVARCHAR(MAX) on MSSQL and s_id FK matches PK Covers the MSSQL nvarchar(max) fix for session_kv_data.svalue, that MySQL keeps mediumtext, and that the session-ID foreign key type/size matches the sessions primary key across mysql/mssql/sqlite. --- .../Tests/Session/SessionSchemaTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/WebFiori/Framework/Tests/Session/SessionSchemaTest.php b/tests/WebFiori/Framework/Tests/Session/SessionSchemaTest.php index 16e5671bd..f52e41a1e 100644 --- a/tests/WebFiori/Framework/Tests/Session/SessionSchemaTest.php +++ b/tests/WebFiori/Framework/Tests/Session/SessionSchemaTest.php @@ -57,4 +57,56 @@ public function testCreateSessionDataTableSQLite() { $this->assertNotNull($table->getColByKey('chunk-number')); $this->assertNotNull($table->getColByKey('data')); } + /** + * @test + * + * On MSSQL the per-key value column must be NVARCHAR(MAX) (size -1), not + * the auto-mapped nvarchar(4000) which would cap a key value at 4000 chars. + */ + public function testKvDataSvalueIsNvarcharMaxOnMSSQL() { + $table = SessionSchema::createSessionKvDataTable('mssql'); + $col = $table->getColByKey('svalue'); + $this->assertNotNull($col); + $this->assertEquals('nvarchar', $col->getDatatype()); + $this->assertEquals(-1, $col->getSize()); + } + /** + * @test + * + * MySQL keeps mediumtext (~16 MB) for the per-key value column. + */ + public function testKvDataSvalueIsMediumTextOnMySQL() { + $table = SessionSchema::createSessionKvDataTable('mysql'); + $col = $table->getColByKey('svalue'); + $this->assertNotNull($col); + $this->assertEquals('mediumtext', $col->getDatatype()); + } + /** + * @test + * + * The per-key table's session-ID foreign key must match the referenced + * sessions primary key type/size on every engine. + */ + public function testKvDataSessionIdMatchesSessionsPk() { + foreach (['mysql', 'mssql', 'sqlite'] as $dbType) { + $sessions = SessionSchema::createSessionsTable($dbType); + $kv = SessionSchema::createSessionKvDataTable($dbType); + + $pk = $sessions->getColByKey('s-id'); + $fk = $kv->getColByKey('s-id'); + + $this->assertNotNull($pk, "$dbType: sessions.s_id missing"); + $this->assertNotNull($fk, "$dbType: session_kv_data.s_id missing"); + $this->assertEquals( + $pk->getDatatype(), + $fk->getDatatype(), + "$dbType: s_id type mismatch between sessions and session_kv_data" + ); + $this->assertEquals( + $pk->getSize(), + $fk->getSize(), + "$dbType: s_id size mismatch between sessions and session_kv_data" + ); + } + } }