SET @column_exists := (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'finanto_inss_cases'
      AND COLUMN_NAME = 'selected_rule_code'
);

SET @ddl := IF(
    @column_exists = 0,
    'ALTER TABLE finanto_inss_cases ADD COLUMN selected_rule_code VARCHAR(80) NULL AFTER selected_rule_id',
    'SELECT 1'
);

PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
