SET @col_exists := (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'credit_cards'
    AND COLUMN_NAME = 'holder_user_id'
);

SET @ddl := IF(
  @col_exists = 0,
  'ALTER TABLE credit_cards ADD COLUMN holder_user_id BIGINT NULL AFTER card_network',
  'SELECT ''holder_user_id exists'' '
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @idx_exists := (
  SELECT COUNT(*)
  FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'credit_cards'
    AND INDEX_NAME = 'idx_credit_cards_family_holder'
);
SET @idx_ddl := IF(
  @idx_exists = 0,
  'CREATE INDEX idx_credit_cards_family_holder ON credit_cards (family_id, holder_user_id)',
  'SELECT ''idx_credit_cards_family_holder exists'' '
);
PREPARE idx_stmt FROM @idx_ddl;
EXECUTE idx_stmt;
DEALLOCATE PREPARE idx_stmt;
