-- Migration: Add categories array to announcements
-- Date: 2026-01-29
-- Description: Dodanie obsługi wielu kategorii dla aktualności

-- 1. Dodaj nową kolumnę categories jako tablica
ALTER TABLE announcements
ADD COLUMN IF NOT EXISTS categories TEXT[] DEFAULT '{}';

-- 2. Migracja danych z category do categories
UPDATE announcements
SET categories = ARRAY[category]
WHERE categories IS NULL OR categories = '{}';

-- 3. Mapowanie starych wartości na nowe
UPDATE announcements
SET categories = ARRAY['internal']
WHERE 'general' = ANY(categories) OR 'member_news' = ANY(categories);

UPDATE announcements
SET categories = array_remove(categories, 'general');

UPDATE announcements
SET categories = array_remove(categories, 'member_news');

-- 4. Indeks dla szybszego wyszukiwania po kategorii
CREATE INDEX IF NOT EXISTS idx_announcements_categories
ON announcements USING GIN(categories);

-- 5. Nadaj uprawnienia
GRANT ALL ON TABLE announcements TO nordabiz_app;
