fix(scheme): 改进MySQL表注释获取与对比的可靠性

This commit is contained in:
augushong
2026-02-01 00:14:45 +08:00
parent 90c0c090ca
commit 6aa176a39a
2 changed files with 48 additions and 19 deletions

View File

@@ -616,22 +616,36 @@ class SchemeToDbService
protected function diffTableComment(string $connection, string $tableName, string $schemeComment): ?string
{
$database = (string)Config::get('database.connections.' . $connection . '.database', '');
if ($database === '') {
$type = strtolower((string)Config::get('database.connections.' . $connection . '.type', ''));
if ($type !== 'mysql') {
return null;
}
$dbComment = '';
try {
$rows = Db::connect($connection)->query(
'SELECT table_comment FROM information_schema.TABLES WHERE table_schema = ? AND table_name = ? LIMIT 1',
[$database, $tableName]
);
$dbComment = isset($rows[0]['table_comment']) ? (string)$rows[0]['table_comment'] : '';
if ($dbComment !== (string)$schemeComment) {
return "表注释不一致DB={$dbComment} Scheme={$schemeComment}";
$escaped = addcslashes($tableName, "\\_%");
$rows = Db::connect($connection)->query("SHOW TABLE STATUS LIKE '$escaped'");
if (!empty($rows)) {
$dbComment = isset($rows[0]['Comment']) ? (string)$rows[0]['Comment'] : '';
}
} catch (\Throwable $e) {
return null;
}
if ($dbComment === '') {
try {
$rows = Db::connect($connection)->query("SHOW CREATE TABLE `$tableName`");
if (!empty($rows)) {
$create = $rows[0]['Create Table'] ?? '';
if ($create !== '' && preg_match("/COMMENT='(.*?)'/s", $create, $matches)) {
$dbComment = stripcslashes($matches[1]);
}
}
} catch (\Throwable $e) {
}
}
if ($dbComment !== (string)$schemeComment) {
return "表注释不一致DB={$dbComment} Scheme={$schemeComment}";
}
return null;