-- Migration: Add 'stopped' status to server_orders table -- Date: 2026-01-29 -- Description: SQLite does not support ALTER TABLE to modify CHECK constraints, -- so we need to recreate the table with the new constraint. -- Step 1: Create temporary table with new CHECK constraint CREATE TABLE server_orders_new ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, spec_id INTEGER NOT NULL, status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending', 'provisioning', 'active', 'stopped', 'failed', 'cancelled', 'terminated')), region TEXT NOT NULL, provider_instance_id TEXT, ip_address TEXT, root_password TEXT, price_paid INTEGER NOT NULL, error_message TEXT, provisioned_at DATETIME, terminated_at DATETIME, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, label TEXT, image TEXT, billing_type TEXT DEFAULT 'monthly', expires_at DATETIME, telegram_user_id TEXT, provider TEXT DEFAULT 'anvil', idempotency_key TEXT, FOREIGN KEY (user_id) REFERENCES users(id) ); -- Step 2: Copy existing data INSERT INTO server_orders_new ( id, user_id, spec_id, status, region, provider_instance_id, ip_address, root_password, price_paid, error_message, provisioned_at, terminated_at, created_at, updated_at, label, image, billing_type, expires_at, telegram_user_id, provider, idempotency_key ) SELECT id, user_id, spec_id, status, region, provider_instance_id, ip_address, root_password, price_paid, error_message, provisioned_at, terminated_at, created_at, updated_at, label, image, billing_type, expires_at, telegram_user_id, provider, idempotency_key FROM server_orders; -- Step 3: Drop old table DROP TABLE server_orders; -- Step 4: Rename new table ALTER TABLE server_orders_new RENAME TO server_orders; -- Step 5: Recreate indexes CREATE INDEX idx_server_orders_user ON server_orders(user_id); CREATE INDEX idx_server_orders_status ON server_orders(status, created_at DESC); CREATE UNIQUE INDEX idx_server_orders_idempotency_unique ON server_orders(idempotency_key) WHERE idempotency_key IS NOT NULL; -- Verification query (comment out for actual migration) -- SELECT COUNT(*) as total_orders, COUNT(DISTINCT status) as status_count FROM server_orders;