You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Command/CloudMigrateV6.php

1252 lines
51 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Command;
use App\Models\User;
use App\Services\Config;
/**
* One-shot, cleanly removable v6 -> v7 production schema migration.
*
* MySQL DDL commits implicitly, so every step is guarded and the version is
* kept at an explicit in-progress marker until the complete v7 schema and all
* historical billing rows have been validated.
*/
final class CloudMigrateV6
{
private const OLD_MARKER = 'go-agent-wss-v6-migration-bundles:'
. '663c6c6da5f8cf4585e00e8764890bb3fee79d917cc9848b27fe1f981612a5ed';
private const NEW_MARKER = 'go-agent-wss-v7-automatic-backups:'
. '2d2c888c7bdce71fb189d75c4fe9fd8b7c7938ffdd2a922325c7e205f2a80dca';
private const IN_PROGRESS_MARKER = 'go-agent-wss-v6-to-v7-in-progress';
private const MIGRATION_FINGERPRINT =
'cloud_schema_migration_v6_fingerprint';
private const TRAFFIC_QUOTA_GIB = 1073741824;
private const MAX_TRAFFIC_QUOTA_BYTES = 1125899906842624;
private const OLD_MAIN_TABLES = [
'cloud_regions', 'cloud_network_lines', 'cloud_resource_pools',
'cloud_hosts', 'cloud_host_agents',
'cloud_host_storage_configurations', 'cloud_templates',
'cloud_template_replicas', 'cloud_user_isos',
'cloud_user_iso_replicas', 'cloud_rescue_template_deletions',
'cloud_rescue_template_imports', 'cloud_upload_staging',
'cloud_host_resource_pool_memberships',
'cloud_host_network_attachments', 'cloud_public_ip_pools',
'cloud_host_capacity_policies', 'cloud_public_ips', 'cloud_offers',
'cloud_instances', 'cloud_instance_firewalls',
'cloud_instance_firewall_versions', 'cloud_instance_firewall_rules',
'cloud_platform_test_policy_state',
'cloud_platform_test_policy_rules', 'cloud_instance_snapshots',
'cloud_instance_backups', 'cloud_backup_restores',
'cloud_instance_migrations', 'cloud_migration_bundle_artifacts',
'cloud_instance_runtime', 'cloud_orders',
'cloud_resource_reservations', 'cloud_service_periods',
'cloud_renewals', 'cloud_invoices', 'cloud_invoice_payments',
'cloud_mail_outbox', 'cloud_jobs', 'cloud_agent_completion_events',
'cloud_ssh_keys', 'cloud_sessions', 'cloud_console_tickets',
];
private const LOG_TABLES = [
'cloud_vm_metric_samples', 'cloud_vm_traffic_checkpoints',
'cloud_vm_traffic_totals',
];
private const V7_ONLY_TABLES = [
'cloud_backup_subscriptions', 'cloud_backup_recoveries',
'cloud_order_items', 'cloud_invoice_items',
];
private const V7_ONLY_COLUMNS = [
'cloud_offers' => ['backup_price'],
'cloud_instance_backups' => [
'source', 'subscription_id', 'scheduled_for',
],
'cloud_jobs' => ['backup_recovery_id'],
];
private const V7_ONLY_INDEXES = [
'cloud_instance_backups' => ['cloud_instance_backup_schedule'],
'cloud_jobs' => ['cloud_job_backup_recovery'],
];
/**
* Exact semantic shapes for tables introduced by v7.
*
* Column tuple: type, nullable, default, collation, extra.
* Index tuple: non-unique flag, ordered columns.
*/
private const V7_TABLE_SHAPES = [
'cloud_backup_subscriptions' => [
'columns' => [
'id' => ['bigint unsigned', 'NO', null, null,
'auto_increment'],
'instance_id' => ['bigint unsigned', 'NO', null, null, ''],
'user_id' => ['bigint unsigned', 'NO', null, null, ''],
'offer_id' => ['bigint unsigned', 'NO', null, null, ''],
'enabled' => ['tinyint(1)', 'NO', '1', null, ''],
'renew_next_period' => ['tinyint(1)', 'NO', '1', null, ''],
'frequency' => ['varchar(8)', 'NO', 'daily', 'ascii_bin', ''],
'weekday' => ['tinyint unsigned', 'NO', '0', null, ''],
'retention_count' => ['tinyint unsigned', 'NO', '2', null, ''],
'timezone' => ['varchar(64)', 'NO', 'America/Los_Angeles',
'ascii_bin', ''],
'price' => ['decimal(12,2)', 'NO', '0', null, ''],
'currency' => ['char(3)', 'NO', 'USD', 'ascii_bin', ''],
'price_version' => ['int unsigned', 'NO', null, null, ''],
'next_run_at' => ['bigint unsigned', 'NO', null, null, ''],
'last_scheduled_at' => ['bigint unsigned', 'YES', null,
null, ''],
'last_backup_id' => ['bigint unsigned', 'YES', null,
null, ''],
'created_at' => ['bigint unsigned', 'NO', null, null, ''],
'updated_at' => ['bigint unsigned', 'NO', null, null, ''],
],
'indexes' => [
'PRIMARY' => [0, ['id']],
'cloud_backup_subscription_instance' => [0, ['instance_id']],
'cloud_backup_subscription_due' => [1,
['enabled', 'next_run_at', 'id']],
'cloud_backup_subscription_owner' => [1, ['user_id', 'id']],
],
'checks' => [
'chk_cloud_backup_subscription_frequency' =>
"frequency IN ('daily','weekly')",
'chk_cloud_backup_subscription_weekday' => 'weekday <= 6',
'chk_cloud_backup_subscription_retention' =>
'retention_count IN (1,2)',
],
],
'cloud_backup_recoveries' => [
'columns' => [
'id' => ['bigint unsigned', 'NO', null, null,
'auto_increment'],
'recovery_key' => ['char(32)', 'NO', null, 'ascii_bin', ''],
'backup_id' => ['bigint unsigned', 'NO', null, null, ''],
'instance_id' => ['bigint unsigned', 'NO', null, null, ''],
'job_id' => ['bigint unsigned', 'YES', null, null, ''],
'operation_id' => ['char(32)', 'NO', null, 'ascii_bin', ''],
'state' => ['varchar(16)', 'NO', 'recovering',
'ascii_bin', ''],
'error_code' => ['varchar(64)', 'NO', '', 'ascii_bin', ''],
'completed_at' => ['bigint unsigned', 'YES', null, null, ''],
'created_at' => ['bigint unsigned', 'NO', null, null, ''],
'updated_at' => ['bigint unsigned', 'NO', null, null, ''],
],
'indexes' => [
'PRIMARY' => [0, ['id']],
'cloud_backup_recovery_key' => [0, ['recovery_key']],
'cloud_backup_recovery_operation' => [0, ['operation_id']],
'cloud_backup_recovery_job' => [0, ['job_id']],
'cloud_backup_recovery_instance' => [1,
['instance_id', 'state', 'id']],
'cloud_backup_recovery_backup' => [1,
['backup_id', 'state', 'id']],
],
'checks' => [],
],
'cloud_order_items' => [
'columns' => [
'id' => ['bigint unsigned', 'NO', null, null,
'auto_increment'],
'order_id' => ['bigint unsigned', 'NO', null, null, ''],
'code' => ['varchar(32)', 'NO', null, 'ascii_bin', ''],
'label' => ['varchar(128)', 'NO', null,
'utf8mb4_unicode_ci', ''],
'amount' => ['decimal(12,2)', 'NO', null, null, ''],
'currency' => ['char(3)', 'NO', 'USD', 'ascii_bin', ''],
'snapshot_json' => ['longtext', 'NO', null,
'utf8mb4_unicode_ci', ''],
'created_at' => ['bigint unsigned', 'NO', null, null, ''],
],
'indexes' => [
'PRIMARY' => [0, ['id']],
'cloud_order_item_code' => [0, ['order_id', 'code']],
'cloud_order_item_order' => [1, ['order_id', 'id']],
],
'checks' => [],
],
'cloud_invoice_items' => [
'columns' => [
'id' => ['bigint unsigned', 'NO', null, null,
'auto_increment'],
'invoice_id' => ['bigint unsigned', 'NO', null, null, ''],
'order_item_id' => ['bigint unsigned', 'YES', null, null, ''],
'code' => ['varchar(32)', 'NO', null, 'ascii_bin', ''],
'label' => ['varchar(128)', 'NO', null,
'utf8mb4_unicode_ci', ''],
'amount' => ['decimal(12,2)', 'NO', null, null, ''],
'currency' => ['char(3)', 'NO', 'USD', 'ascii_bin', ''],
'snapshot_json' => ['longtext', 'NO', null,
'utf8mb4_unicode_ci', ''],
'created_at' => ['bigint unsigned', 'NO', null, null, ''],
],
'indexes' => [
'PRIMARY' => [0, ['id']],
'cloud_invoice_item_code' => [0, ['invoice_id', 'code']],
'cloud_invoice_item_invoice' => [1, ['invoice_id', 'id']],
'cloud_invoice_item_order_item' => [1, ['order_item_id']],
],
'checks' => [],
],
];
/** Column tuple plus the immediately preceding column name. */
private const V7_ADDED_COLUMN_SHAPES = [
'cloud_offers' => [
'backup_price' => ['decimal(12,2)', 'NO', '0', null, '', 'price'],
],
'cloud_instance_backups' => [
'source' => ['varchar(16)', 'NO', 'manual', 'ascii_bin', '',
'purpose'],
'subscription_id' => ['bigint unsigned', 'YES', null, null, '',
'source'],
'scheduled_for' => ['bigint unsigned', 'YES', null, null, '',
'subscription_id'],
],
'cloud_jobs' => [
'backup_recovery_id' => ['bigint unsigned', 'YES', null, null,
'', 'backup_restore_id'],
],
];
private const V7_ADDED_INDEX_SHAPES = [
'cloud_instance_backups' => [
'cloud_instance_backup_schedule' => [0,
['subscription_id', 'scheduled_for']],
],
'cloud_jobs' => [
'cloud_job_backup_recovery' => [1,
['backup_recovery_id', 'id']],
],
];
public static function run(): bool
{
if (Config::get('enable_log', 'false') !== 'true') {
throw new \RuntimeException(
'请启用并配置日志数据库后再升级虚拟化结构'
);
}
if ((string) Config::get('db_prefix', '') !== '') {
throw new \RuntimeException(
'虚拟化集成使用独立表名,请将 db_prefix 留空'
);
}
$connection = (new User())->getConnection();
$logConnection = User::getConnectionResolver()->connection('log');
$mainLock = 'virt-cloud-main:' . substr(hash(
'sha256', (string) $connection->getDatabaseName()
), 0, 48);
$logLock = 'virt-cloud-log:' . substr(hash(
'sha256', (string) $logConnection->getDatabaseName()
), 0, 48);
$mainLocked = false;
$logLocked = false;
try {
$mainLocked = self::acquire($connection, $mainLock);
if (!$mainLocked) {
throw new \RuntimeException('无法取得云业务数据库升级锁');
}
$logLocked = self::acquire($logConnection, $logLock);
if (!$logLocked) {
throw new \RuntimeException('无法取得云日志数据库升级锁');
}
$marker = self::configuration(
$connection, 'cloud_schema_version'
);
$sourceMarker = CloudSetup::v6MigrationSchemaMarker();
if (!hash_equals(self::NEW_MARKER, $sourceMarker)) {
throw new \RuntimeException(
'当前源码结构标记不是预期的 v7,拒绝升级'
);
}
if (hash_equals(self::NEW_MARKER, $marker)) {
CloudSetup::assertPublishedV7Migration(
$connection, $logConnection
);
echo "云数据库已经是 v7,无需重复升级。\n";
return true;
}
self::prepareMigrationMarker($connection, $logConnection, $marker);
self::upgradeSchema($connection);
self::assertV7SchemaShape($connection);
[$specs, $orders, $invoices] = self::backfillBillingItems(
$connection
);
self::publishMigration($connection, $logConnection);
echo '云数据库已从 v6 升级到 v7;历史订单快照 '
. $specs . ' 条,历史订单明细 ' . $orders
. ' 条,历史账单明细 ' . $invoices . " 条。\n";
return true;
} finally {
if ($logLocked) {
$logConnection->selectOne(
'SELECT RELEASE_LOCK(?) AS released', [$logLock]
);
}
if ($mainLocked) {
$connection->selectOne(
'SELECT RELEASE_LOCK(?) AS released', [$mainLock]
);
}
}
}
private static function acquire($connection, string $name): bool
{
$row = $connection->selectOne(
'SELECT GET_LOCK(?, 30) AS acquired', [$name]
);
return (int) ($row->acquired ?? 0) === 1;
}
private static function prepareMigrationMarker($connection,
$logConnection, string $marker): void
{
$stored = self::configuration(
$connection, 'cloud_schema_fingerprint'
);
if (preg_match('/^[a-f0-9]{64}$/D', $stored) !== 1) {
throw new \RuntimeException('v6 数据库指纹缺失或格式无效');
}
if (hash_equals(self::OLD_MARKER, $marker)) {
$actual = self::oldFingerprint($connection, $logConnection);
if (!hash_equals($stored, $actual)) {
throw new \RuntimeException(
'v6 数据库实际结构与安装指纹不一致,拒绝升级'
);
}
self::assertV7ObjectsAbsent($connection);
if ($connection->table('configurations')->where(
'name', self::MIGRATION_FINGERPRINT
)->exists()) {
throw new \RuntimeException('v6 数据库含有意外的升级临时指纹');
}
$connection->transaction(static function () use (
$connection, $stored
): void {
$connection->table('configurations')->insert([
'name' => self::MIGRATION_FINGERPRINT,
'value' => $stored,
]);
$updated = $connection->table('configurations')->where(
'name', 'cloud_schema_version'
)->where('value', self::OLD_MARKER)->update([
'value' => self::IN_PROGRESS_MARKER,
]);
if ((int) $updated !== 1) {
throw new \RuntimeException('v6 数据库版本标记发生竞争');
}
});
return;
}
if (!hash_equals(self::IN_PROGRESS_MARKER, $marker)) {
throw new \RuntimeException(
'仅支持从精确的 v6 数据库升级到 v7'
);
}
$original = self::configuration(
$connection, self::MIGRATION_FINGERPRINT
);
if (preg_match('/^[a-f0-9]{64}$/D', $original) !== 1
|| !hash_equals($stored, $original)) {
throw new \RuntimeException(
'v6 升级断点缺少原始数据库指纹,拒绝继续'
);
}
}
private static function assertV7ObjectsAbsent($connection): void
{
$schema = $connection->getSchemaBuilder();
foreach (self::V7_ONLY_TABLES as $table) {
if ($schema->hasTable($table)) {
throw new \RuntimeException(
'v6 数据库包含迁移前不应存在的 v7 数据表:' . $table
);
}
}
foreach (self::V7_ONLY_COLUMNS as $table => $columns) {
foreach ($columns as $column) {
if ($schema->hasColumn($table, $column)) {
throw new \RuntimeException(
'v6 数据库包含迁移前不应存在的 v7 字段:'
. $table . '.' . $column
);
}
}
}
foreach (self::V7_ONLY_INDEXES as $table => $indexes) {
foreach ($indexes as $index) {
if (self::indexExists($connection, $table, $index)) {
throw new \RuntimeException(
'v6 数据库包含迁移前不应存在的 v7 索引:'
. $table . '.' . $index
);
}
}
}
}
private static function assertV7SchemaShape($connection): void
{
foreach (self::V7_TABLE_SHAPES as $table => $shape) {
self::assertTableShape($connection, $table, $shape);
}
foreach (self::V7_ADDED_COLUMN_SHAPES as $table => $columns) {
foreach ($columns as $column => $shape) {
$previous = array_pop($shape);
self::assertColumnShape(
$connection, $table, $column, $shape, $previous
);
}
}
foreach (self::V7_ADDED_INDEX_SHAPES as $table => $indexes) {
$actual = self::indexShapes($connection, $table);
foreach ($indexes as $name => $shape) {
if (!isset($actual[$name]) || $actual[$name] !== $shape) {
throw new \RuntimeException(
'v7 索引结构校验失败:' . $table . '.' . $name
);
}
}
}
self::assertTerminalShapeConstraint($connection);
}
private static function assertTableShape($connection, string $table,
array $shape): void
{
$database = (string) $connection->getDatabaseName();
$metadata = $connection->selectOne(
'SELECT ENGINE AS engine, TABLE_COLLATION AS table_collation '
. 'FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? '
. 'AND TABLE_NAME = ? LIMIT 1',
[$database, $table]
);
if ($metadata === null
|| strcasecmp((string) ($metadata->engine ?? ''), 'InnoDB') !== 0
|| strcasecmp(
(string) ($metadata->table_collation ?? ''),
'utf8mb4_unicode_ci'
) !== 0) {
throw new \RuntimeException('v7 数据表属性校验失败:' . $table);
}
$rows = $connection->select(
'SELECT ORDINAL_POSITION AS ordinal_position, '
. 'COLUMN_NAME AS column_name, COLUMN_TYPE AS column_type, '
. 'IS_NULLABLE AS is_nullable, COLUMN_DEFAULT AS column_default, '
. 'EXTRA AS extra, COLLATION_NAME AS collation_name FROM '
. 'information_schema.COLUMNS WHERE TABLE_SCHEMA = ? '
. 'AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION',
[$database, $table]
);
if (array_map(static fn ($row): string =>
(string) $row->column_name, $rows)
!== array_keys($shape['columns'])) {
throw new \RuntimeException('v7 数据表字段顺序校验失败:' . $table);
}
foreach ($rows as $offset => $row) {
$column = (string) $row->column_name;
self::assertColumnRow(
$table, $column, $row, $shape['columns'][$column]
);
if ((int) $row->ordinal_position !== $offset + 1) {
throw new \RuntimeException(
'v7 数据表字段序号校验失败:' . $table . '.' . $column
);
}
}
$indexes = self::indexShapes($connection, $table);
$expectedIndexes = $shape['indexes'];
ksort($indexes, SORT_STRING);
ksort($expectedIndexes, SORT_STRING);
if ($indexes !== $expectedIndexes) {
throw new \RuntimeException('v7 数据表索引校验失败:' . $table);
}
$checks = self::checkShapes($connection, $table);
$expectedChecks = array_map(
[self::class, 'normalizeCheckClause'],
$shape['checks']
);
ksort($checks, SORT_STRING);
ksort($expectedChecks, SORT_STRING);
if ($checks !== $expectedChecks) {
throw new \RuntimeException('v7 数据表约束校验失败:' . $table);
}
$foreign = $connection->selectOne(
'SELECT COUNT(*) AS aggregate FROM '
. 'information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = ? '
. "AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'FOREIGN KEY'",
[$database, $table]
);
if ((int) ($foreign->aggregate ?? -1) !== 0) {
throw new \RuntimeException('v7 数据表含有意外外键:' . $table);
}
}
private static function assertColumnShape($connection, string $table,
string $column, array $shape, string $previous): void
{
$database = (string) $connection->getDatabaseName();
$rows = $connection->select(
'SELECT ORDINAL_POSITION AS ordinal_position, '
. 'COLUMN_NAME AS column_name, COLUMN_TYPE AS column_type, '
. 'IS_NULLABLE AS is_nullable, COLUMN_DEFAULT AS column_default, '
. 'EXTRA AS extra, COLLATION_NAME AS collation_name FROM '
. 'information_schema.COLUMNS WHERE TABLE_SCHEMA = ? '
. 'AND TABLE_NAME = ? AND COLUMN_NAME IN (?, ?) '
. 'ORDER BY ORDINAL_POSITION',
[$database, $table, $previous, $column]
);
if (count($rows) !== 2
|| (string) $rows[0]->column_name !== $previous
|| (string) $rows[1]->column_name !== $column
|| (int) $rows[1]->ordinal_position
!== (int) $rows[0]->ordinal_position + 1) {
throw new \RuntimeException(
'v7 字段位置校验失败:' . $table . '.' . $column
);
}
self::assertColumnRow($table, $column, $rows[1], $shape);
}
private static function assertColumnRow(string $table, string $column,
$row, array $shape): void
{
[$type, $nullable, $default, $collation, $extra] = $shape;
$actual = [
self::normalizeColumnType((string) $row->column_type),
strtoupper((string) $row->is_nullable),
self::normalizeColumnDefault($row->column_default),
$row->collation_name === null
? null : strtolower((string) $row->collation_name),
self::normalizeColumnExtra((string) $row->extra),
];
$expected = [
self::normalizeColumnType($type), strtoupper($nullable),
self::normalizeColumnDefault($default),
$collation === null ? null : strtolower($collation),
self::normalizeColumnExtra($extra),
];
if ($actual !== $expected) {
throw new \RuntimeException(
'v7 字段结构校验失败:' . $table . '.' . $column
);
}
}
private static function normalizeColumnType(string $type): string
{
$type = strtolower((string) preg_replace('/\s+/', ' ', trim($type)));
return (string) preg_replace_callback(
'/\b(tinyint|smallint|mediumint|int|bigint)\((\d+)\)/',
static function (array $match): string {
return $match[1] === 'tinyint' && $match[2] === '1'
? 'tinyint(1)' : $match[1];
},
$type
);
}
private static function normalizeColumnDefault($value): ?string
{
if ($value === null) {
return null;
}
$value = trim((string) $value);
if (strlen($value) >= 2
&& (($value[0] === "'" && substr($value, -1) === "'")
|| ($value[0] === '"' && substr($value, -1) === '"'))) {
$value = substr($value, 1, -1);
}
if (strcasecmp($value, 'NULL') === 0) {
return null;
}
if (preg_match('/^[+-]?[0-9]+(?:\.[0-9]+)?$/D', $value) === 1) {
$negative = str_starts_with($value, '-');
$value = ltrim($value, '+-');
[$integer, $fraction] = array_pad(explode('.', $value, 2), 2, '');
$integer = ltrim($integer, '0');
$integer = $integer === '' ? '0' : $integer;
$fraction = rtrim($fraction, '0');
$value = $integer . ($fraction === '' ? '' : '.' . $fraction);
return $negative && $value !== '0' ? '-' . $value : $value;
}
return $value;
}
private static function normalizeColumnExtra(string $extra): string
{
$extra = strtolower($extra);
$extra = (string) preg_replace(
'/\bdefault_generated\b/', '', $extra
);
return trim((string) preg_replace('/\s+/', ' ', $extra));
}
private static function indexShapes($connection, string $table): array
{
$rows = $connection->select(
'SELECT INDEX_NAME AS index_name, NON_UNIQUE AS non_unique, '
. 'SEQ_IN_INDEX AS seq_in_index, COLUMN_NAME AS column_name, '
. 'SUB_PART AS sub_part, INDEX_TYPE AS index_type FROM '
. 'information_schema.STATISTICS WHERE TABLE_SCHEMA = ? '
. 'AND TABLE_NAME = ? ORDER BY INDEX_NAME, SEQ_IN_INDEX',
[(string) $connection->getDatabaseName(), $table]
);
$indexes = [];
foreach ($rows as $row) {
$name = (string) $row->index_name;
if ($row->sub_part !== null
|| strcasecmp((string) $row->index_type, 'BTREE') !== 0) {
throw new \RuntimeException(
'v7 索引类型校验失败:' . $table . '.' . $name
);
}
$indexes[$name] ??= [(int) $row->non_unique, []];
if ($indexes[$name][0] !== (int) $row->non_unique
|| (int) $row->seq_in_index
!== count($indexes[$name][1]) + 1) {
throw new \RuntimeException(
'v7 索引顺序校验失败:' . $table . '.' . $name
);
}
$indexes[$name][1][] = (string) $row->column_name;
}
return $indexes;
}
private static function checkShapes($connection, string $table): array
{
$rows = $connection->select(
'SELECT tc.CONSTRAINT_NAME AS constraint_name, '
. 'cc.CHECK_CLAUSE AS check_clause FROM '
. 'information_schema.TABLE_CONSTRAINTS tc JOIN '
. 'information_schema.CHECK_CONSTRAINTS cc ON '
. 'cc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA AND '
. 'cc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME WHERE '
. 'tc.TABLE_SCHEMA = ? AND tc.TABLE_NAME = ? '
. "AND tc.CONSTRAINT_TYPE = 'CHECK' ORDER BY tc.CONSTRAINT_NAME",
[(string) $connection->getDatabaseName(), $table]
);
$checks = [];
foreach ($rows as $row) {
$checks[(string) $row->constraint_name] =
self::normalizeCheckClause((string) $row->check_clause);
}
return $checks;
}
private static function normalizeCheckClause(string $clause): string
{
$clause = strtolower(str_replace('`', '', $clause));
$clause = (string) preg_replace(
'/_(?:utf8mb4|utf8|ascii)(?=[\'\"])/', '', $clause
);
$clause = (string) preg_replace('/\s+/', '', $clause);
while (strlen($clause) >= 2 && $clause[0] === '('
&& substr($clause, -1) === ')'
&& self::outerParenthesesWrap($clause)) {
$clause = substr($clause, 1, -1);
}
return $clause;
}
private static function outerParenthesesWrap(string $value): bool
{
$depth = 0;
$length = strlen($value);
for ($index = 0; $index < $length; $index++) {
if ($value[$index] === '(') {
$depth++;
} elseif ($value[$index] === ')') {
$depth--;
if ($depth === 0 && $index !== $length - 1) {
return false;
}
}
if ($depth < 0) {
return false;
}
}
return $depth === 0;
}
private static function assertTerminalShapeConstraint($connection): void
{
$checks = self::checkShapes($connection, 'cloud_jobs');
$clause = $checks['chk_cloud_job_terminal_shape'] ?? '';
// MySQL and MariaDB serialize equivalent boolean parentheses
// differently. Verify every state-machine predicate and its cardinality
// while keeping the explicit cross-collation comparison mandatory.
$required = [
"phase='terminal'",
'outcomeisnotnull',
'statuscollateutf8mb4_unicode_ci='
. 'convert(outcomeusingutf8mb4)collateutf8mb4_unicode_ci',
'terminal_atisnotnull',
'effect_may_have_appliedisnotnull',
'safe_to_resubmitisnotnull',
"outcome='succeeded'",
"outcome='failed'",
"phase<>'terminal'",
'outcomeisnull',
'terminal_atisnull',
'effect_may_have_appliedisnull',
'safe_to_resubmitisnull',
"statusin('queued','running')",
"effect_resolution='not_applicable'",
];
foreach ($required as $fragment) {
if (substr_count($clause, $fragment) !== 1) {
throw new \RuntimeException(
'v7 任务终态约束校验失败:chk_cloud_job_terminal_shape'
);
}
}
}
private static function configuration($connection, string $name): string
{
$row = $connection->table('configurations')
->where('name', $name)->first(['value']);
return $row === null ? '' : (string) $row->value;
}
private static function publishMigration($connection,
$logConnection): void
{
if (!hash_equals(
self::IN_PROGRESS_MARKER,
self::configuration($connection, 'cloud_schema_version')
)) {
throw new \RuntimeException('v6 升级不在可发布的断点状态');
}
$stored = self::configuration(
$connection, 'cloud_schema_fingerprint'
);
$original = self::configuration(
$connection, self::MIGRATION_FINGERPRINT
);
if (preg_match('/^[a-f0-9]{64}$/D', $stored) !== 1
|| preg_match('/^[a-f0-9]{64}$/D', $original) !== 1
|| !hash_equals($stored, $original)) {
throw new \RuntimeException('v6 升级原始数据库指纹已变化');
}
if (!hash_equals(
self::NEW_MARKER,
CloudSetup::v6MigrationSchemaMarker()
)) {
throw new \RuntimeException('待发布源码结构标记不是预期的 v7');
}
self::assertV7SchemaShape($connection);
self::assertBillingBackfill($connection);
$fingerprint = CloudSetup::v6MigrationInstalledFingerprint(
$connection, $logConnection
);
if (preg_match('/^[a-f0-9]{64}$/D', $fingerprint) !== 1) {
throw new \RuntimeException('无法生成完整的 v7 数据库指纹');
}
$connection->transaction(static function () use (
$connection, $original, $fingerprint
): void {
$fingerprintUpdated = $connection->table('configurations')
->where('name', 'cloud_schema_fingerprint')
->where('value', $original)
->update(['value' => $fingerprint]);
if ((int) $fingerprintUpdated !== 1) {
throw new \RuntimeException('v7 数据库指纹发布竞争');
}
$temporaryDeleted = $connection->table('configurations')
->where('name', self::MIGRATION_FINGERPRINT)
->where('value', $original)->delete();
if ((int) $temporaryDeleted !== 1) {
throw new \RuntimeException('v6 升级临时指纹删除竞争');
}
// Publish the version marker last. HTTP and workers must never
// observe v7 before the exact installed fingerprint is committed.
$versionUpdated = $connection->table('configurations')
->where('name', 'cloud_schema_version')
->where('value', self::IN_PROGRESS_MARKER)
->update(['value' => self::NEW_MARKER]);
if ((int) $versionUpdated !== 1) {
throw new \RuntimeException('v7 数据库版本发布竞争');
}
});
}
private static function upgradeSchema($connection): void
{
self::addColumn($connection, 'cloud_offers', 'backup_price',
'DECIMAL(12,2) NOT NULL DEFAULT 0.00 AFTER `price`');
self::createCurrentTable($connection, 'cloud_backup_subscriptions');
self::addColumn($connection, 'cloud_instance_backups', 'source',
"VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL "
. "DEFAULT 'manual' AFTER `purpose`");
self::addColumn(
$connection, 'cloud_instance_backups', 'subscription_id',
'BIGINT UNSIGNED DEFAULT NULL AFTER `source`'
);
self::addColumn(
$connection, 'cloud_instance_backups', 'scheduled_for',
'BIGINT UNSIGNED DEFAULT NULL AFTER `subscription_id`'
);
self::addIndex(
$connection, 'cloud_instance_backups',
'cloud_instance_backup_schedule',
'UNIQUE KEY `cloud_instance_backup_schedule` '
. '(`subscription_id`, `scheduled_for`)'
);
self::createCurrentTable($connection, 'cloud_backup_recoveries');
self::createCurrentTable($connection, 'cloud_order_items');
self::createCurrentTable($connection, 'cloud_invoice_items');
self::upgradeJobs($connection);
}
private static function upgradeJobs($connection): void
{
// Some MySQL/MariaDB versions re-evaluate this cross-collation CHECK
// during ALTER TABLE. Recreate it with an explicit UTF-8 comparison so
// the migration is deterministic on both engines.
if (self::constraintExists(
$connection, 'cloud_jobs', 'chk_cloud_job_terminal_shape'
)) {
$connection->statement(
'ALTER TABLE `cloud_jobs` DROP CONSTRAINT '
. '`chk_cloud_job_terminal_shape`'
);
}
self::addColumn(
$connection, 'cloud_jobs', 'backup_recovery_id',
'BIGINT UNSIGNED DEFAULT NULL AFTER `backup_restore_id`'
);
self::addIndex(
$connection, 'cloud_jobs', 'cloud_job_backup_recovery',
'KEY `cloud_job_backup_recovery` (`backup_recovery_id`, `id`)'
);
if (!self::constraintExists(
$connection, 'cloud_jobs', 'chk_cloud_job_terminal_shape'
)) {
$connection->statement(self::terminalShapeConstraintSql());
}
}
private static function terminalShapeConstraintSql(): string
{
return <<<'SQL'
ALTER TABLE `cloud_jobs` ADD CONSTRAINT `chk_cloud_job_terminal_shape` CHECK (
(
`phase` = 'terminal' AND `outcome` IS NOT NULL
AND (`status` COLLATE utf8mb4_unicode_ci) =
(CONVERT(`outcome` USING utf8mb4) COLLATE utf8mb4_unicode_ci)
AND `terminal_at` IS NOT NULL
AND `effect_may_have_applied` IS NOT NULL
AND `safe_to_resubmit` IS NOT NULL
AND (
(`outcome` = 'succeeded' AND `effect_may_have_applied` = 0
AND `safe_to_resubmit` = 0 AND `effect_resolution` = 'applied'
AND `error_code` IS NULL)
OR
(`outcome` = 'failed' AND (
(`effect_may_have_applied` = 1 AND `safe_to_resubmit` = 0
AND `effect_resolution` IN ('unknown','applied','not_applied'))
OR
(`effect_may_have_applied` = 0
AND `effect_resolution` = 'not_applied')
))
)
)
OR
(
`phase` <> 'terminal' AND `outcome` IS NULL
AND `terminal_at` IS NULL AND `effect_may_have_applied` IS NULL
AND `safe_to_resubmit` IS NULL AND `status` IN ('queued','running')
AND `effect_resolution` = 'not_applicable'
)
)
SQL;
}
private static function addColumn($connection, string $table,
string $column, string $definition): void
{
if (!$connection->getSchemaBuilder()->hasColumn($table, $column)) {
$connection->statement(
'ALTER TABLE `' . $table . '` ADD COLUMN `'
. $column . '` ' . $definition
);
}
}
private static function addIndex($connection, string $table,
string $index, string $definition): void
{
if (!self::indexExists($connection, $table, $index)) {
$connection->statement(
'ALTER TABLE `' . $table . '` ADD ' . $definition
);
}
}
private static function indexExists($connection, string $table,
string $index): bool
{
$row = $connection->selectOne(
'SELECT 1 AS present FROM information_schema.STATISTICS '
. 'WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ? '
. 'LIMIT 1',
[(string) $connection->getDatabaseName(), $table, $index]
);
return $row !== null;
}
private static function constraintExists($connection, string $table,
string $constraint): bool
{
$row = $connection->selectOne(
'SELECT 1 AS present FROM information_schema.TABLE_CONSTRAINTS '
. 'WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? '
. 'AND CONSTRAINT_NAME = ? LIMIT 1',
[(string) $connection->getDatabaseName(), $table, $constraint]
);
return $row !== null;
}
private static function createCurrentTable($connection,
string $table): void
{
if ($connection->getSchemaBuilder()->hasTable($table)) {
return;
}
$allowed = [
'cloud_backup_subscriptions', 'cloud_backup_recoveries',
'cloud_order_items', 'cloud_invoice_items',
];
if (!in_array($table, $allowed, true)) {
throw new \LogicException('不允许迁移该数据表');
}
$source = file_get_contents(BASE_PATH . '/sql/cloud.sql');
if ($source === false) {
throw new \RuntimeException('缺少当前 cloud.sql');
}
$pattern = '/(?:^|\n)(CREATE TABLE '
. preg_quote($table, '/')
. ' \(\n.*?\n\) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 '
. 'COLLATE=utf8mb4_unicode_ci;)/s';
if (preg_match($pattern, $source, $match) !== 1) {
throw new \RuntimeException('无法读取当前建表定义:' . $table);
}
$connection->unprepared($match[1]);
}
/** @return array{0:int,1:int,2:int} */
private static function backfillBillingItems($connection): array
{
$specs = self::backfillOrderSpecs($connection);
$orders = self::backfillOrders($connection);
$invoices = self::backfillInvoices($connection);
return [$specs, $orders, $invoices];
}
private static function backfillOrderSpecs($connection): int
{
$last = 0;
$updated = 0;
do {
$rows = $connection->table('cloud_orders')
->where('id', '>', $last)->orderBy('id')->limit(500)
->get(['id', 'amount', 'spec_json'])->all();
foreach ($rows as $row) {
$last = (int) $row->id;
$original = (string) $row->spec_json;
$encoded = self::migrateOrderSpec(
$original, (string) $row->amount, $last
);
if (hash_equals($original, $encoded)) {
continue;
}
$affected = $connection->table('cloud_orders')
->where('id', $last)->where('spec_json', $original)
->update(['spec_json' => $encoded]);
if ((int) $affected !== 1) {
throw new \RuntimeException(
'历史订单快照在升级期间发生变化:#' . $last
);
}
$updated++;
}
} while (count($rows) === 500);
return $updated;
}
private static function migrateOrderSpec(string $original,
string $amount, int $orderId): string
{
$trimmed = trim($original);
if ($trimmed === '' || $trimmed[0] !== '{'
|| substr($trimmed, -1) !== '}') {
throw new \RuntimeException(
'历史订单快照必须是 JSON 对象:#' . $orderId
);
}
try {
$spec = json_decode(
$trimmed, true, 512, JSON_THROW_ON_ERROR
);
} catch (\JsonException $exception) {
throw new \RuntimeException(
'历史订单快照不是有效 JSON:#' . $orderId,
0,
$exception
);
}
if (!is_array($spec)) {
throw new \RuntimeException(
'历史订单快照必须是 JSON 对象:#' . $orderId
);
}
if (!self::validMoney($amount)
|| !self::validMoney($spec['renewal_price'] ?? null)
|| !is_int($spec['validity_days'] ?? null)
|| $spec['validity_days'] < 1
|| $spec['validity_days'] > 3650
|| !is_int($spec['traffic_quota_bytes'] ?? null)
|| $spec['traffic_quota_bytes'] < 0
|| $spec['traffic_quota_bytes']
> self::MAX_TRAFFIC_QUOTA_BYTES
|| ($spec['traffic_quota_bytes'] !== 0
&& $spec['traffic_quota_bytes']
% self::TRAFFIC_QUOTA_GIB !== 0)) {
throw new \RuntimeException(
'历史订单计费快照无效:#' . $orderId
);
}
// v6 orders only priced the base instance. Keep every old immutable
// field (including renewal_price), and add the v7 optional-service
// split needed by the success finalizer.
$spec['instance_price'] = $amount;
$spec['automatic_backup'] = false;
$spec['backup_price'] = '0.00';
return json_encode(
$spec,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
);
}
private static function validMoney($value): bool
{
return (is_string($value) || is_int($value))
&& preg_match(
'/^(0|[1-9][0-9]{0,9})(?:\.[0-9]{1,2})?$/D',
(string) $value
) === 1;
}
private static function backfillOrders($connection): int
{
$last = 0;
$inserted = 0;
do {
$rows = $connection->table('cloud_orders')
->where('id', '>', $last)->orderBy('id')->limit(500)
->get([
'id', 'plan_id', 'offer_id', 'amount', 'currency',
'created_at',
])->all();
$values = [];
foreach ($rows as $row) {
$last = (int) $row->id;
$values[] = [
'order_id' => $last,
'code' => 'instance',
'label' => '实例套餐',
'amount' => (string) $row->amount,
'currency' => strtoupper((string) $row->currency),
'snapshot_json' => json_encode([
'migrated_from' => 'v6',
'plan_id' => (int) $row->plan_id,
'offer_id' => $row->offer_id === null
? null : (int) $row->offer_id,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES),
'created_at' => max(0, (int) $row->created_at),
];
}
if ($values !== []) {
$inserted += (int) $connection->table(
'cloud_order_items'
)->insertOrIgnore($values);
}
} while (count($rows) === 500);
return $inserted;
}
private static function backfillInvoices($connection): int
{
$last = 0;
$inserted = 0;
do {
$rows = $connection->table('cloud_invoices as invoice')
->leftJoin('cloud_order_items as order_item',
static function ($join): void {
$join->on('order_item.order_id', '=',
'invoice.order_id')
->where('order_item.code', '=', 'instance');
})
->where('invoice.id', '>', $last)
->orderBy('invoice.id')->limit(500)->get([
'invoice.id', 'invoice.purpose', 'invoice.instance_id',
'invoice.order_id', 'invoice.renewal_id',
'invoice.plan_id', 'invoice.amount', 'invoice.currency',
'invoice.created_at',
'order_item.id as order_item_id',
])->all();
$values = [];
foreach ($rows as $row) {
$last = (int) $row->id;
$purpose = (string) ($row->purpose ?: 'renewal');
$values[] = [
'invoice_id' => $last,
'order_item_id' => $purpose === 'provision'
&& $row->order_item_id !== null
? (int) $row->order_item_id : null,
'code' => 'instance',
'label' => self::invoiceLabel($purpose),
'amount' => (string) $row->amount,
'currency' => strtoupper((string) $row->currency),
'snapshot_json' => json_encode([
'migrated_from' => 'v6',
'purpose' => $purpose,
'instance_id' => $row->instance_id === null
? null : (int) $row->instance_id,
'order_id' => $row->order_id === null
? null : (int) $row->order_id,
'renewal_id' => $row->renewal_id === null
? null : (int) $row->renewal_id,
'plan_id' => $row->plan_id === null
? null : (int) $row->plan_id,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES),
'created_at' => max(0, (int) $row->created_at),
];
}
if ($values !== []) {
$inserted += (int) $connection->table(
'cloud_invoice_items'
)->insertOrIgnore($values);
}
} while (count($rows) === 500);
return $inserted;
}
private static function invoiceLabel(string $purpose): string
{
return match ($purpose) {
'provision' => '实例套餐',
'renewal' => '实例套餐续费',
'upgrade' => '实例升级',
'ip_change' => '更换 IP',
default => '实例费用',
};
}
private static function assertBillingBackfill($connection): void
{
self::assertOrderSpecs($connection);
$badOrder = $connection->selectOne(
"SELECT orders.id FROM cloud_orders orders LEFT JOIN "
. "cloud_order_items item ON item.order_id = orders.id "
. "GROUP BY orders.id, orders.amount, orders.currency HAVING "
. "COUNT(item.id) <> 1 OR "
. "SUM(item.code = 'instance') <> 1 OR "
. "MAX(item.amount) <> orders.amount OR "
. "MAX(item.currency) <> orders.currency LIMIT 1"
);
if ($badOrder !== null) {
throw new \RuntimeException(
'历史订单费用明细回填校验失败:#' . (int) $badOrder->id
);
}
$badInvoice = $connection->selectOne(
"SELECT invoice.id FROM cloud_invoices invoice LEFT JOIN "
. "cloud_invoice_items item ON item.invoice_id = invoice.id "
. "LEFT JOIN cloud_order_items order_item ON "
. "order_item.order_id = invoice.order_id "
. "AND order_item.code = 'instance' "
. "GROUP BY invoice.id, invoice.amount, invoice.currency, "
. "invoice.purpose, order_item.id HAVING COUNT(item.id) <> 1 "
. "OR SUM(item.code = 'instance') <> 1 "
. "OR MAX(item.amount) <> invoice.amount "
. "OR MAX(item.currency) <> invoice.currency "
. "OR (invoice.purpose = 'provision' AND (order_item.id IS NULL "
. "OR MAX(item.order_item_id) IS NULL "
. "OR MAX(item.order_item_id) <> order_item.id)) "
. "OR (invoice.purpose <> 'provision' "
. "AND MAX(item.order_item_id) IS NOT NULL) LIMIT 1"
);
if ($badInvoice !== null) {
throw new \RuntimeException(
'历史账单费用明细回填校验失败:#'
. (int) $badInvoice->id
);
}
}
private static function assertOrderSpecs($connection): void
{
$last = 0;
do {
$rows = $connection->table('cloud_orders')
->where('id', '>', $last)->orderBy('id')->limit(500)
->get(['id', 'amount', 'spec_json'])->all();
foreach ($rows as $row) {
$last = (int) $row->id;
try {
$spec = json_decode(
(string) $row->spec_json,
true,
512,
JSON_THROW_ON_ERROR
);
} catch (\JsonException $exception) {
throw new \RuntimeException(
'历史订单快照回填校验失败:#' . $last,
0,
$exception
);
}
if (!is_array($spec)
|| ($spec['instance_price'] ?? null)
!== (string) $row->amount
|| ($spec['automatic_backup'] ?? null) !== false
|| ($spec['backup_price'] ?? null) !== '0.00'
|| !self::validMoney($spec['renewal_price'] ?? null)
|| !is_int($spec['validity_days'] ?? null)
|| $spec['validity_days'] < 1
|| $spec['validity_days'] > 3650
|| !is_int($spec['traffic_quota_bytes'] ?? null)
|| $spec['traffic_quota_bytes'] < 0
|| $spec['traffic_quota_bytes']
> self::MAX_TRAFFIC_QUOTA_BYTES
|| ($spec['traffic_quota_bytes'] !== 0
&& $spec['traffic_quota_bytes']
% self::TRAFFIC_QUOTA_GIB !== 0)) {
throw new \RuntimeException(
'历史订单计费快照回填校验失败:#' . $last
);
}
}
} while (count($rows) === 500);
}
private static function oldFingerprint($connection,
$logConnection): string
{
return hash('sha256', json_encode([
'main' => CloudSetup::v6MigrationSchemaObjects(
$connection, self::OLD_MAIN_TABLES
),
'log' => CloudSetup::v6MigrationSchemaObjects(
$logConnection, self::LOG_TABLES
),
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE));
}
}