-- site_pages.locale: aynı path için dil bazlı içerik (varsayılan '' = TR kök)
USE faunamix;

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

SET @sql = IF(
  @col_exists = 0,
  "ALTER TABLE site_pages ADD COLUMN locale VARCHAR(16) NOT NULL DEFAULT '' AFTER path",
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @old_uq = (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'site_pages' AND INDEX_NAME = 'uq_site_pages_path'
);

SET @sql = IF(
  @old_uq > 0,
  'ALTER TABLE site_pages DROP INDEX uq_site_pages_path',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @new_uq = (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'site_pages' AND INDEX_NAME = 'uq_site_pages_locale_path'
);

SET @sql = IF(
  @new_uq = 0,
  'ALTER TABLE site_pages ADD UNIQUE KEY uq_site_pages_locale_path (locale, path)',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
