getConnection(); $schema = $connection->getSchemaBuilder(); if (!$schema->hasTable('user') || !$schema->hasTable('shop')) { throw new \RuntimeException('请先在全新业务数据库导入 web/sql/ss.sql'); } $logConnection = User::getConnectionResolver()->connection('log'); self::assertFreshSchemaBoundary($connection, $logConnection); $connection->unprepared(self::schema('cloud.sql')); self::ensureMultiHostDefaults($connection); $logConnection->unprepared(self::schema('cloud_log.sql')); echo "云业务表与 Agent 指标日志表已准备。未连接或修改任何宿主机。\n"; return true; } /** Seed only immutable/default control rows in an otherwise fresh schema. */ private static function ensureMultiHostDefaults($connection): void { $connection->table('configurations')->insertOrIgnore([ 'name' => 'cloud_network_lines_enabled', 'value' => 'false', ]); } /** Refuse to mix the Go/Agent schema with any unmarked cloud database. */ private static function assertFreshSchemaBoundary($connection, $logConnection): void { $marker = $connection->table('configurations') ->where('name', 'cloud_schema_version')->first(['value']); if ($marker !== null) { if (!hash_equals(self::SCHEMA_VERSION, (string) $marker->value)) { throw new \RuntimeException( '虚拟化数据库版本不兼容,请使用全新数据库' ); } foreach (self::RETIRED_LOG_TABLES as $retiredTable) { if ($logConnection->getSchemaBuilder()->hasTable($retiredTable)) { throw new \RuntimeException( '日志库包含已废弃的 Python Engine 表,请使用全新日志库' ); } } return; } foreach (self::MAIN_TABLES as $table) { if ($connection->getSchemaBuilder()->hasTable($table)) { throw new \RuntimeException( '检测到未标记的旧虚拟化数据库,不支持原地升级' ); } } foreach (array_merge(self::LOG_TABLES, self::RETIRED_LOG_TABLES) as $table) { if ($logConnection->getSchemaBuilder()->hasTable($table)) { throw new \RuntimeException( '检测到未标记的虚拟化日志库,不支持原地升级' ); } } $connection->table('configurations')->insert([ 'name' => 'cloud_schema_version', 'value' => self::SCHEMA_VERSION, ]); } private static function schema(string $name): string { $sql = file_get_contents(BASE_PATH . '/sql/' . $name); if ($sql === false || trim($sql) === '') { throw new \RuntimeException('缺少虚拟化建表脚本:' . $name); } return $sql; } }