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/CloudSetup.php

1667 lines
74 KiB
PHP

<?php
namespace App\Command;
use App\Models\User;
use App\Services\Config;
/** Initializes only the clean-break PHP business and Agent log schemas. */
final class CloudSetup
{
private const SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations';
private const MAIN_TABLES = [
'cloud_regions', 'cloud_traffic_pool_groups',
'cloud_network_lines', 'cloud_resource_pools',
'cloud_hosts', 'cloud_host_agents',
'cloud_host_storage_configurations', 'cloud_templates',
'cloud_template_import_intents', 'cloud_template_import_parts',
'cloud_template_artifacts', 'cloud_template_replicas',
'cloud_user_isos',
'cloud_user_iso_replicas', 'cloud_rescue_templates',
'cloud_rescue_template_artifacts', 'cloud_rescue_template_replicas',
'cloud_host_resource_pool_memberships',
'cloud_host_network_attachments',
'cloud_public_ip_pool_catalog_mutex', 'cloud_public_ip_pools',
'cloud_public_ip_pool_replacement_batches',
'cloud_public_ip_pool_replacement_batch_items',
'cloud_host_vpc_transports', 'cloud_vpc_cidr_pools',
'cloud_host_capacity_policies', 'cloud_public_ips', 'cloud_offers',
'cloud_instances', 'cloud_vpcs', 'cloud_vpc_ports',
'cloud_vpc_host_states', 'cloud_vpc_membership_batches',
'cloud_vpc_membership_batch_items', '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_backup_subscriptions', 'cloud_backup_period_entitlements',
'cloud_instance_backups', 'cloud_deployment_snapshots',
'cloud_backup_restores', 'cloud_backup_restore_attempts',
'cloud_backup_recoveries', 'cloud_instance_migrations',
'cloud_instance_migration_source_debts',
'cloud_peer_migration_artifacts',
'cloud_instance_runtime', 'cloud_orders', 'cloud_order_items',
'cloud_resource_reservations', 'cloud_resource_reservation_ips',
'cloud_public_network_transitions',
'cloud_public_network_transition_ips',
'cloud_service_periods',
'cloud_traffic_pools', 'cloud_traffic_entitlements',
'cloud_traffic_ledger_events', 'cloud_traffic_instance_cursors',
'cloud_traffic_scan_failures',
'cloud_instance_network_restrictions',
'cloud_instance_network_policy_states',
'cloud_renewals', 'cloud_invoices', 'cloud_invoice_items',
'cloud_invoice_payments',
'cloud_mail_outbox', 'cloud_maintenance_notices',
'cloud_maintenance_notice_instances',
'cloud_maintenance_notice_recipients',
'cloud_service_compensations',
'cloud_service_compensation_items',
'cloud_service_compensation_recipients',
'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', 'cloud_vm_daily_traffic_cursors',
'cloud_vm_daily_traffic',
];
/** Refusal-only denylist: never read, import, rename or migrate these rows. */
private const RETIRED_LOG_TABLES = [
'engine_vm_metric_samples',
'engine_vm_traffic_checkpoints', 'engine_vm_traffic_totals',
];
private const REQUIRED_MAIN_COLUMNS = [
'cloud_public_ip_pool_catalog_mutex' => [
'id', 'updated_at',
],
'cloud_host_agents' => [
'host_id', 'revision', 'tested_revision',
'network_selectors_json', 'public_network_capabilities_json',
'portable_restore_contract_json',
'portable_restore_contract_sha256',
'last_tested_at',
],
'cloud_host_network_attachments' => [
'id', 'attachment_key', 'host_id', 'line_id', 'name',
'network_mode', 'network_selector', 'state', 'priority',
'config_revision', 'verified_agent_revision',
'verification_state', 'last_verified_at',
'verification_error_code', 'desired_revision',
'observed_revision', 'sync_state', 'last_error_code',
'created_at', 'updated_at',
],
'cloud_host_resource_pool_memberships' => [
'id', 'host_id', 'resource_pool_id', 'scheduler_state',
'priority', 'weight', 'revision', 'created_at', 'updated_at',
],
'cloud_public_ip_pools' => [
'id', 'pool_key', 'host_id', 'network_attachment_id',
'address_family', 'name', 'cidr', 'gateway', 'gateway_on_link',
'guest_prefix_length', 'dns_servers_json', 'reserved_ips_json',
'allocation_start', 'allocation_end', 'allocation_cursor',
'max_allocations', 'state', 'priority', 'desired_revision',
'observed_revision', 'sync_state', 'last_error_code',
'created_at', 'updated_at',
],
'cloud_public_ips' => [
'id', 'host_id', 'public_ip_pool_id', 'address_family',
'address', 'address_binary', 'prefix_length', 'state',
'reservation_id', 'reservation_ip_claim_id', 'instance_id',
'operation_id',
'created_at', 'updated_at',
],
'cloud_traffic_pool_groups' => [
'id', 'code', 'name', 'mode', 'revision', 'created_at',
'updated_at',
],
'cloud_network_lines' => [
'id', 'region_id', 'traffic_pool_group_id', 'code', 'name',
'is_default', 'status', 'sort', 'created_at', 'updated_at',
],
'cloud_template_import_intents' => [
'id', 'user_id', 'request_idempotency_key',
'request_fingerprint', 'source_kind', 'name', 'os_family',
'os_version', 'source_filename', 'source_size_bytes',
'source_last_modified', 'source_sha256', 'sealed_source',
'template_id', 'catalog_key', 'version_label',
'generation_token', 'storage_revision',
'sealed_storage_runtime', 'object_key', 'multipart_upload_id',
'multipart_part_size_bytes', 'multipart_part_count',
'object_version_id', 'object_etag', 'state', 'active_job_id',
'error_code', 'expires_at', 'completed_at', 'created_at',
'updated_at',
],
'cloud_template_import_parts' => [
'intent_id', 'part_number', 'size_bytes', 'etag',
'created_at', 'updated_at',
],
'cloud_template_artifacts' => [
'template_id', 'storage_revision', 'sealed_storage_runtime',
'object_key', 'object_version_id', 'object_etag', 'size_bytes',
'sha256', 'state', 'error_code', 'delete_operation_id',
'delete_generation', 'delete_cycle_attempts',
'delete_presence_confirmations', 'delete_last_checked_at',
'delete_next_check_at', 'delete_attention_at', 'verified_at',
'created_at', 'updated_at',
],
'cloud_template_replicas' => [
'host_id', 'template_id', 'revision', 'generation_token',
'state', 'image_bytes', 'sha256', 'operation_id', 'job_id',
'error_code', 'observed_state', 'observed_revision',
'observed_generation_token', 'observed_image_bytes',
'observed_sha256', 'last_verified_at', 'created_at', 'updated_at',
],
'cloud_rescue_templates' => [
'id', 'name', 'size_bytes', 'sha256', 'revision',
'generation_token', 'state', 'active_job_id',
'created_at', 'updated_at',
],
'cloud_rescue_template_artifacts' => [
'template_id', 'storage_revision', 'sealed_storage_runtime',
'object_key', 'object_version_id', 'object_etag', 'size_bytes',
'sha256', 'state', 'error_code', 'delete_operation_id',
'delete_generation', 'delete_cycle_attempts',
'delete_presence_confirmations', 'delete_last_checked_at',
'delete_next_check_at', 'delete_attention_at', 'verified_at',
'created_at', 'updated_at',
],
'cloud_rescue_template_replicas' => [
'host_id', 'template_id', 'revision', 'generation_token',
'state', 'size_bytes', 'sha256', 'operation_id', 'job_id',
'error_code', 'observed_state', 'observed_revision',
'observed_generation_token', 'observed_size_bytes',
'observed_sha256', 'last_verified_at', 'cleanup_resolved_at',
'created_at', 'updated_at',
],
'cloud_offers' => [
'backup_price',
],
'cloud_jobs' => [
'id', 'operation_id', 'request_idempotency_key',
'request_fingerprint', 'parent_job_id', 'user_id', 'host_id',
'backup_id', 'backup_restore_id', 'backup_recovery_id',
'public_network_transition_id',
'vpc_id', 'vpc_port_id', 'vpc_batch_id',
'kind', 'execution_mode', 'phase', 'outcome', 'status',
'attempts', 'dispatch_attempts', 'dispatch_deadline_at',
'dispatch_started_at', 'agent_accepted_at',
'receipt_deadline_at', 'terminal_at',
'effect_may_have_applied', 'safe_to_resubmit',
'effect_resolution', 'finalization_incident',
'finalization_error_code', 'completion_event_id', 'read_at',
'lease_token', 'lease_until', 'available_at', 'error_code',
'result_json', 'created_at', 'updated_at',
],
'cloud_host_vpc_transports' => [
'host_id', 'transport_address', 'transport_prefix_length',
'transport_interface', 'transport_mode', 'mtu',
'desired_revision', 'observed_revision', 'sync_state',
'last_operation_id', 'last_error_code', 'last_verified_at',
'created_at', 'updated_at',
],
'cloud_vpc_cidr_pools' => [
'id', 'pool_key', 'region_id', 'name', 'cidr',
'prefix_length', 'allocation_prefix', 'state',
'revision', 'created_at', 'updated_at',
],
'cloud_vpcs' => [
'id', 'vpc_key', 'user_id', 'region_id', 'cidr_pool_id',
'name', 'cidr', 'prefix_length', 'gateway', 'active_claim', 'state',
'desired_revision', 'deleted_at', 'created_at', 'updated_at',
],
'cloud_vpc_ports' => [
'id', 'port_key', 'vpc_id', 'instance_id',
'migration_host_id', 'user_id',
'private_ip', 'mac_address', 'active_claim', 'state',
'desired_revision', 'applied_revision', 'last_operation_id',
'last_error_code', 'attached_at', 'released_at',
'created_at', 'updated_at',
],
'cloud_vpc_host_states' => [
'id', 'vpc_id', 'host_id', 'desired_revision',
'applied_revision', 'desired_digest', 'applied_digest',
'sync_state', 'last_operation_id', 'last_error_code',
'last_success_at', 'last_failure_at', 'created_at', 'updated_at',
],
'cloud_vpc_membership_batches' => [
'id', 'batch_key', 'vpc_id', 'user_id', 'parent_job_id',
'kind', 'state', 'desired_revision', 'active_claim',
'last_error_code', 'terminal_at', 'created_at', 'updated_at',
],
'cloud_vpc_membership_batch_items' => [
'id', 'batch_id', 'user_id', 'instance_id', 'vpc_port_id',
'host_id', 'child_job_id', 'state', 'last_error_code',
'created_at', 'updated_at',
],
'cloud_public_ip_pool_replacement_batches' => [
'id', 'batch_key', 'host_id', 'source_public_ip_pool_id',
'target_public_ip_pool_id', 'source_initial_state',
'parent_job_id',
'requested_by_user_id', 'state', 'active_claim',
'last_error_code', 'terminal_at', 'created_at', 'updated_at',
],
'cloud_public_ip_pool_replacement_batch_items' => [
'id', 'batch_id', 'instance_id', 'address_family',
'source_reservation_id',
'source_public_ip_id', 'target_public_ip_id',
'source_network_attachment_id',
'target_network_attachment_id', 'child_job_id', 'state',
'last_error_code', 'created_at', 'updated_at',
],
'cloud_agent_completion_events' => [
'event_id', 'host_id', 'operation_id', 'payload_sha256',
'outcome', 'effect_may_have_applied', 'safe_to_resubmit',
'error_code', 'result_json', 'disposition', 'received_at',
'applied_at',
],
'cloud_backup_subscriptions' => [
'id', 'instance_id', 'user_id', 'offer_id', 'enabled',
'renew_next_period', 'frequency', 'weekday', 'retention_count',
'timezone', 'price', 'currency', 'price_version',
'state_revision', 'next_run_at',
'last_scheduled_at', 'last_backup_id', 'created_at', 'updated_at',
],
'cloud_backup_period_entitlements' => [
'id', 'subscription_id', 'instance_id', 'service_period_id',
'user_id', 'offer_id', 'invoice_id', 'starts_at', 'ends_at',
'amount', 'full_period_price', 'currency', 'price_version',
'source', 'paid_at', 'created_at', 'updated_at',
],
'cloud_instance_backups' => [
'id', 'backup_key', 'source_instance_id', 'source_host_id',
'user_id', 'source_uuid', 'name', 'purpose', 'source',
'subscription_id', 'scheduled_for', 'state', 'consistency',
'object_key', 'object_size_bytes', 'virtual_size_bytes',
'sha256', 'object_etag', 'object_version_id', 'spec_json',
'source_portable_restore_contract_json',
'source_portable_restore_contract_sha256',
'sealed_storage_runtime',
'sealed_credential', 'storage_revision', 'active_job_id',
'last_operation_id', 'delete_operation_id', 'delete_generation',
'delete_cycle_attempts', 'delete_presence_confirmations',
'delete_requested_at', 'delete_last_checked_at',
'delete_next_check_at', 'delete_attention_at', 'error_code',
'ready_at', 'created_at', 'updated_at',
],
'cloud_deployment_snapshots' => [
'id', 'snapshot_key', 'user_id', 'backup_id',
'source_instance_id', 'name', 'state', 'idempotency_key',
'request_hash', 'error_code', 'ready_at', 'deleted_at',
'created_at', 'updated_at',
],
'cloud_orders' => [
'deployment_snapshot_id',
],
'cloud_backup_restores' => [
'id', 'restore_key', 'backup_id', 'requested_by_user_id',
'request_idempotency_key', 'request_fingerprint',
'selected_plan_json', 'selected_plan_sha256', 'target_host_id',
'target_instance_id', 'reservation_id', 'job_id', 'operation_id',
'new_uuid', 'new_name', 'state', 'error_code', 'ready_at',
'created_at', 'updated_at',
],
'cloud_backup_restore_attempts' => [
'id', 'attempt_key', 'backup_restore_id', 'generation',
'operation_id', 'job_id', 'reservation_id', 'target_host_id',
'target_uuid', 'target_name', 'target_plan_json',
'target_plan_sha256', 'target_network_attachment_id',
'target_identity_json', 'target_identity_sha256',
'target_claim_set_sha256', 'target_public_network_json',
'target_public_network_sha256', 'state', 'effect_resolution',
'evidence_resolution', 'evidence_json', 'evidence_sha256',
'evidence_recorded_at', 'active_attempt', 'last_error_code',
'finalization_error_code', 'effect_resolved_at',
'terminal_at', 'created_at', 'updated_at',
],
'cloud_backup_recoveries' => [
'id', 'recovery_key', 'backup_id', 'instance_id', 'job_id',
'operation_id', 'state', 'error_code', 'completed_at',
'created_at', 'updated_at',
],
'cloud_order_items' => [
'id', 'order_id', 'code', 'label', 'amount', 'currency',
'snapshot_json', 'created_at',
],
'cloud_invoice_items' => [
'id', 'invoice_id', 'order_item_id', 'code', 'label', 'amount',
'currency', 'snapshot_json', 'created_at',
],
'cloud_maintenance_notices' => [
'id', 'notice_key', 'requested_by_user_id',
'request_idempotency_key', 'request_fingerprint', 'scope_type',
'scope_id', 'scope_snapshot_json', 'subject', 'message_text',
'impact', 'maintenance_starts_at', 'maintenance_ends_at',
'scheduled_at', 'audience_digest', 'instance_count',
'recipient_count', 'skipped_count', 'status', 'last_error_code',
'terminal_at', 'created_at', 'updated_at',
],
'cloud_maintenance_notice_instances' => [
'id', 'notice_id', 'instance_id', 'user_id', 'host_id',
'public_ip_pool_id', 'instance_name', 'ipv4', 'ipv6',
'evidence_json', 'created_at',
],
'cloud_maintenance_notice_recipients' => [
'id', 'notice_id', 'user_id', 'email_snapshot', 'status',
'attempts', 'available_at', 'lease_token', 'lease_until',
'sent_at', 'last_error_code', 'last_error', 'created_at',
'updated_at',
],
'cloud_service_compensations' => [
'id', 'compensation_key', 'incident_key',
'requested_by_user_id', 'request_idempotency_key',
'request_fingerprint', 'scope_type', 'scope_id',
'scope_snapshot_json', 'reason_text', 'incident_starts_at',
'incident_ends_at', 'compensation_seconds', 'notify_users',
'audience_digest', 'instance_count', 'recipient_count',
'applied_count', 'deferred_count', 'rejected_count',
'attention_count', 'state', 'last_error_code', 'terminal_at',
'created_at', 'updated_at',
],
'cloud_service_compensation_items' => [
'id', 'item_key', 'batch_id', 'incident_key', 'instance_id',
'user_id', 'host_id', 'instance_name', 'requested_seconds',
'service_period_id', 'traffic_entitlement_id',
'backup_entitlement_id', 'source_invoice_id',
'replacement_invoice_id', 'audit_job_id', 'before_expires_at',
'after_expires_at', 'before_period_ends_at',
'after_period_ends_at', 'before_traffic_ends_at',
'after_traffic_ends_at', 'before_backup_ends_at',
'after_backup_ends_at', 'before_snapshot_json',
'after_snapshot_json', 'state', 'attempts', 'available_at',
'lease_token', 'lease_until', 'applied_at', 'terminal_at',
'last_error_code', 'last_error', 'created_at', 'updated_at',
],
'cloud_service_compensation_recipients' => [
'id', 'batch_id', 'user_id', 'email_snapshot',
'instance_count', 'payload_json', 'payload_sha256', 'status',
'attempts', 'available_at', 'lease_token', 'lease_until',
'sent_at', 'last_error_code', 'last_error', 'created_at',
'updated_at',
],
'cloud_instance_migrations' => [
'id', 'migration_key', 'instance_id', 'vpc_port_id', 'restore_id',
'source_host_id', 'source_uuid', 'source_reservation_id',
'target_host_id', 'target_uuid', 'target_reservation_id',
'source_prepare_job_id', 'target_receive_job_id',
'firewall_job_id',
'activate_job_id', 'rollback_job_id', 'cleanup_job_id',
'cleanup_mode', 'state', 'recovery_action', 'retry_generation',
'source_endpoint', 'peer_trust_id', 'manifest_digest',
'source_was_running', 'source_autostart_enabled',
'source_service_status', 'source_network_status', 'error_code',
'cutover_at', 'completed_at', 'created_at', 'updated_at',
],
'cloud_instance_migration_source_debts' => [
'id', 'debt_key', 'migration_id', 'source_reservation_id',
'source_host_id', 'source_uuid',
'source_network_attachment_id', 'source_identity_json',
'source_identity_sha256', 'source_claim_set_sha256',
'source_public_network_json', 'source_public_network_sha256',
'cleanup_operation_id', 'cleanup_job_id', 'state',
'active_debt', 'last_error_code', 'resolved_at',
'created_at', 'updated_at',
],
'cloud_peer_migration_artifacts' => [
'id', 'migration_id', 'artifact_id', 'role',
'source_snapshot_id', 'snapshot_key', 'name', 'consistency',
'object_size_bytes',
'virtual_size_bytes', 'sha256', 'state', 'snapshot_created_at',
'created_at', 'updated_at',
],
'cloud_resource_reservations' => [
'id', 'reservation_key', 'operation_id', 'order_id',
'backup_restore_id', 'backup_restore_attempt_key',
'migration_source_debt_id', 'offer_id', 'resource_pool_id',
'host_id',
'user_id', 'instance_id', 'network_attachment_id',
'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib',
'disk_gib', 'state', 'expires_at', 'released_at',
'release_reason', 'created_at', 'updated_at',
],
'cloud_resource_reservation_ips' => [
'id', 'reservation_id', 'address_family', 'claim_generation',
'active_claim',
'network_attachment_id', 'public_ip_pool_id', 'public_ip_id',
'operation_id', 'state', 'released_at', 'release_reason',
'created_at', 'updated_at',
],
'cloud_public_network_transitions' => [
'id', 'transition_key', 'operation_id', 'kind', 'instance_id',
'host_id', 'reservation_id', 'invoice_id',
'pool_replacement_batch_item_id', 'requested_by_user_id',
'source_network_attachment_id', 'target_network_attachment_id',
'source_claim_set_sha256', 'target_claim_set_sha256',
'source_public_network_json', 'source_public_network_sha256',
'target_public_network_json', 'target_public_network_sha256',
'state', 'effect_resolution', 'active_transition',
'last_error_code', 'effect_resolved_at', 'terminal_at',
'created_at', 'updated_at',
],
'cloud_public_network_transition_ips' => [
'id', 'transition_id', 'address_family', 'source_claim_id',
'target_claim_id', 'changed', 'created_at', 'updated_at',
],
'cloud_service_periods' => [
'id', 'instance_id', 'user_id', 'plan_id', 'order_id',
'renewal_id', 'traffic_pool_group_id', 'traffic_quota_mode',
'starts_at', 'ends_at', 'traffic_quota_bytes',
'download_base_bytes', 'upload_base_bytes',
'download_used_bytes', 'upload_used_bytes', 'status',
'activated_at', 'created_at', 'updated_at',
],
'cloud_traffic_pools' => [
'id', 'user_id', 'traffic_pool_group_id', 'state',
'available_bytes', 'overage_bytes', 'version', 'last_sequence',
'last_event_id', 'created_at', 'updated_at',
],
'cloud_traffic_entitlements' => [
'id', 'pool_id', 'user_id', 'instance_id',
'service_period_id', 'starts_at', 'ends_at', 'quota_mode',
'granted_bytes', 'consumed_bytes', 'forfeited_bytes', 'state',
'activated_at', 'closed_at', 'version', 'created_at',
'updated_at',
],
'cloud_traffic_ledger_events' => [
'id', 'pool_id', 'user_id', 'pool_sequence', 'event_key',
'event_type', 'source_type', 'source_id', 'source_revision',
'payload_hash', 'instance_id', 'service_period_id',
'entitlement_id', 'amount_bytes', 'effective_at',
'observed_at', 'payload_json', 'created_at',
],
'cloud_traffic_instance_cursors' => [
'instance_id', 'user_id', 'pool_id',
'accounted_download_bytes', 'accounted_upload_bytes',
'last_observed_at', 'last_event_id', 'state',
'attention_code', 'version', 'updated_at',
],
'cloud_traffic_scan_failures' => [
'id', 'scan_kind', 'subject_id', 'attempt_count',
'last_error_code', 'retry_at', 'first_failed_at',
'last_failed_at',
],
'cloud_instance_network_restrictions' => [
'instance_id', 'user_id', 'reason_code', 'desired_active',
'revision', 'source_type', 'source_id', 'created_at', 'updated_at',
],
'cloud_instance_network_policy_states' => [
'instance_id', 'user_id', 'desired_suspended',
'applied_suspended', 'revision', 'state', 'operation_id',
'created_at', 'updated_at',
],
];
private const REQUIRED_LOG_COLUMNS = [
'cloud_vm_metric_samples' => [
'host_scope', 'vm_uuid', 'host_id', 'user_id', 'sampled_at',
'cpu_percent', 'memory_used_mib', 'memory_total_mib',
'disk_read_bps', 'disk_write_bps', 'network_rx_bps',
'network_tx_bps', 'network_rx_bytes', 'network_tx_bytes',
],
'cloud_vm_traffic_totals' => [
'host_scope', 'vm_uuid', 'host_id', 'user_id',
'last_observed_at', 'network_signature',
'network_baseline_json', 'download_rx_bytes',
'upload_tx_bytes', 'updated_at',
],
'cloud_vm_traffic_checkpoints' => [
'host_scope', 'vm_uuid', 'checkpoint_hour', 'host_id',
'user_id', 'first_observed_at', 'download_rx_bytes',
'upload_tx_bytes',
],
'cloud_vm_daily_traffic_cursors' => [
'instance_id', 'user_id', 'last_observed_at',
'download_total_bytes', 'upload_total_bytes', 'updated_at',
],
'cloud_vm_daily_traffic' => [
'instance_id', 'user_id', 'day_start', 'download_bytes',
'upload_bytes', 'sample_count', 'first_observed_at',
'last_observed_at', 'created_at', 'updated_at',
],
];
private const REQUIRED_CHECKS = [
'cloud_public_ip_pool_catalog_mutex' => [
'chk_cloud_public_ip_pool_catalog_mutex',
],
'cloud_host_network_attachments' => [
'chk_cloud_network_attachment_state',
'chk_cloud_network_attachment_verification',
'chk_cloud_network_attachment_config_revision',
],
'cloud_public_ip_pools' => [
'chk_cloud_public_ip_pool_family',
'chk_cloud_public_ip_pool_prefix',
'chk_cloud_public_ip_pool_gateway',
'chk_cloud_public_ip_pool_allocation',
],
'cloud_public_ips' => [
'chk_cloud_public_ip_family', 'chk_cloud_public_ip_prefix',
'chk_cloud_public_ip_state',
'chk_cloud_public_ip_owner_shape',
],
'cloud_traffic_pool_groups' => [
'chk_cloud_traffic_pool_group_mode',
],
'cloud_service_periods' => [
'chk_cloud_period_traffic_quota_mode',
],
'cloud_traffic_pools' => [
'chk_cloud_traffic_pool_state',
],
'cloud_traffic_entitlements' => [
'chk_cloud_traffic_entitlement_window',
'chk_cloud_traffic_entitlement_mode',
'chk_cloud_traffic_entitlement_balance',
'chk_cloud_traffic_entitlement_state',
],
'cloud_traffic_instance_cursors' => [
'chk_cloud_traffic_cursor_state',
],
'cloud_traffic_scan_failures' => [
'chk_cloud_traffic_scan_failure_kind',
],
'cloud_instance_network_restrictions' => [
'chk_cloud_network_restriction_reason',
],
'cloud_instance_network_policy_states' => [
'chk_cloud_network_policy_state',
],
'cloud_template_import_intents' => [
'chk_cloud_template_import_source',
'chk_cloud_template_import_state',
'chk_cloud_template_import_completion',
],
'cloud_template_import_parts' => [
'chk_cloud_template_import_part',
],
'cloud_host_vpc_transports' => [
'chk_cloud_host_vpc_transport_prefix',
'chk_cloud_host_vpc_transport_mode',
'chk_cloud_host_vpc_transport_mtu',
'chk_cloud_host_vpc_transport_state',
'chk_cloud_host_vpc_transport_revision',
],
'cloud_vpc_cidr_pools' => [
'chk_cloud_vpc_cidr_pool_prefix',
'chk_cloud_vpc_cidr_pool_state',
],
'cloud_vpcs' => [
'chk_cloud_vpc_prefix', 'chk_cloud_vpc_active_claim',
'chk_cloud_vpc_state',
'chk_cloud_vpc_deleted_at',
],
'cloud_vpc_ports' => [
'chk_cloud_vpc_port_active_claim',
'chk_cloud_vpc_port_state', 'chk_cloud_vpc_port_release',
'chk_cloud_vpc_port_revision', 'chk_cloud_vpc_port_migration',
],
'cloud_vpc_host_states' => [
'chk_cloud_vpc_host_state', 'chk_cloud_vpc_host_revision',
],
'cloud_vpc_membership_batches' => [
'chk_cloud_vpc_membership_batch_kind',
'chk_cloud_vpc_membership_batch_state',
'chk_cloud_vpc_membership_batch_claim',
],
'cloud_vpc_membership_batch_items' => [
'chk_cloud_vpc_membership_batch_item_state',
],
'cloud_public_ip_pool_replacement_batches' => [
'chk_cloud_public_pool_replacement_distinct',
'chk_cloud_public_pool_replacement_source_initial',
'chk_cloud_public_pool_replacement_state',
'chk_cloud_public_pool_replacement_claim',
],
'cloud_public_ip_pool_replacement_batch_items' => [
'chk_cloud_public_pool_replacement_item_ip',
'chk_cloud_public_pool_replacement_item_family',
'chk_cloud_public_pool_replacement_item_state',
],
'cloud_backup_subscriptions' => [
'chk_cloud_backup_subscription_frequency',
'chk_cloud_backup_subscription_weekday',
'chk_cloud_backup_subscription_retention',
],
'cloud_backup_period_entitlements' => [
'chk_cloud_backup_entitlement_source',
'chk_cloud_backup_entitlement_window',
],
'cloud_deployment_snapshots' => [
'chk_cloud_deployment_snapshot_state',
],
'cloud_backup_restore_attempts' => [
'chk_cloud_backup_restore_attempt_generation',
'chk_cloud_backup_restore_attempt_active',
'chk_cloud_backup_restore_attempt_evidence',
'chk_cloud_backup_restore_attempt_plan',
'chk_cloud_backup_restore_attempt_state',
'chk_cloud_backup_restore_attempt_shape',
],
'cloud_backup_restores' => [
'chk_cloud_backup_restore_request',
],
'cloud_instance_migration_source_debts' => [
'chk_cloud_migration_source_debt_active',
'chk_cloud_migration_source_debt_state',
'chk_cloud_migration_source_debt_cleanup_pair',
'chk_cloud_migration_source_debt_shape',
],
'cloud_invoice_payments' => [
'chk_cloud_invoice_payment_status',
],
'cloud_maintenance_notices' => [
'chk_cloud_maintenance_notice_scope',
'chk_cloud_maintenance_notice_impact',
'chk_cloud_maintenance_notice_status',
'chk_cloud_maintenance_notice_window',
'chk_cloud_maintenance_notice_terminal',
],
'cloud_maintenance_notice_recipients' => [
'chk_cloud_maintenance_notice_recipient_status',
'chk_cloud_maintenance_notice_recipient_shape',
],
'cloud_service_compensations' => [
'chk_cloud_service_compensation_scope',
'chk_cloud_service_compensation_request',
'chk_cloud_service_compensation_window',
'chk_cloud_service_compensation_counts',
'chk_cloud_service_compensation_state',
'chk_cloud_service_compensation_terminal',
],
'cloud_service_compensation_items' => [
'chk_cloud_service_compensation_item_state',
'chk_cloud_service_compensation_item_window',
'chk_cloud_service_compensation_item_lease',
'chk_cloud_service_compensation_item_terminal',
'chk_cloud_service_compensation_item_applied',
'chk_cloud_service_compensation_item_error',
],
'cloud_service_compensation_recipients' => [
'chk_cloud_service_compensation_recipient_status',
'chk_cloud_service_compensation_recipient_payload',
'chk_cloud_service_compensation_recipient_shape',
],
'cloud_jobs' => [
'chk_cloud_job_dispatch_once', 'chk_cloud_job_attempt_shape',
'chk_cloud_job_phase', 'chk_cloud_job_outcome',
'chk_cloud_job_effect_resolution',
'chk_cloud_job_finalization_incident',
'chk_cloud_job_terminal_shape', 'chk_cloud_job_dispatch_shape',
'chk_cloud_job_target_shape', 'chk_cloud_job_effect_shape',
],
'cloud_resource_reservations' => [
'chk_cloud_resource_reservation_source',
],
'cloud_resource_reservation_ips' => [
'chk_cloud_reservation_ip_family',
'chk_cloud_reservation_ip_generation',
'chk_cloud_reservation_ip_active',
'chk_cloud_reservation_ip_state',
'chk_cloud_reservation_ip_release',
],
'cloud_public_network_transitions' => [
'chk_cloud_public_network_transition_kind',
'chk_cloud_public_network_transition_state',
'chk_cloud_public_network_transition_active',
'chk_cloud_public_network_transition_shape',
],
'cloud_public_network_transition_ips' => [
'chk_cloud_public_network_transition_ip_family',
'chk_cloud_public_network_transition_ip_changed',
'chk_cloud_public_network_transition_ip_target',
],
'cloud_agent_completion_events' => [
'chk_cloud_completion_outcome',
'chk_cloud_completion_disposition',
],
];
private const REQUIRED_INDEXES = [
'cloud_public_ip_pool_catalog_mutex' => [
'PRIMARY',
],
'cloud_host_network_attachments' => [
'PRIMARY', 'cloud_host_network_attachment_key',
'cloud_host_network_selector',
'cloud_host_network_attachment_line',
'cloud_network_attachment_sync',
'cloud_network_attachment_verification',
],
'cloud_public_ip_pools' => [
'PRIMARY', 'cloud_public_ip_pool_key',
'cloud_public_ip_pool_cidr',
'cloud_public_ip_pool_attachment',
'cloud_public_ip_pool_sync',
],
'cloud_public_ips' => [
'PRIMARY', 'cloud_public_ip_address',
'cloud_public_ip_available', 'cloud_public_ip_pool_available',
'cloud_public_ip_reservation', 'cloud_public_ip_claim',
'cloud_public_ip_instance',
'cloud_public_ip_operation',
],
'cloud_traffic_pool_groups' => [
'PRIMARY', 'cloud_traffic_pool_group_code',
'cloud_traffic_pool_group_mode',
],
'cloud_network_lines' => [
'cloud_network_line_traffic_pool_group',
],
'cloud_service_periods' => [
'cloud_period_traffic_pool',
],
'cloud_traffic_pools' => [
'PRIMARY', 'cloud_traffic_pool_owner_group',
'cloud_traffic_pool_state',
],
'cloud_traffic_entitlements' => [
'PRIMARY', 'cloud_traffic_entitlement_period',
'cloud_traffic_entitlement_fefo',
'cloud_traffic_entitlement_activation',
'cloud_traffic_entitlement_owner',
'cloud_traffic_entitlement_instance',
],
'cloud_traffic_ledger_events' => [
'PRIMARY', 'cloud_traffic_ledger_event_key',
'cloud_traffic_ledger_sequence', 'cloud_traffic_ledger_source',
'cloud_traffic_ledger_owner', 'cloud_traffic_ledger_instance',
'cloud_traffic_ledger_period', 'cloud_traffic_ledger_pool_time',
],
'cloud_traffic_instance_cursors' => [
'PRIMARY', 'cloud_traffic_cursor_pool',
'cloud_traffic_cursor_owner',
],
'cloud_traffic_scan_failures' => [
'PRIMARY', 'cloud_traffic_scan_failure_subject',
'cloud_traffic_scan_failure_retry',
],
'cloud_instance_network_restrictions' => [
'PRIMARY', 'cloud_network_restriction_desired',
'cloud_network_restriction_owner',
],
'cloud_instance_network_policy_states' => [
'PRIMARY', 'cloud_network_policy_operation',
'cloud_network_policy_state', 'cloud_network_policy_owner',
],
'cloud_template_import_intents' => [
'PRIMARY', 'cloud_template_import_request',
'cloud_template_import_template',
'cloud_template_import_generation',
'cloud_template_import_object', 'cloud_template_import_state',
'cloud_template_import_job',
],
'cloud_template_import_parts' => [
'PRIMARY', 'cloud_template_import_part_order',
],
'cloud_template_artifacts' => [
'PRIMARY', 'cloud_template_artifact_object',
'cloud_template_artifact_state',
'cloud_template_artifact_delete_due',
],
'cloud_template_replicas' => [
'PRIMARY', 'cloud_template_replica_ready',
'cloud_template_replica_observed',
],
'cloud_rescue_templates' => [
'PRIMARY', 'cloud_rescue_template_generation',
'cloud_rescue_template_catalog',
],
'cloud_rescue_template_artifacts' => [
'PRIMARY', 'cloud_rescue_artifact_object',
'cloud_rescue_artifact_state',
'cloud_rescue_artifact_delete_due',
],
'cloud_rescue_template_replicas' => [
'PRIMARY', 'cloud_rescue_replica_ready',
'cloud_rescue_replica_observed',
'cloud_rescue_replica_cleanup',
],
'cloud_host_vpc_transports' => [
'PRIMARY', 'cloud_host_vpc_transport_operation',
'cloud_host_vpc_transport_state',
],
'cloud_vpc_cidr_pools' => [
'PRIMARY', 'cloud_vpc_cidr_pool_key',
'cloud_vpc_cidr_pool_region', 'cloud_vpc_cidr_pool_catalog',
],
'cloud_vpcs' => [
'PRIMARY', 'cloud_vpc_key', 'cloud_vpc_region_cidr',
'cloud_vpc_owner', 'cloud_vpc_region_state',
'cloud_vpc_pool_state',
],
'cloud_vpc_ports' => [
'PRIMARY', 'cloud_vpc_port_key', 'cloud_vpc_port_ip_claim',
'cloud_vpc_port_instance_claim', 'cloud_vpc_port_mac',
'cloud_vpc_port_operation', 'cloud_vpc_port_owner',
'cloud_vpc_port_vpc_state',
],
'cloud_vpc_host_states' => [
'PRIMARY', 'cloud_vpc_host_identity',
'cloud_vpc_host_operation', 'cloud_vpc_host_sync',
],
'cloud_vpc_membership_batches' => [
'PRIMARY', 'cloud_vpc_membership_batch_key',
'cloud_vpc_membership_batch_job',
'cloud_vpc_membership_batch_active',
'cloud_vpc_membership_batch_owner',
],
'cloud_vpc_membership_batch_items' => [
'PRIMARY', 'cloud_vpc_membership_batch_instance',
'cloud_vpc_membership_batch_port',
'cloud_vpc_membership_batch_child',
'cloud_vpc_membership_batch_item_owner',
'cloud_vpc_membership_batch_item_state',
],
'cloud_public_ip_pool_replacement_batches' => [
'PRIMARY', 'cloud_public_pool_replacement_key',
'cloud_public_pool_replacement_job',
'cloud_public_pool_replacement_active',
'cloud_public_pool_replacement_host',
'cloud_public_pool_replacement_target',
],
'cloud_public_ip_pool_replacement_batch_items' => [
'PRIMARY', 'cloud_public_pool_replacement_instance',
'cloud_public_pool_replacement_child',
'cloud_public_pool_replacement_target_ip',
'cloud_public_pool_replacement_item_state',
],
'cloud_maintenance_notices' => [
'PRIMARY', 'cloud_maintenance_notice_key',
'cloud_maintenance_notice_request',
'cloud_maintenance_notice_delivery',
'cloud_maintenance_notice_scope',
],
'cloud_maintenance_notice_instances' => [
'PRIMARY', 'cloud_maintenance_notice_instance',
'cloud_maintenance_notice_instance_owner',
'cloud_maintenance_notice_instance_source',
],
'cloud_maintenance_notice_recipients' => [
'PRIMARY', 'cloud_maintenance_notice_recipient',
'cloud_maintenance_notice_recipient_due',
'cloud_maintenance_notice_recipient_lease',
'cloud_maintenance_notice_recipient_owner',
],
'cloud_service_compensations' => [
'PRIMARY', 'cloud_service_compensation_key',
'cloud_service_compensation_request',
'cloud_service_compensation_incident_identity',
'cloud_service_compensation_incident',
'cloud_service_compensation_scope',
'cloud_service_compensation_state',
],
'cloud_service_compensation_items' => [
'PRIMARY', 'cloud_service_compensation_item_key',
'cloud_service_compensation_item_instance',
'cloud_service_compensation_item_incident',
'cloud_service_compensation_item_batch_state',
'cloud_service_compensation_item_due',
'cloud_service_compensation_item_lease',
'cloud_service_compensation_item_open',
'cloud_service_compensation_item_owner',
'cloud_service_compensation_item_instance_history',
'cloud_service_compensation_item_audit_job',
],
'cloud_service_compensation_recipients' => [
'PRIMARY', 'cloud_service_compensation_recipient',
'cloud_service_compensation_recipient_due',
'cloud_service_compensation_recipient_lease',
'cloud_service_compensation_recipient_owner',
],
'cloud_jobs' => [
'PRIMARY', 'uniq_cloud_operation', 'uniq_cloud_job_request',
'uniq_cloud_job_completion_event', 'uniq_cloud_provision_order',
'cloud_job_poll', 'cloud_job_receipt_deadline',
'cloud_job_backup', 'cloud_job_backup_restore',
'cloud_job_backup_recovery',
'cloud_job_public_network_transition',
'cloud_job_vpc', 'cloud_job_vpc_port', 'cloud_job_vpc_batch',
'cloud_job_supersedes', 'cloud_job_vpc_repair_root',
],
'cloud_agent_completion_events' => [
'PRIMARY', 'uniq_cloud_completion_operation',
],
'cloud_resource_reservation_ips' => [
'PRIMARY', 'cloud_reservation_ip_active',
'cloud_reservation_ip_generation',
'cloud_reservation_ip_operation',
'cloud_reservation_ip_address', 'cloud_reservation_ip_pool',
'cloud_reservation_ip_attachment',
],
'cloud_public_network_transitions' => [
'PRIMARY', 'cloud_public_network_transition_key',
'cloud_public_network_transition_operation',
'cloud_public_network_transition_active_instance',
'cloud_public_network_transition_active_reservation',
'cloud_public_network_transition_host_state',
'cloud_public_network_transition_invoice',
'cloud_public_network_transition_batch_item',
],
'cloud_public_network_transition_ips' => [
'PRIMARY', 'cloud_public_network_transition_ip_family',
'cloud_public_network_transition_ip_target',
'cloud_public_network_transition_ip_source',
],
'cloud_backup_subscriptions' => [
'PRIMARY', 'cloud_backup_subscription_instance',
'cloud_backup_subscription_due',
'cloud_backup_subscription_owner',
],
'cloud_backup_period_entitlements' => [
'PRIMARY', 'cloud_backup_entitlement_period',
'cloud_backup_entitlement_subscription',
'cloud_backup_entitlement_instance',
'cloud_backup_entitlement_owner',
'cloud_backup_entitlement_invoice',
],
'cloud_instance_backups' => [
'PRIMARY', 'cloud_instance_backup_key',
'cloud_instance_backup_object', 'cloud_instance_backup_schedule',
'cloud_instance_backup_source',
'cloud_instance_backup_owner', 'cloud_instance_backup_operation',
'cloud_instance_backup_delete_due',
],
'cloud_deployment_snapshots' => [
'PRIMARY', 'cloud_deployment_snapshot_key',
'cloud_deployment_snapshot_backup',
'cloud_deployment_snapshot_request',
'cloud_deployment_snapshot_owner',
'cloud_deployment_snapshot_source',
],
'cloud_orders' => [
'cloud_order_deployment_snapshot',
],
'cloud_backup_restores' => [
'PRIMARY', 'cloud_backup_restore_key',
'cloud_backup_restore_request',
'cloud_backup_restore_operation',
'cloud_backup_restore_reservation',
'cloud_backup_restore_instance', 'cloud_backup_restore_backup',
'cloud_backup_restore_host',
],
'cloud_backup_restore_attempts' => [
'PRIMARY', 'cloud_backup_restore_attempt_key',
'cloud_backup_restore_attempt_operation',
'cloud_backup_restore_attempt_generation',
'cloud_backup_restore_attempt_active',
'cloud_backup_restore_attempt_job',
'cloud_backup_restore_attempt_reservation',
'cloud_backup_restore_attempt_host',
],
'cloud_backup_recoveries' => [
'PRIMARY', 'cloud_backup_recovery_key',
'cloud_backup_recovery_operation', 'cloud_backup_recovery_job',
'cloud_backup_recovery_instance', 'cloud_backup_recovery_backup',
],
'cloud_order_items' => [
'PRIMARY', 'cloud_order_item_code', 'cloud_order_item_order',
],
'cloud_invoice_items' => [
'PRIMARY', 'cloud_invoice_item_code',
'cloud_invoice_item_invoice', 'cloud_invoice_item_order_item',
],
'cloud_instance_migrations' => [
'PRIMARY', 'cloud_instance_migration_key',
'cloud_instance_migration_restore',
'cloud_instance_migration_prepare_job',
'cloud_instance_migration_receive_job',
'cloud_instance_migration_firewall_job',
'cloud_instance_migration_activate_job',
'cloud_instance_migration_rollback_job',
'cloud_instance_migration_cleanup_job',
'cloud_instance_migration_instance',
'cloud_instance_migration_vpc_port',
'cloud_instance_migration_source',
'cloud_instance_migration_target',
],
'cloud_instance_migration_source_debts' => [
'PRIMARY', 'cloud_migration_source_debt_key',
'cloud_migration_source_debt_migration',
'cloud_migration_source_debt_active_reservation',
'cloud_migration_source_debt_cleanup_operation',
'cloud_migration_source_debt_cleanup_job',
'cloud_migration_source_debt_host',
],
'cloud_peer_migration_artifacts' => [
'PRIMARY', 'cloud_peer_migration_artifact',
'cloud_peer_migration_snapshot',
'cloud_peer_migration_source_snapshot',
'cloud_peer_migration_state',
],
'cloud_resource_reservations' => [
'PRIMARY', 'cloud_resource_reservation_key',
'cloud_resource_reservation_operation',
'cloud_resource_reservation_order',
'cloud_resource_reservation_backup_restore',
'cloud_resource_reservation_backup_attempt',
'cloud_resource_reservation_migration_debt',
'cloud_resource_reservation_host',
'cloud_resource_reservation_pool',
'cloud_resource_reservation_instance',
'cloud_resource_reservation_network',
'cloud_resource_reservation_ip',
],
];
/**
* Safety-critical indexes whose name alone is not a sufficient contract.
* `non_unique` mirrors information_schema.STATISTICS: 0 is unique, 1 is
* non-unique. Column order is part of the ownership/fencing semantics.
*/
private const REQUIRED_INDEX_SHAPES = [
'cloud_maintenance_notices' => [
'cloud_maintenance_notice_key' => [
'non_unique' => 0,
'columns' => ['notice_key'],
],
'cloud_maintenance_notice_request' => [
'non_unique' => 0,
'columns' => [
'requested_by_user_id', 'request_idempotency_key',
],
],
'cloud_maintenance_notice_delivery' => [
'non_unique' => 1,
'columns' => ['status', 'scheduled_at', 'id'],
],
],
'cloud_maintenance_notice_instances' => [
'cloud_maintenance_notice_instance' => [
'non_unique' => 0,
'columns' => ['notice_id', 'instance_id'],
],
'cloud_maintenance_notice_instance_owner' => [
'non_unique' => 1,
'columns' => ['notice_id', 'user_id', 'id'],
],
],
'cloud_maintenance_notice_recipients' => [
'cloud_maintenance_notice_recipient' => [
'non_unique' => 0,
'columns' => ['notice_id', 'user_id'],
],
'cloud_maintenance_notice_recipient_due' => [
'non_unique' => 1,
'columns' => ['status', 'available_at', 'id'],
],
'cloud_maintenance_notice_recipient_lease' => [
'non_unique' => 1,
'columns' => ['status', 'lease_until', 'id'],
],
],
'cloud_service_compensations' => [
'cloud_service_compensation_key' => [
'non_unique' => 0,
'columns' => ['compensation_key'],
],
'cloud_service_compensation_request' => [
'non_unique' => 0,
'columns' => [
'requested_by_user_id', 'request_idempotency_key',
],
],
'cloud_service_compensation_incident_identity' => [
'non_unique' => 0,
'columns' => ['id', 'incident_key'],
],
],
'cloud_service_compensation_items' => [
'cloud_service_compensation_item_instance' => [
'non_unique' => 0,
'columns' => ['batch_id', 'instance_id'],
],
'cloud_service_compensation_item_incident' => [
'non_unique' => 0,
'columns' => ['incident_key', 'instance_id'],
],
'cloud_service_compensation_item_due' => [
'non_unique' => 1,
'columns' => ['state', 'available_at', 'id'],
],
'cloud_service_compensation_item_lease' => [
'non_unique' => 1,
'columns' => ['state', 'lease_until', 'id'],
],
'cloud_service_compensation_item_open' => [
'non_unique' => 1,
'columns' => ['instance_id', 'state', 'id'],
],
],
'cloud_service_compensation_recipients' => [
'cloud_service_compensation_recipient' => [
'non_unique' => 0,
'columns' => ['batch_id', 'user_id'],
],
'cloud_service_compensation_recipient_due' => [
'non_unique' => 1,
'columns' => ['status', 'available_at', 'id'],
],
'cloud_service_compensation_recipient_lease' => [
'non_unique' => 1,
'columns' => ['status', 'lease_until', 'id'],
],
],
'cloud_backup_restores' => [
'cloud_backup_restore_request' => [
'non_unique' => 0,
'columns' => [
'requested_by_user_id', 'request_idempotency_key',
],
],
],
'cloud_backup_restore_attempts' => [
'cloud_backup_restore_attempt_generation' => [
'non_unique' => 0,
'columns' => ['backup_restore_id', 'generation'],
],
'cloud_backup_restore_attempt_active' => [
'non_unique' => 0,
'columns' => ['backup_restore_id', 'active_attempt'],
],
'cloud_backup_restore_attempt_operation' => [
'non_unique' => 0,
'columns' => ['operation_id'],
],
'cloud_backup_restore_attempt_reservation' => [
'non_unique' => 0,
'columns' => ['reservation_id'],
],
],
'cloud_instance_migration_source_debts' => [
'cloud_migration_source_debt_migration' => [
'non_unique' => 0,
'columns' => ['migration_id'],
],
'cloud_migration_source_debt_active_reservation' => [
'non_unique' => 0,
'columns' => ['source_reservation_id', 'active_debt'],
],
'cloud_migration_source_debt_cleanup_operation' => [
'non_unique' => 0,
'columns' => ['cleanup_operation_id'],
],
],
'cloud_resource_reservations' => [
'cloud_resource_reservation_backup_attempt' => [
'non_unique' => 0,
'columns' => ['backup_restore_attempt_key'],
],
'cloud_resource_reservation_migration_debt' => [
'non_unique' => 0,
'columns' => ['migration_source_debt_id'],
],
],
'cloud_public_ips' => [
'cloud_public_ip_reservation' => [
'non_unique' => 1,
'columns' => [
'reservation_id', 'address_family', 'state', 'id',
],
],
'cloud_public_ip_claim' => [
'non_unique' => 0,
'columns' => ['reservation_ip_claim_id'],
],
'cloud_public_ip_operation' => [
'non_unique' => 0,
'columns' => ['operation_id', 'address_family'],
],
],
'cloud_resource_reservation_ips' => [
'cloud_reservation_ip_active' => [
'non_unique' => 0,
'columns' => [
'reservation_id', 'address_family', 'active_claim',
],
],
'cloud_reservation_ip_generation' => [
'non_unique' => 0,
'columns' => [
'reservation_id', 'claim_generation', 'address_family',
],
],
'cloud_reservation_ip_operation' => [
'non_unique' => 0,
'columns' => ['operation_id', 'address_family'],
],
],
'cloud_public_network_transitions' => [
'cloud_public_network_transition_key' => [
'non_unique' => 0,
'columns' => ['transition_key'],
],
'cloud_public_network_transition_operation' => [
'non_unique' => 0,
'columns' => ['operation_id'],
],
'cloud_public_network_transition_active_instance' => [
'non_unique' => 0,
'columns' => ['instance_id', 'active_transition'],
],
'cloud_public_network_transition_active_reservation' => [
'non_unique' => 0,
'columns' => ['reservation_id', 'active_transition'],
],
'cloud_public_network_transition_invoice' => [
'non_unique' => 0,
'columns' => ['invoice_id'],
],
'cloud_public_network_transition_batch_item' => [
'non_unique' => 0,
'columns' => ['pool_replacement_batch_item_id'],
],
],
'cloud_public_network_transition_ips' => [
'cloud_public_network_transition_ip_family' => [
'non_unique' => 0,
'columns' => ['transition_id', 'address_family'],
],
'cloud_public_network_transition_ip_target' => [
'non_unique' => 0,
'columns' => ['target_claim_id'],
],
],
'cloud_jobs' => [
'cloud_job_public_network_transition' => [
'non_unique' => 0,
'columns' => ['public_network_transition_id'],
],
],
];
private const REQUIRED_FOREIGN_KEYS = [
'cloud_template_import_intents' => [
'fk_cloud_template_import_user',
],
'cloud_template_import_parts' => [
'fk_cloud_template_import_part_intent',
],
'cloud_maintenance_notice_instances' => [
'fk_cloud_maintenance_notice_instance_notice',
],
'cloud_maintenance_notice_recipients' => [
'fk_cloud_maintenance_notice_recipient_notice',
],
'cloud_service_compensation_items' => [
'fk_cloud_service_compensation_item_batch',
],
'cloud_service_compensation_recipients' => [
'fk_cloud_service_compensation_recipient_batch',
],
'cloud_jobs' => ['fk_cloud_job_user'],
];
public static function initialize(): 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();
$schema = $connection->getSchemaBuilder();
if (!$schema->hasTable('user') || !$schema->hasTable('shop')) {
throw new \RuntimeException('请先在全新业务数据库导入 web/sql/ss.sql');
}
$logConnection = User::getConnectionResolver()->connection('log');
// MySQL advisory lock names are limited to 64 characters. Lock both
// schemas in a fixed order: one main database can never be installed
// concurrently with two log targets, and one shared log database can
// never receive concurrent DDL from two main installations.
$mainLockName = 'virt-cloud-main:' . substr(hash('sha256',
(string) $connection->getDatabaseName()), 0, 48);
$logLockName = 'virt-cloud-log:' . substr(hash('sha256',
(string) $logConnection->getDatabaseName()), 0, 48);
$mainLocked = false;
$logLocked = false;
try {
$locked = $connection->selectOne(
'SELECT GET_LOCK(?, 30) AS acquired', [$mainLockName]
);
$mainLocked = (int) ($locked->acquired ?? 0) === 1;
if (!$mainLocked) {
throw new \RuntimeException(
'无法取得云业务数据库初始化锁,请稍后重试'
);
}
$locked = $logConnection->selectOne(
'SELECT GET_LOCK(?, 30) AS acquired', [$logLockName]
);
$logLocked = (int) ($locked->acquired ?? 0) === 1;
if (!$logLocked) {
throw new \RuntimeException(
'无法取得云日志数据库初始化锁,请稍后重试'
);
}
$installed = self::assertFreshSchemaBoundary(
$connection, $logConnection
);
if (!$installed) {
// MySQL DDL commits implicitly. A failed first install is
// intentionally not repaired: without the final marker the
// next run rejects the partial database and requires a fresh
// one, exactly matching the no-migration deployment contract.
$connection->unprepared(self::schema('cloud.sql'));
$logConnection->unprepared(self::schema('cloud_log.sql'));
self::assertInstalledSchema($connection, $logConnection);
self::ensureMultiHostDefaults($connection);
// One atomic insert publishes both the source version and the
// exact installed information_schema signature. The version
// row is therefore never visible without its fingerprint.
$connection->table('configurations')->insert([
[
'name' => 'cloud_schema_fingerprint',
'value' => self::installedSchemaFingerprint(
$connection, $logConnection
),
],
[
'name' => 'cloud_schema_version',
'value' => self::schemaMarker(),
],
]);
} else {
self::ensureMultiHostDefaults($connection);
}
} finally {
if ($logLocked) {
$logConnection->selectOne(
'SELECT RELEASE_LOCK(?) AS released', [$logLockName]
);
}
if ($mainLocked) {
$connection->selectOne(
'SELECT RELEASE_LOCK(?) AS released', [$mainLockName]
);
}
}
echo "云业务表与 Agent 指标日志表已准备。未连接或修改任何宿主机。\n";
return true;
}
/** Seed only immutable/default control rows in an otherwise fresh schema. */
private static function ensureMultiHostDefaults($connection): void
{
$connection->table('cloud_public_ip_pool_catalog_mutex')
->insertOrIgnore(['id' => 1, 'updated_at' => 0]);
$connection->table('configurations')->insertOrIgnore([
'name' => 'cloud_network_lines_enabled',
'value' => 'false',
]);
$connection->table('configurations')->insertOrIgnore([
'name' => 'cloud_vpc_enabled',
'value' => 'false',
]);
}
/** Refuse to mix the Go/Agent schema with any unmarked cloud database. */
private static function assertFreshSchemaBoundary($connection,
$logConnection): bool
{
$marker = $connection->table('configurations')
->where('name', 'cloud_schema_version')->first(['value']);
if ($marker !== null) {
if (!hash_equals(self::schemaMarker(), (string) $marker->value)) {
throw new \RuntimeException(
'虚拟化数据库版本不兼容,请使用全新数据库'
);
}
foreach (self::RETIRED_LOG_TABLES as $retiredTable) {
if ($logConnection->getSchemaBuilder()->hasTable($retiredTable)) {
throw new \RuntimeException(
'日志库包含已废弃的 Python Engine 表,请使用全新日志库'
);
}
}
self::assertInstalledSchema($connection, $logConnection);
$fingerprint = $connection->table('configurations')
->where('name', 'cloud_schema_fingerprint')->first(['value']);
$actual = self::installedSchemaFingerprint(
$connection, $logConnection
);
if ($fingerprint === null
|| !is_string($fingerprint->value)
|| preg_match('/^[a-f0-9]{64}$/D',
$fingerprint->value) !== 1
|| !hash_equals($fingerprint->value, $actual)) {
throw new \RuntimeException(
'虚拟化数据库结构已变化,请使用全新数据库'
);
}
return true;
}
if ($connection->table('configurations')
->where('name', 'cloud_schema_fingerprint')->exists()) {
throw new \RuntimeException(
'检测到不完整的云数据库安装标记,请使用全新数据库'
);
}
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(
'检测到未标记的虚拟化日志库,不支持原地升级'
);
}
}
return false;
}
/** Reject a marker that points at a partial, edited or incompatible schema. */
private static function assertInstalledSchema($connection,
$logConnection): void
{
$schema = $connection->getSchemaBuilder();
foreach (self::MAIN_TABLES as $table) {
if (!$schema->hasTable($table)) {
throw new \RuntimeException(
'云数据库结构不完整,请使用全新数据库:' . $table
);
}
}
$logSchema = $logConnection->getSchemaBuilder();
foreach (self::LOG_TABLES as $table) {
if (!$logSchema->hasTable($table)) {
throw new \RuntimeException(
'云日志数据库结构不完整,请使用全新日志库:' . $table
);
}
}
foreach (self::REQUIRED_MAIN_COLUMNS as $table => $columns) {
foreach ($columns as $column) {
if (!$schema->hasColumn($table, $column)) {
throw new \RuntimeException(
'云数据库缺少必需字段,请使用全新数据库:'
. $table . '.' . $column
);
}
}
}
foreach (self::REQUIRED_LOG_COLUMNS as $table => $columns) {
foreach ($columns as $column) {
if (!$logSchema->hasColumn($table, $column)) {
throw new \RuntimeException(
'云日志数据库缺少必需字段,请使用全新日志库:'
. $table . '.' . $column
);
}
}
}
self::assertNamedDatabaseObjects(
$connection, 'TABLE_CONSTRAINTS', 'CONSTRAINT_NAME',
'CONSTRAINT_TYPE', 'CHECK', self::REQUIRED_CHECKS
);
self::assertNamedDatabaseObjects(
$connection, 'STATISTICS', 'INDEX_NAME', null, null,
self::REQUIRED_INDEXES
);
self::assertIndexShapes($connection, self::REQUIRED_INDEX_SHAPES);
self::assertNamedDatabaseObjects(
$connection, 'TABLE_CONSTRAINTS', 'CONSTRAINT_NAME',
'CONSTRAINT_TYPE', 'FOREIGN KEY', self::REQUIRED_FOREIGN_KEYS
);
}
private static function assertNamedDatabaseObjects($connection,
string $informationTable, string $nameColumn, ?string $typeColumn,
?string $type, array $required): void
{
$database = (string) $connection->getDatabaseName();
foreach ($required as $table => $names) {
$sql = 'SELECT DISTINCT ' . $nameColumn . ' AS object_name FROM '
. 'information_schema.' . $informationTable
. ' WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?';
$bindings = [$database, $table];
if ($typeColumn !== null && $type !== null) {
$sql .= ' AND ' . $typeColumn . ' = ?';
$bindings[] = $type;
}
$found = [];
foreach ($connection->select($sql, $bindings) as $row) {
$found[(string) ($row->object_name ?? '')] = true;
}
foreach ($names as $name) {
if (!isset($found[$name])) {
throw new \RuntimeException(
'云数据库结构校验失败,请使用全新数据库:'
. $table . '.' . $name
);
}
}
}
}
private static function assertIndexShapes($connection,
array $required): void
{
$database = (string) $connection->getDatabaseName();
foreach ($required as $table => $indexes) {
$rows = $connection->select(
'SELECT INDEX_NAME, NON_UNIQUE, SEQ_IN_INDEX, COLUMN_NAME '
. 'FROM information_schema.STATISTICS '
. 'WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? '
. 'ORDER BY INDEX_NAME, SEQ_IN_INDEX',
[$database, $table]
);
$found = [];
foreach ($rows as $row) {
$name = (string) ($row->INDEX_NAME
?? $row->index_name ?? '');
if ($name === '') {
continue;
}
if (!isset($found[$name])) {
$found[$name] = [
'non_unique' => (int) ($row->NON_UNIQUE
?? $row->non_unique ?? -1),
'columns' => [],
];
}
$found[$name]['columns'][] = (string) ($row->COLUMN_NAME
?? $row->column_name ?? '');
}
foreach ($indexes as $name => $shape) {
if (!isset($found[$name])
|| $found[$name]['non_unique']
!== (int) $shape['non_unique']
|| $found[$name]['columns'] !== $shape['columns']) {
throw new \RuntimeException(
'云数据库索引结构校验失败,请使用全新数据库:'
. $table . '.' . $name
);
}
}
}
}
private static function schemaMarker(): string
{
return self::SCHEMA_VERSION . ':' . hash('sha256',
self::schema('cloud.sql') . "\0" . self::schema('cloud_log.sql')
);
}
/** Hash the actual columns, indexes and constraints of both cloud schemas. */
private static function installedSchemaFingerprint($connection,
$logConnection): string
{
return hash('sha256', json_encode([
'main' => self::schemaObjects($connection, self::MAIN_TABLES),
'log' => self::schemaObjects($logConnection, self::LOG_TABLES),
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE));
}
private static function schemaObjects($connection, array $tables): array
{
$database = (string) $connection->getDatabaseName();
$placeholders = implode(',', array_fill(0, count($tables), '?'));
$bindings = array_merge([$database], $tables);
$queries = [
'tables' => 'SELECT TABLE_NAME, ENGINE, TABLE_COLLATION '
. 'FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? '
. 'AND TABLE_NAME IN (' . $placeholders . ') '
. 'ORDER BY TABLE_NAME',
'columns' => 'SELECT TABLE_NAME, ORDINAL_POSITION, COLUMN_NAME, '
. 'COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, EXTRA, '
. 'COLLATION_NAME FROM information_schema.COLUMNS '
. 'WHERE TABLE_SCHEMA = ? AND TABLE_NAME IN ('
. $placeholders . ') ORDER BY TABLE_NAME, ORDINAL_POSITION',
'indexes' => 'SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, '
. 'SEQ_IN_INDEX, COLUMN_NAME, SUB_PART, INDEX_TYPE '
. 'FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = ? '
. 'AND TABLE_NAME IN (' . $placeholders . ') '
. 'ORDER BY TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX',
'constraints' => 'SELECT tc.TABLE_NAME, tc.CONSTRAINT_NAME, '
. 'tc.CONSTRAINT_TYPE, cc.CHECK_CLAUSE '
. 'FROM information_schema.TABLE_CONSTRAINTS tc LEFT 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 IN ('
. $placeholders . ') ORDER BY tc.TABLE_NAME, '
. 'tc.CONSTRAINT_NAME',
'keys' => 'SELECT TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION, '
. 'COLUMN_NAME, REFERENCED_TABLE_NAME, '
. 'REFERENCED_COLUMN_NAME FROM '
. 'information_schema.KEY_COLUMN_USAGE '
. 'WHERE TABLE_SCHEMA = ? AND TABLE_NAME IN ('
. $placeholders . ') ORDER BY TABLE_NAME, CONSTRAINT_NAME, '
. 'ORDINAL_POSITION',
'referential' => 'SELECT TABLE_NAME, CONSTRAINT_NAME, '
. 'UNIQUE_CONSTRAINT_SCHEMA, UNIQUE_CONSTRAINT_NAME, '
. 'MATCH_OPTION, UPDATE_RULE, DELETE_RULE, '
. 'REFERENCED_TABLE_NAME FROM '
. 'information_schema.REFERENTIAL_CONSTRAINTS '
. 'WHERE CONSTRAINT_SCHEMA = ? AND TABLE_NAME IN ('
. $placeholders . ') ORDER BY TABLE_NAME, CONSTRAINT_NAME',
];
$objects = [];
foreach ($queries as $name => $sql) {
$objects[$name] = array_map(static function ($row): array {
$value = (array) $row;
ksort($value, SORT_STRING);
return $value;
}, $connection->select($sql, $bindings));
}
return $objects;
}
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;
}
}