chore(schema): add one-use template v16 upgrade
parent
f1d9e46421
commit
5d280c1a58
@ -0,0 +1,407 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
/** One-use in-place upgrade for the v16 immutable template catalog. */
|
||||
final class CloudTemplateV16
|
||||
{
|
||||
public static function run(array $argv): int
|
||||
{
|
||||
$apply = in_array('--apply', $argv, true);
|
||||
$connection = (new User())->getConnection();
|
||||
$schema = $connection->getSchemaBuilder();
|
||||
if (!$schema->hasTable('cloud_templates')
|
||||
|| !$schema->hasTable('cloud_template_replicas')) {
|
||||
throw new \RuntimeException('云模板基础表不存在,拒绝增量升级');
|
||||
}
|
||||
|
||||
self::report($connection);
|
||||
if (!$apply) {
|
||||
fwrite(STDOUT, "只读检查完成;确认后使用 --apply。\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
$lockName = 'virt-template-v16:' . substr(hash(
|
||||
'sha256',
|
||||
(string) $connection->getDatabaseName()
|
||||
), 0, 44);
|
||||
$locked = $connection->selectOne(
|
||||
'SELECT GET_LOCK(?, 30) AS acquired',
|
||||
[$lockName]
|
||||
);
|
||||
if ((int) ($locked->acquired ?? 0) !== 1) {
|
||||
throw new \RuntimeException('无法取得云模板升级锁');
|
||||
}
|
||||
try {
|
||||
self::assertIdle($connection);
|
||||
self::upgradeTemplates($connection);
|
||||
self::createArtifacts($connection);
|
||||
self::upgradeReplicas($connection);
|
||||
self::retireLegacySourceColumns($connection);
|
||||
self::publishSchemaMarker($connection);
|
||||
} finally {
|
||||
$connection->selectOne(
|
||||
'SELECT RELEASE_LOCK(?) AS released',
|
||||
[$lockName]
|
||||
);
|
||||
}
|
||||
|
||||
CloudSetup::initialize();
|
||||
self::report($connection);
|
||||
fwrite(STDOUT, "cloudTemplateV16 增量升级完成。\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static function report($connection): void
|
||||
{
|
||||
$schema = $connection->getSchemaBuilder();
|
||||
$jobs = self::activeJobCounts($connection);
|
||||
$templates = (int) $connection->table('cloud_templates')->count();
|
||||
$replicas = (int) $connection->table(
|
||||
'cloud_template_replicas'
|
||||
)->count();
|
||||
$artifacts = $schema->hasTable('cloud_template_artifacts')
|
||||
? (int) $connection->table('cloud_template_artifacts')->count()
|
||||
: -1;
|
||||
fwrite(STDOUT, sprintf(
|
||||
"templates=%d replicas=%d artifacts=%s dispatching=%d "
|
||||
. "awaiting_receipt=%d open_template_jobs=%d old_source_type=%s "
|
||||
. "old_sealed_source_url=%s\n",
|
||||
$templates,
|
||||
$replicas,
|
||||
$artifacts < 0 ? 'missing' : (string) $artifacts,
|
||||
$jobs['dispatching'],
|
||||
$jobs['awaiting_receipt'],
|
||||
$jobs['open_template_jobs'],
|
||||
$schema->hasColumn('cloud_templates', 'source_type') ? 'yes' : 'no',
|
||||
$schema->hasColumn('cloud_templates', 'sealed_source_url')
|
||||
? 'yes' : 'no'
|
||||
));
|
||||
}
|
||||
|
||||
/** @return array{dispatching:int,awaiting_receipt:int,open_template_jobs:int} */
|
||||
private static function activeJobCounts($connection): array
|
||||
{
|
||||
$dispatching = (int) $connection->table('cloud_jobs')
|
||||
->where('phase', 'dispatching')->count();
|
||||
$awaiting = (int) $connection->table('cloud_jobs')
|
||||
->where('phase', 'awaiting_receipt')->count();
|
||||
$openTemplates = (int) $connection->table('cloud_jobs')
|
||||
->where('phase', '<>', 'terminal')
|
||||
->whereIn('kind', [
|
||||
'template_import',
|
||||
'template_import_replica',
|
||||
'template_metadata_sync',
|
||||
'template_delete',
|
||||
'template_delete_replica',
|
||||
])->count();
|
||||
return [
|
||||
'dispatching' => $dispatching,
|
||||
'awaiting_receipt' => $awaiting,
|
||||
'open_template_jobs' => $openTemplates,
|
||||
];
|
||||
}
|
||||
|
||||
private static function assertIdle($connection): void
|
||||
{
|
||||
$counts = self::activeJobCounts($connection);
|
||||
if ($counts['dispatching'] !== 0
|
||||
|| $counts['awaiting_receipt'] !== 0
|
||||
|| $counts['open_template_jobs'] !== 0) {
|
||||
throw new \RuntimeException(
|
||||
'仍有派发中、待回执或未完成云模板任务,拒绝升级'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static function upgradeTemplates($connection): void
|
||||
{
|
||||
self::addColumn(
|
||||
$connection,
|
||||
'cloud_templates',
|
||||
'catalog_key',
|
||||
'ALTER TABLE `cloud_templates` ADD COLUMN `catalog_key` '
|
||||
. 'VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NULL '
|
||||
. 'AFTER `id`'
|
||||
);
|
||||
self::addColumn(
|
||||
$connection,
|
||||
'cloud_templates',
|
||||
'version_label',
|
||||
'ALTER TABLE `cloud_templates` ADD COLUMN `version_label` '
|
||||
. 'VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL '
|
||||
. 'AFTER `catalog_key`'
|
||||
);
|
||||
self::addColumn(
|
||||
$connection,
|
||||
'cloud_templates',
|
||||
'is_default',
|
||||
'ALTER TABLE `cloud_templates` ADD COLUMN `is_default` '
|
||||
. 'TINYINT(1) NOT NULL DEFAULT 0 AFTER `version_label`'
|
||||
);
|
||||
|
||||
$rows = $connection->table('cloud_templates')
|
||||
->orderBy('created_at')->orderBy('id')
|
||||
->get([
|
||||
'id', 'os_family', 'os_version', 'state', 'created_at',
|
||||
'catalog_key', 'version_label',
|
||||
]);
|
||||
foreach ($rows as $row) {
|
||||
$catalogKey = trim((string) ($row->catalog_key ?? ''));
|
||||
if ($catalogKey === '') {
|
||||
$catalogKey = self::catalogKey(
|
||||
(string) $row->os_family,
|
||||
(string) $row->os_version
|
||||
);
|
||||
}
|
||||
$versionLabel = trim((string) ($row->version_label ?? ''));
|
||||
if ($versionLabel === '') {
|
||||
$versionLabel = 'legacy-'
|
||||
. gmdate('Ymd-His', max(0, (int) $row->created_at))
|
||||
. '-' . substr(hash('sha256', (string) $row->id), 0, 12);
|
||||
}
|
||||
$connection->table('cloud_templates')
|
||||
->where('id', (string) $row->id)
|
||||
->update([
|
||||
'catalog_key' => $catalogKey,
|
||||
'version_label' => $versionLabel,
|
||||
]);
|
||||
}
|
||||
|
||||
$connection->table('cloud_templates')->update(['is_default' => 0]);
|
||||
$candidates = $connection->table('cloud_templates')
|
||||
->whereIn('state', ['active', 'degraded'])
|
||||
->orderBy('catalog_key')->orderByDesc('created_at')
|
||||
->orderByDesc('id')->get(['id', 'catalog_key']);
|
||||
$chosen = [];
|
||||
foreach ($candidates as $candidate) {
|
||||
$key = (string) $candidate->catalog_key;
|
||||
if (isset($chosen[$key])) {
|
||||
continue;
|
||||
}
|
||||
$chosen[$key] = true;
|
||||
$connection->table('cloud_templates')
|
||||
->where('id', (string) $candidate->id)
|
||||
->update(['is_default' => 1]);
|
||||
}
|
||||
|
||||
$duplicates = $connection->selectOne(
|
||||
'SELECT COUNT(*) AS aggregate FROM ('
|
||||
. 'SELECT `catalog_key`, `version_label` '
|
||||
. 'FROM `cloud_templates` '
|
||||
. 'GROUP BY `catalog_key`, `version_label` HAVING COUNT(*) > 1'
|
||||
. ') AS duplicate_versions'
|
||||
);
|
||||
if ((int) ($duplicates->aggregate ?? 0) !== 0) {
|
||||
throw new \RuntimeException('云模板目录版本回填产生重复值');
|
||||
}
|
||||
$connection->statement(
|
||||
'ALTER TABLE `cloud_templates` '
|
||||
. 'MODIFY COLUMN `catalog_key` VARCHAR(80) CHARACTER SET ascii '
|
||||
. 'COLLATE ascii_bin NOT NULL, '
|
||||
. 'MODIFY COLUMN `version_label` VARCHAR(64) CHARACTER SET ascii '
|
||||
. 'COLLATE ascii_bin NOT NULL'
|
||||
);
|
||||
self::addIndex(
|
||||
$connection,
|
||||
'cloud_templates',
|
||||
'cloud_template_catalog_version',
|
||||
'ALTER TABLE `cloud_templates` ADD UNIQUE KEY '
|
||||
. '`cloud_template_catalog_version` '
|
||||
. '(`catalog_key`, `version_label`)'
|
||||
);
|
||||
self::addIndex(
|
||||
$connection,
|
||||
'cloud_templates',
|
||||
'cloud_template_default',
|
||||
'ALTER TABLE `cloud_templates` ADD KEY `cloud_template_default` '
|
||||
. '(`catalog_key`, `is_default`, `state`)'
|
||||
);
|
||||
}
|
||||
|
||||
private static function createArtifacts($connection): void
|
||||
{
|
||||
if ($connection->getSchemaBuilder()->hasTable(
|
||||
'cloud_template_artifacts'
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
$connection->unprepared(<<<'SQL'
|
||||
CREATE TABLE `cloud_template_artifacts` (
|
||||
`template_id` VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
|
||||
`storage_revision` BIGINT UNSIGNED NOT NULL,
|
||||
`sealed_storage_runtime` LONGTEXT NOT NULL,
|
||||
`object_key` VARCHAR(512) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
|
||||
`object_version_id` VARCHAR(1024) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
|
||||
`object_etag` VARCHAR(256) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
|
||||
`size_bytes` BIGINT UNSIGNED NOT NULL,
|
||||
`sha256` CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
|
||||
`state` VARCHAR(24) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'ready',
|
||||
`error_code` VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
|
||||
`delete_operation_id` VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
|
||||
`delete_generation` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`delete_cycle_attempts` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`delete_presence_confirmations` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`delete_last_checked_at` BIGINT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`delete_next_check_at` BIGINT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`delete_attention_at` BIGINT UNSIGNED DEFAULT NULL,
|
||||
`verified_at` BIGINT UNSIGNED NOT NULL,
|
||||
`created_at` BIGINT UNSIGNED NOT NULL,
|
||||
`updated_at` BIGINT UNSIGNED NOT NULL,
|
||||
UNIQUE KEY `cloud_template_artifact_object` (`object_key`),
|
||||
KEY `cloud_template_artifact_state` (`state`, `verified_at`, `template_id`),
|
||||
KEY `cloud_template_artifact_delete_due` (`state`, `delete_next_check_at`, `template_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
SQL);
|
||||
}
|
||||
|
||||
private static function upgradeReplicas($connection): void
|
||||
{
|
||||
$columns = [
|
||||
'observed_state' => 'ALTER TABLE `cloud_template_replicas` '
|
||||
. 'ADD COLUMN `observed_state` VARCHAR(16) CHARACTER SET ascii '
|
||||
. "COLLATE ascii_bin NOT NULL DEFAULT 'never' AFTER `error_code`",
|
||||
'observed_revision' => 'ALTER TABLE `cloud_template_replicas` '
|
||||
. 'ADD COLUMN `observed_revision` BIGINT UNSIGNED NOT NULL '
|
||||
. 'DEFAULT 0 AFTER `observed_state`',
|
||||
'observed_generation_token' => 'ALTER TABLE `cloud_template_replicas` '
|
||||
. 'ADD COLUMN `observed_generation_token` CHAR(32) '
|
||||
. 'CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '
|
||||
. "'' AFTER `observed_revision`",
|
||||
'observed_image_bytes' => 'ALTER TABLE `cloud_template_replicas` '
|
||||
. 'ADD COLUMN `observed_image_bytes` BIGINT UNSIGNED NOT NULL '
|
||||
. 'DEFAULT 0 AFTER `observed_generation_token`',
|
||||
'observed_sha256' => 'ALTER TABLE `cloud_template_replicas` '
|
||||
. 'ADD COLUMN `observed_sha256` CHAR(64) CHARACTER SET ascii '
|
||||
. "COLLATE ascii_bin NOT NULL DEFAULT '' AFTER `observed_image_bytes`",
|
||||
'last_verified_at' => 'ALTER TABLE `cloud_template_replicas` '
|
||||
. 'ADD COLUMN `last_verified_at` BIGINT UNSIGNED NOT NULL '
|
||||
. 'DEFAULT 0 AFTER `observed_sha256`',
|
||||
];
|
||||
foreach ($columns as $name => $sql) {
|
||||
self::addColumn(
|
||||
$connection,
|
||||
'cloud_template_replicas',
|
||||
$name,
|
||||
$sql
|
||||
);
|
||||
}
|
||||
$connection->statement(
|
||||
"UPDATE `cloud_template_replicas` SET "
|
||||
. "`observed_state` = 'active', "
|
||||
. '`observed_revision` = `revision`, '
|
||||
. '`observed_generation_token` = `generation_token`, '
|
||||
. '`observed_image_bytes` = `image_bytes`, '
|
||||
. '`observed_sha256` = `sha256`, '
|
||||
. '`last_verified_at` = `updated_at` '
|
||||
. "WHERE `state` = 'active' AND `observed_state` = 'never' "
|
||||
. 'AND `observed_revision` = 0'
|
||||
);
|
||||
self::addIndex(
|
||||
$connection,
|
||||
'cloud_template_replicas',
|
||||
'cloud_template_replica_observed',
|
||||
'ALTER TABLE `cloud_template_replicas` ADD KEY '
|
||||
. '`cloud_template_replica_observed` '
|
||||
. '(`observed_state`, `last_verified_at`, `host_id`)'
|
||||
);
|
||||
}
|
||||
|
||||
private static function retireLegacySourceColumns($connection): void
|
||||
{
|
||||
foreach (['source_type', 'sealed_source_url'] as $column) {
|
||||
if ($connection->getSchemaBuilder()->hasColumn(
|
||||
'cloud_templates',
|
||||
$column
|
||||
)) {
|
||||
$connection->statement(
|
||||
'ALTER TABLE `cloud_templates` DROP COLUMN `' . $column . '`'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function publishSchemaMarker($connection): void
|
||||
{
|
||||
$logConnection = User::getConnectionResolver()->connection('log');
|
||||
self::invokeCloudSetup(
|
||||
'assertInstalledSchema',
|
||||
[$connection, $logConnection]
|
||||
);
|
||||
$marker = (string) self::invokeCloudSetup('schemaMarker');
|
||||
$fingerprint = (string) self::invokeCloudSetup(
|
||||
'installedSchemaFingerprint',
|
||||
[$connection, $logConnection]
|
||||
);
|
||||
$connection->transaction(static function () use (
|
||||
$connection,
|
||||
$marker,
|
||||
$fingerprint
|
||||
): void {
|
||||
$connection->table('configurations')->updateOrInsert(
|
||||
['name' => 'cloud_schema_fingerprint'],
|
||||
['value' => $fingerprint]
|
||||
);
|
||||
$connection->table('configurations')->updateOrInsert(
|
||||
['name' => 'cloud_schema_version'],
|
||||
['value' => $marker]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private static function invokeCloudSetup(
|
||||
string $methodName,
|
||||
array $arguments = []
|
||||
) {
|
||||
$method = new \ReflectionMethod(CloudSetup::class, $methodName);
|
||||
$method->setAccessible(true);
|
||||
return $method->invokeArgs(null, $arguments);
|
||||
}
|
||||
|
||||
private static function catalogKey(string $family, string $version): string
|
||||
{
|
||||
$key = strtolower(preg_replace(
|
||||
'/[^A-Za-z0-9]+/',
|
||||
'-',
|
||||
$family . '-' . $version
|
||||
) ?? '');
|
||||
$key = trim($key, '-');
|
||||
if ($key === '') {
|
||||
$key = 'legacy-' . substr(hash(
|
||||
'sha256',
|
||||
$family . "\0" . $version
|
||||
), 0, 16);
|
||||
}
|
||||
return substr($key, 0, 80);
|
||||
}
|
||||
|
||||
private static function addColumn(
|
||||
$connection,
|
||||
string $table,
|
||||
string $column,
|
||||
string $sql
|
||||
): void {
|
||||
if (!$connection->getSchemaBuilder()->hasColumn($table, $column)) {
|
||||
$connection->statement($sql);
|
||||
}
|
||||
}
|
||||
|
||||
private static function addIndex(
|
||||
$connection,
|
||||
string $table,
|
||||
string $index,
|
||||
string $sql
|
||||
): void {
|
||||
$database = (string) $connection->getDatabaseName();
|
||||
$exists = $connection->selectOne(
|
||||
'SELECT COUNT(*) AS aggregate FROM information_schema.STATISTICS '
|
||||
. 'WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?',
|
||||
[$database, $table, $index]
|
||||
);
|
||||
if ((int) ($exists->aggregate ?? 0) === 0) {
|
||||
$connection->statement($sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue