-- Fresh-install cloud business tables. PHP is the sole writer of these records. CREATE TABLE IF NOT EXISTS cloud_hosts ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(80) NOT NULL, region VARCHAR(64) NOT NULL DEFAULT '', enabled TINYINT(1) NOT NULL DEFAULT 0, default_network VARCHAR(128) NOT NULL DEFAULT 'default', created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_instances ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, uuid CHAR(36) NOT NULL, host_id BIGINT UNSIGNED NOT NULL, user_id BIGINT UNSIGNED NOT NULL, order_id BIGINT UNSIGNED DEFAULT NULL, plan_id BIGINT UNSIGNED DEFAULT NULL, current_period_id BIGINT UNSIGNED DEFAULT NULL, name VARCHAR(63) NOT NULL, status VARCHAR(32) NOT NULL DEFAULT 'active', service_status VARCHAR(32) NOT NULL DEFAULT 'exempt', network_status VARCHAR(32) NOT NULL DEFAULT 'active', expires_at BIGINT UNSIGNED DEFAULT NULL, renewal_price DECIMAL(12,2) DEFAULT NULL, renewal_days INT UNSIGNED NOT NULL DEFAULT 0, renewal_traffic_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, traffic_quota_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, traffic_download_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, traffic_upload_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, lifetime_download_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, lifetime_upload_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, traffic_synced_at BIGINT UNSIGNED NOT NULL DEFAULT 0, suspended_at BIGINT UNSIGNED DEFAULT NULL, network_suspended_at BIGINT UNSIGNED DEFAULT NULL, spec_json LONGTEXT NOT NULL, credential_cipher LONGTEXT DEFAULT NULL, created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL, UNIQUE KEY uniq_cloud_host_vm (host_id, uuid), UNIQUE KEY uniq_cloud_instance_order (order_id), KEY cloud_instance_owner (user_id, status), KEY cloud_instance_lifecycle (service_status, expires_at, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_orders ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, plan_id BIGINT UNSIGNED NOT NULL, host_id BIGINT UNSIGNED NOT NULL, idempotency_key VARCHAR(128) NOT NULL, request_hash CHAR(64) NOT NULL, amount DECIMAL(12,2) NOT NULL, status VARCHAR(32) NOT NULL DEFAULT 'queued', spec_json LONGTEXT NOT NULL, instance_id BIGINT UNSIGNED DEFAULT NULL, refunded_at BIGINT DEFAULT NULL, created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL, UNIQUE KEY uniq_cloud_order_request (user_id, idempotency_key), KEY cloud_order_owner (user_id, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_service_periods ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, instance_id BIGINT UNSIGNED NOT NULL, user_id BIGINT UNSIGNED NOT NULL, plan_id BIGINT UNSIGNED DEFAULT NULL, order_id BIGINT UNSIGNED DEFAULT NULL, renewal_id BIGINT UNSIGNED DEFAULT NULL, starts_at BIGINT UNSIGNED NOT NULL, ends_at BIGINT UNSIGNED NOT NULL, traffic_quota_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, download_base_bytes BIGINT UNSIGNED DEFAULT NULL, upload_base_bytes BIGINT UNSIGNED DEFAULT NULL, download_used_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, upload_used_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, status VARCHAR(16) NOT NULL DEFAULT 'scheduled', activated_at BIGINT UNSIGNED DEFAULT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY uniq_cloud_period_order (order_id), UNIQUE KEY uniq_cloud_period_renewal (renewal_id), KEY cloud_period_timeline (instance_id, starts_at, ends_at), KEY cloud_period_activation (status, starts_at, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_renewals ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, instance_id BIGINT UNSIGNED NOT NULL, user_id BIGINT UNSIGNED NOT NULL, plan_id BIGINT UNSIGNED DEFAULT NULL, period_id BIGINT UNSIGNED DEFAULT NULL, idempotency_key VARCHAR(128) NOT NULL, request_hash CHAR(64) NOT NULL, amount DECIMAL(12,2) NOT NULL, validity_days INT UNSIGNED NOT NULL, traffic_bytes_added BIGINT UNSIGNED NOT NULL DEFAULT 0, previous_expires_at BIGINT UNSIGNED NOT NULL, new_expires_at BIGINT UNSIGNED NOT NULL, created_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY uniq_cloud_renewal_request (user_id, idempotency_key), KEY cloud_renewal_instance (instance_id, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_jobs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, operation_id CHAR(32) NOT NULL, order_id BIGINT UNSIGNED DEFAULT NULL, instance_id BIGINT UNSIGNED DEFAULT NULL, user_id BIGINT UNSIGNED NOT NULL, host_id BIGINT UNSIGNED NOT NULL, kind VARCHAR(32) NOT NULL, status VARCHAR(32) NOT NULL DEFAULT 'queued', payload_cipher LONGTEXT NOT NULL, attempts INT NOT NULL DEFAULT 0, dispatched_at BIGINT DEFAULT NULL, lease_token CHAR(32) DEFAULT NULL, lease_until BIGINT NOT NULL DEFAULT 0, available_at BIGINT NOT NULL, error_code VARCHAR(128) DEFAULT NULL, result_json LONGTEXT DEFAULT NULL, created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL, UNIQUE KEY uniq_cloud_operation (operation_id), UNIQUE KEY uniq_cloud_provision_order (order_id), KEY cloud_job_poll (status, available_at, lease_until), KEY cloud_job_instance_state (instance_id, status, id), KEY cloud_job_owner (user_id, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_ssh_keys ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, name VARCHAR(80) NOT NULL, public_key TEXT NOT NULL, fingerprint VARCHAR(80) NOT NULL, created_at BIGINT NOT NULL, UNIQUE KEY uniq_cloud_user_key (user_id, fingerprint) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS cloud_sessions ( id CHAR(64) NOT NULL PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, version BIGINT NOT NULL, fingerprint CHAR(64) NOT NULL, auth_driver VARCHAR(16) NOT NULL, auth_reference TEXT DEFAULT NULL, active TINYINT(1) NOT NULL DEFAULT 1, expires_at BIGINT NOT NULL, created_at BIGINT NOT NULL, UNIQUE KEY uniq_cloud_session_fingerprint (fingerprint), KEY cloud_session_owner (user_id, active) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;