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

@@ -57,17 +57,32 @@ class DbToSchemeService
protected function getTableComment($tableName): string
{
// 简单适配 MySQL
try {
$config = Config::get('database.connections.' . $this->connection);
$database = $config['database'];
// 暂时只支持MySQL的表注释获取其他返回空
$sql = "SELECT table_comment FROM information_schema.TABLES WHERE table_schema = ? AND table_name = ?";
$res = Db::connect($this->connection)->query($sql, [$database, $tableName]);
return $res[0]['table_comment'] ?? '';
} catch (\Exception $e) {
$type = strtolower((string)Config::get('database.connections.' . $this->connection . '.type', ''));
if ($type !== 'mysql') {
return '';
}
try {
$escaped = addcslashes($tableName, "\\_%");
$rows = Db::connect($this->connection)->query("SHOW TABLE STATUS LIKE '$escaped'");
if (!empty($rows)) {
return isset($rows[0]['Comment']) ? (string)$rows[0]['Comment'] : '';
}
} catch (\Exception $e) {
}
try {
$rows = Db::connect($this->connection)->query("SHOW CREATE TABLE `$tableName`");
if (!empty($rows)) {
$create = $rows[0]['Create Table'] ?? '';
if ($create !== '' && preg_match("/COMMENT='(.*?)'/s", $create, $matches)) {
return stripcslashes($matches[1]);
}
}
} catch (\Exception $e) {
}
return '';
}
protected function getTableIndices($tableName): array