From 599a5f024dae48674f27290aad36f7933afb04ef Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Fri, 14 Aug 2026 21:02:24 +0800 Subject: [PATCH] fix: support wide upstream mirror tables --- .../Services/BusinessMirrorSchemaManager.php | 9 +++-- .../BusinessMirrorSynchronizerTest.php | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/modules/EcommerceFinance/Services/BusinessMirrorSchemaManager.php b/modules/EcommerceFinance/Services/BusinessMirrorSchemaManager.php index 5608d1fd..32b1aeb9 100644 --- a/modules/EcommerceFinance/Services/BusinessMirrorSchemaManager.php +++ b/modules/EcommerceFinance/Services/BusinessMirrorSchemaManager.php @@ -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; diff --git a/tests/Unit/EcommerceFinance/BusinessMirrorSynchronizerTest.php b/tests/Unit/EcommerceFinance/BusinessMirrorSynchronizerTest.php index a1205baa..4a206e8c 100644 --- a/tests/Unit/EcommerceFinance/BusinessMirrorSynchronizerTest.php +++ b/tests/Unit/EcommerceFinance/BusinessMirrorSynchronizerTest.php @@ -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 = [[ -- 2.47.2