-- Apply after 202609240008 and the foundation migration. Retains meeting IDs and bookings.
ALTER TABLE meetings
 ADD COLUMN event_type VARCHAR(30) NOT NULL DEFAULT 'regular' AFTER event_slug,
 ADD COLUMN document_kind VARCHAR(12) NOT NULL DEFAULT 'summons' AFTER summons_url;
UPDATE meetings SET document_kind='invitation'
 WHERE document_kind='summons' AND special_event_type IN ('ladies_evening','other');
UPDATE meetings SET event_type='special' WHERE event_type='regular' AND special_event_enabled=1 AND special_event_type<>'none';
-- Reconcile legacy entries that predated the event slug before adding uniqueness.
UPDATE meetings SET event_slug=CONCAT('event-',id) WHERE event_slug IS NULL OR event_slug='';
-- MySQL/MariaDB compatible conditional index creation (the foundation may already have it).
SET @event_slug_index_exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema=DATABASE() AND table_name='meetings' AND index_name IN ('uq_meeting_unit_slug','uq_meetings_unit_event_slug'));
SET @event_slug_sql = IF(@event_slug_index_exists=0,
 'ALTER TABLE meetings ADD UNIQUE KEY uq_meetings_unit_event_slug (unit_id,event_slug)',
 'SELECT 1');
PREPARE event_slug_index_statement FROM @event_slug_sql;
EXECUTE event_slug_index_statement;
DEALLOCATE PREPARE event_slug_index_statement;
