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/sql/cloud.sql

243 lines
12 KiB
MySQL

-- 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 '',
country_code CHAR(2) 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_template_retirements (
template_id VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'deleting',
template_created_at BIGINT UNSIGNED NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
KEY cloud_template_retirement_state (state, updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE cloud_template_retirements ADD COLUMN IF NOT EXISTS
template_created_at BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER state;
CREATE TABLE IF NOT EXISTS cloud_template_catalog_locks (
id TINYINT UNSIGNED NOT NULL PRIMARY KEY,
updated_at BIGINT UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT IGNORE INTO cloud_template_catalog_locks (id, updated_at) VALUES (1, 0);
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
country_code CHAR(2) NOT NULL DEFAULT '' AFTER region;
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,
payload_cipher LONGTEXT DEFAULT NULL,
instance_id BIGINT UNSIGNED DEFAULT NULL,
refunded_at BIGINT DEFAULT NULL,
stock_reserved_at BIGINT UNSIGNED DEFAULT NULL,
stock_released_at BIGINT UNSIGNED 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_invoices (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
invoice_no VARCHAR(40) NOT NULL,
purpose VARCHAR(16) NOT NULL DEFAULT 'renewal',
instance_id BIGINT UNSIGNED DEFAULT NULL,
order_id BIGINT UNSIGNED DEFAULT NULL,
user_id BIGINT UNSIGNED NOT NULL,
plan_id BIGINT UNSIGNED DEFAULT NULL,
renewal_id BIGINT UNSIGNED DEFAULT NULL,
renews_expires_at BIGINT UNSIGNED DEFAULT NULL,
due_at BIGINT UNSIGNED NOT NULL,
amount DECIMAL(12,2) NOT NULL,
validity_days INT UNSIGNED NOT NULL,
traffic_quota_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
status VARCHAR(16) NOT NULL DEFAULT 'open',
paid_via VARCHAR(32) DEFAULT NULL,
payment_reference VARCHAR(80) DEFAULT NULL,
paid_at BIGINT UNSIGNED DEFAULT NULL,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY uniq_cloud_invoice_no (invoice_no),
UNIQUE KEY uniq_cloud_invoice_order (order_id),
KEY cloud_invoice_period (instance_id, renews_expires_at, status),
UNIQUE KEY uniq_cloud_invoice_renewal (renewal_id),
KEY cloud_invoice_owner (user_id, status, due_at),
KEY cloud_invoice_due (status, due_at, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Releases created before void invoices became replaceable used a unique
-- period key. Keep void rows as an audit trail while permitting one corrected
-- replacement bill for the same expiry.
ALTER TABLE cloud_invoices DROP INDEX IF EXISTS uniq_cloud_invoice_period;
ALTER TABLE cloud_invoices ADD INDEX IF NOT EXISTS
cloud_invoice_period (instance_id, renews_expires_at, status);
-- Upgrade an existing installation so unpaid provisioning requests can live
-- behind an invoice without fabricating an instance or expiry timestamp.
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS payload_cipher LONGTEXT NULL AFTER spec_json;
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS stock_reserved_at BIGINT UNSIGNED NULL AFTER refunded_at;
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS stock_released_at BIGINT UNSIGNED NULL AFTER stock_reserved_at;
ALTER TABLE cloud_invoices ADD COLUMN IF NOT EXISTS purpose VARCHAR(16) NOT NULL DEFAULT 'renewal' AFTER invoice_no;
ALTER TABLE cloud_invoices MODIFY instance_id BIGINT UNSIGNED NULL;
ALTER TABLE cloud_invoices ADD COLUMN IF NOT EXISTS order_id BIGINT UNSIGNED NULL AFTER instance_id;
ALTER TABLE cloud_invoices MODIFY renews_expires_at BIGINT UNSIGNED NULL;
ALTER TABLE cloud_invoices ADD UNIQUE INDEX IF NOT EXISTS uniq_cloud_invoice_order (order_id);
CREATE TABLE IF NOT EXISTS cloud_invoice_payments (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
invoice_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
provider VARCHAR(24) NOT NULL,
provider_order_no VARCHAR(64) NOT NULL,
idempotency_key VARCHAR(128) NOT NULL,
amount DECIMAL(12,2) NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'creating',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY uniq_cloud_invoice_provider_order (provider, provider_order_no),
UNIQUE KEY uniq_cloud_invoice_payment_request (user_id, idempotency_key),
KEY cloud_invoice_payment_invoice (invoice_id, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_mail_outbox (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
invoice_id BIGINT UNSIGNED NOT NULL,
kind VARCHAR(32) NOT NULL,
status VARCHAR(16) NOT NULL DEFAULT 'pending',
attempts INT UNSIGNED NOT NULL DEFAULT 0,
available_at BIGINT UNSIGNED NOT NULL,
lease_token CHAR(32) DEFAULT NULL,
lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0,
sent_at BIGINT UNSIGNED DEFAULT NULL,
last_error VARCHAR(500) NOT NULL DEFAULT '',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY uniq_cloud_invoice_mail (invoice_id, kind),
KEY cloud_mail_delivery (status, available_at, lease_until, 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;