-- DentalCare v0.5.9.8.59 reset/layout/form compatibility repair
-- Adds queue in-house provider display compatibility for fresh reset installs and old copies.

ALTER TABLE `{{prefix}}queue_entries`
  ADD COLUMN IF NOT EXISTS `inhouse_provider_name` VARCHAR(150) NULL AFTER `inhouse_provider_user_id`;

UPDATE `{{prefix}}queue_entries` q
LEFT JOIN `{{prefix}}users` u ON u.id = q.inhouse_provider_user_id
SET q.inhouse_provider_name = COALESCE(NULLIF(q.inhouse_provider_name,''), u.name)
WHERE q.inhouse_provider_user_id IS NOT NULL
  AND (q.inhouse_provider_name IS NULL OR q.inhouse_provider_name = '');
