Skip to content
Merged
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
23 changes: 19 additions & 4 deletions WebFiori/Framework/Session/SessionSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions tests/WebFiori/Framework/Tests/Session/SessionSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}
}
Loading