Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/deploy/ansible/roles/elasticsearch/templates/reindex_triggers.sql.j2

39 lignes
1,4 Kio
Text
Brut Vue normale Historique

-- Matches the _op_type values from elasticsearch bulk API
-- https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
CREATE TYPE torrents_action AS ENUM ('index', 'delete');
CREATE TABLE IF NOT EXISTS reindex_torrents (
reindex_torrents_id SERIAL,
torrent_id int,
action torrents_action
);
CREATE OR REPLACE FUNCTION add_reindex_torrents_action() RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
INSERT INTO reindex_torrents (torrent_id, action) VALUES (NEW.torrent_id, 'index');
RETURN NEW;
ELSIF (TG_OP = 'UPDATE') THEN
IF (NEW.deleted_at IS NOT NULL) THEN
INSERT INTO reindex_torrents (torrent_id, action) VALUES (OLD.torrent_id, 'delete');
RETURN NEW;
ELSE
INSERT INTO reindex_torrents (torrent_id, action) VALUES (NEW.torrent_id, 'index');
RETURN NEW;
END IF;
ELSIF (TG_OP = 'DELETE') THEN
INSERT INTO reindex_torrents (torrent_id, action) VALUES (OLD.torrent_id, 'delete');
RETURN OLD;
END IF;
RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trigger_reindex_torrents ON {{ nyaapantsu_torrent_tablename }};
CREATE TRIGGER trigger_reindex_torrents
AFTER INSERT OR UPDATE OR DELETE ON {{ nyaapantsu_torrent_tablename }}
FOR EACH ROW EXECUTE PROCEDURE add_reindex_torrents_action();
-- vim: ft=sql