-- Add optional name field to ICAO table.

SET @has_table := (
  SELECT COUNT(*)
  FROM information_schema.tables
  WHERE table_schema = DATABASE()
    AND table_name = 'icaos'
);

SET @has_column := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'icaos'
    AND column_name = 'name'
);

SET @sql := IF(
  @has_table > 0 AND @has_column = 0,
  'ALTER TABLE icaos ADD COLUMN name VARCHAR(128) NULL AFTER icao_code',
  'SELECT 1'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
