-- Add operator-managed order status to app_calls.
-- Compatible with MySQL/MariaDB without `ADD COLUMN IF NOT EXISTS`.

SET @db := DATABASE();

SET @sql := IF(
    (SELECT COUNT(*) FROM information_schema.COLUMNS
     WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'app_calls' AND COLUMN_NAME = 'order_status') = 0,
    'ALTER TABLE app_calls ADD COLUMN order_status VARCHAR(32) NOT NULL DEFAULT ''none'' AFTER operator_note',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := IF(
    (SELECT COUNT(*) FROM information_schema.STATISTICS
     WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'app_calls' AND INDEX_NAME = 'idx_order_status') = 0,
    'ALTER TABLE app_calls ADD KEY idx_order_status (order_status)',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
