-- Stable English keys for website special groups (display title stays free-language)

ALTER TABLE store_specials
  ADD COLUMN IF NOT EXISTS key VARCHAR(100);

UPDATE store_specials
SET key = 'special-' || id::text
WHERE key IS NULL OR BTRIM(key) = '';

ALTER TABLE store_specials
  ALTER COLUMN key SET NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS store_specials_business_id_key_unique
  ON store_specials (business_id, key);

ALTER TABLE website_category_groups
  ADD COLUMN IF NOT EXISTS key VARCHAR(100);

UPDATE website_category_groups
SET key = 'category-group-' || id::text
WHERE key IS NULL OR BTRIM(key) = '';

ALTER TABLE website_category_groups
  ALTER COLUMN key SET NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS website_category_groups_business_id_key_unique
  ON website_category_groups (business_id, key);

ALTER TABLE website_brand_groups
  ADD COLUMN IF NOT EXISTS key VARCHAR(100);

UPDATE website_brand_groups
SET key = 'brand-group-' || id::text
WHERE key IS NULL OR BTRIM(key) = '';

ALTER TABLE website_brand_groups
  ALTER COLUMN key SET NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS website_brand_groups_business_id_key_unique
  ON website_brand_groups (business_id, key);
