-- 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 NOT NULL, name VARCHAR(63) NOT NULL, status VARCHAR(32) NOT NULL DEFAULT 'active', 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) ) 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_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_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;