-- 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