Files
tg_tinder_bot/sql/fix_notify_message_trigger.sql
2025-11-06 15:09:15 +09:00

50 lines
1.7 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Исправление триггера notify_new_message для использования правильной схемы notifications
-- Проблема: триггер использует content и reference_id вместо data (jsonb)
-- Удаляем старый триггер и функцию
DROP TRIGGER IF EXISTS notify_new_message_trigger ON messages;
DROP FUNCTION IF EXISTS notify_new_message();
-- Создаём новую функцию с правильной схемой
CREATE OR REPLACE FUNCTION notify_new_message()
RETURNS TRIGGER AS $$
DECLARE
recipient_id UUID;
BEGIN
-- Определяем получателя сообщения (второго участника матча)
SELECT CASE
WHEN m.user_id_1 = NEW.sender_id THEN m.user_id_2
ELSE m.user_id_1
END INTO recipient_id
FROM matches m
WHERE m.id = NEW.match_id;
-- Создаём уведомление с правильной структурой (data jsonb)
IF recipient_id IS NOT NULL THEN
INSERT INTO notifications (user_id, type, data, created_at)
VALUES (
recipient_id,
'new_message',
jsonb_build_object(
'message_id', NEW.id,
'match_id', NEW.match_id,
'sender_id', NEW.sender_id,
'content_preview', LEFT(NEW.content, 50)
),
NOW()
);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Создаём триггер заново
CREATE TRIGGER notify_new_message_trigger
AFTER INSERT ON messages
FOR EACH ROW
EXECUTE FUNCTION notify_new_message();
-- Проверка
SELECT 'Trigger notify_new_message fixed successfully' AS status;