修复业务镜像宽表行大小限制 #58

Merged
WuYongJie merged 1 commits from hotfix/WuYongJie-business-mirror-wide-schema into main 2026-08-14 21:02:59 +08:00
2 changed files with 41 additions and 2 deletions

View File

@ -112,7 +112,12 @@ class BusinessMirrorSchemaManager
'time' => $table->time($name, 6),
'uniqueidentifier' => $table->char($name, 36),
'binary', 'varbinary', 'image', 'timestamp', 'rowversion' => $table->binary($name),
'char', 'nchar', 'varchar', 'nvarchar' => $length > 0 && $length <= 1024
// MariaDB counts every inline varchar column towards the 65,535-byte
// row limit. Wide legacy SQL Server tables can contain dozens of
// nvarchar columns even though each individual column looks small.
// Keep only genuinely short values inline; TEXT is stored off-page
// and lets the mirror preserve the complete source schema.
'char', 'nchar', 'varchar', 'nvarchar' => $length > 0 && $length <= 64
? $table->string($name, $length)
: $table->longText($name),
default => $table->longText($name),
@ -132,7 +137,7 @@ class BusinessMirrorSchemaManager
$length = (int) ($column['max_length'] ?? 0);
return ! in_array($type, ['text', 'ntext', 'xml', 'image', 'binary', 'varbinary'], true)
&& (! in_array($type, ['char', 'nchar', 'varchar', 'nvarchar'], true) || ($length > 0 && $length <= 191));
&& (! in_array($type, ['char', 'nchar', 'varchar', 'nvarchar'], true) || ($length > 0 && $length <= 64));
}
return false;

View File

@ -168,6 +168,40 @@ class BusinessMirrorSynchronizerTest extends TestCase
$this->assertStringNotContainsString("\0", $rows[0]);
}
public function test_wide_source_schema_stores_only_short_strings_inline(): void
{
$datasets = (array) config('upstream_mirrors.datasets', []);
$datasets['test.dbo.wide_records'] = [
'source_key' => 'test',
'schema' => 'dbo',
'object' => 'wide_records',
'target' => 'test_wide_records',
'keys' => ['Id'],
'watermark' => 'updateTime',
];
config(['upstream_mirrors.datasets' => $datasets]);
$columns = [
$this->column('Id', 1, 'int', null),
$this->column('updateTime', 2, 'datetime2', null),
$this->column('shortCode', 3, 'nvarchar', 32),
$this->column('longLabel', 4, 'nvarchar', 120),
];
DB::table('upstream_schema_columns')->insert(array_map(
fn (array $column): array => [...$column, 'object_name' => 'wide_records'],
$columns,
));
$dataset = (new BusinessMirrorDatasetRegistry())->get('test.dbo.wide_records');
(new BusinessMirrorSchemaManager())->ensure($dataset);
$types = DB::connection('business_mirror')->select('PRAGMA table_info(test_wide_records)');
$types = collect($types)->pluck('type', 'name')->map(fn (string $type): string => strtolower($type));
$this->assertSame('varchar', $types['shortCode']);
$this->assertSame('text', $types['longLabel']);
}
public function test_empty_full_read_does_not_delete_previous_mirror_rows(): void
{
$this->query->batches = [[