-- Add latitude and longitude columns to events table for geocoding support
-- Migration: 20251105_add_lat_long_to_events

-- Add lat column if it doesn't exist
SET @dbname = DATABASE();
SET @tablename = "events";
SET @columnname = "lat";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE events ADD COLUMN lat DECIMAL(9, 6) NULL AFTER oneplan_path"
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Add long column if it doesn't exist
SET @columnname = "long";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE events ADD COLUMN `long` DECIMAL(9, 6) NULL AFTER lat"
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;
