SET @add_recurring_ahead_count = (
  SELECT IF(
    EXISTS(
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'recurring_order_templates'
        AND column_name = 'generate_ahead_count'
    ),
    'SELECT 1',
    'ALTER TABLE recurring_order_templates ADD COLUMN generate_ahead_count INT NOT NULL DEFAULT 1 AFTER next_order_date'
  )
);
PREPARE add_recurring_ahead_count_stmt FROM @add_recurring_ahead_count;
EXECUTE add_recurring_ahead_count_stmt;
DEALLOCATE PREPARE add_recurring_ahead_count_stmt;

SET @add_recurring_ahead_unit = (
  SELECT IF(
    EXISTS(
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'recurring_order_templates'
        AND column_name = 'generate_ahead_unit'
    ),
    'SELECT 1',
    'ALTER TABLE recurring_order_templates ADD COLUMN generate_ahead_unit VARCHAR(30) NOT NULL DEFAULT ''month'' AFTER generate_ahead_count'
  )
);
PREPARE add_recurring_ahead_unit_stmt FROM @add_recurring_ahead_unit;
EXECUTE add_recurring_ahead_unit_stmt;
DEALLOCATE PREPARE add_recurring_ahead_unit_stmt;
