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

47 lines
1.5 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.

-- Исправление триггера update_last_active для работы с messages и swipes
-- Проблема: в messages есть sender_id, а в swipes есть user_id
-- Удаляем старые триггеры
DROP TRIGGER IF EXISTS update_last_active_on_message ON messages;
DROP TRIGGER IF EXISTS update_last_active_on_swipe ON swipes;
DROP FUNCTION IF EXISTS update_last_active();
-- Создаём функцию для обновления last_active для отправителя сообщения
CREATE OR REPLACE FUNCTION update_last_active_on_message()
RETURNS TRIGGER AS $$
BEGIN
UPDATE profiles
SET last_active = CURRENT_TIMESTAMP
WHERE user_id = NEW.sender_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Создаём функцию для обновления last_active при свайпе
CREATE OR REPLACE FUNCTION update_last_active_on_swipe()
RETURNS TRIGGER AS $$
BEGIN
UPDATE profiles
SET last_active = CURRENT_TIMESTAMP
WHERE user_id = NEW.user_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Создаём триггер для messages
CREATE TRIGGER update_last_active_on_message
AFTER INSERT ON messages
FOR EACH ROW
EXECUTE FUNCTION update_last_active_on_message();
-- Создаём триггер для swipes
CREATE TRIGGER update_last_active_on_swipe
AFTER INSERT ON swipes
FOR EACH ROW
EXECUTE FUNCTION update_last_active_on_swipe();
-- Проверка
SELECT 'Triggers update_last_active fixed successfully' AS status;