From 6aa176a39ad5dea8ec308d5ce681ff29b3c91dcb Mon Sep 17 00:00:00 2001 From: augushong Date: Sun, 1 Feb 2026 00:14:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(scheme):=20=E6=94=B9=E8=BF=9BMySQL=E8=A1=A8?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E8=8E=B7=E5=8F=96=E4=B8=8E=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E7=9A=84=E5=8F=AF=E9=9D=A0=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/scheme/DbToSchemeService.php | 33 +++++++++++++----- .../service/scheme/SchemeToDbService.php | 34 +++++++++++++------ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/extend/base/common/service/scheme/DbToSchemeService.php b/extend/base/common/service/scheme/DbToSchemeService.php index 5b45be9..47b6a7a 100644 --- a/extend/base/common/service/scheme/DbToSchemeService.php +++ b/extend/base/common/service/scheme/DbToSchemeService.php @@ -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 diff --git a/extend/base/common/service/scheme/SchemeToDbService.php b/extend/base/common/service/scheme/SchemeToDbService.php index 6ee495f..5905a39 100644 --- a/extend/base/common/service/scheme/SchemeToDbService.php +++ b/extend/base/common/service/scheme/SchemeToDbService.php @@ -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;