Skip to content

Commit

Permalink
Fix triggers
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Feb 15, 2023
1 parent b0032e9 commit d4415c4
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions core/lib/storage/migrations/2023-01-18-164812_txs_count/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ CREATE TABLE txs_count (
CREATE OR REPLACE FUNCTION decrease_txs_count_for_address() RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO txs_count (address, token, count) VALUES (OLD.address, -1 , 0) ON CONFLICT (address, token) DO NOTHING;
INSERT INTO txs_count (address, token, count) VALUES (OLD.address, OLD.token, 0) ON CONFLICT (address, token) DO NOTHING;

-- Postgresql doesn't support unique indexes for nullable fields, so we have to use
-- artificial token which means no token
UPDATE txs_count
SET count = txs_count.count - CASE WHEN NOT EXISTS(SELECT 1 FROM tx_filters WHERE address = OLD.address AND tx_hash = OLD.tx_hash)
THEN 1 ELSE 0 END
WHERE address=OLD.address AND token=-1;

UPDATE txs_count
SET count = txs_count.count - CASE WHEN NOT EXISTS(SELECT 1 FROM tx_filters WHERE address = OLD.address AND tx_hash = OLD.tx_hash AND token = OLD.token)
THEN 1 ELSE 0 END
WHERE address=OLD.address AND token=OLD.token;
UPDATE txs_count SET count = txs_count.count -
CASE WHEN NOT EXISTS(SELECT 1 FROM tx_filters WHERE address = OLD.address AND tx_hash = OLD.tx_hash FOR UPDATE) THEN 1 ELSE 0 END
WHERE address=OLD.address AND token = -1;

UPDATE txs_count SET count = txs_count.count -
CASE WHEN NOT EXISTS(SELECT 1 FROM tx_filters WHERE address = OLD.address AND tx_hash = OLD.tx_hash AND token = OLD.token) THEN 1 ELSE 0 END
WHERE address=OLD.address AND token=OLD.token;

END IF;
RETURN NULL;
END;
Expand All @@ -37,16 +39,17 @@ BEGIN
*/

IF (TG_OP = 'INSERT') THEN
INSERT INTO txs_count (address, token, count) VALUES (NEW.address, -1 , 0) ON CONFLICT (address, token) DO NOTHING;
INSERT INTO txs_count (address, token, count) VALUES (NEW.address, NEW.token, 0) ON CONFLICT (address, token) DO NOTHING;

-- Postgresql doesn't support unique indexes for nullable fields, so we have to use
-- artificial token which means no token
INSERT INTO txs_count (address, token, count) VALUES (NEW.address, -1, 1)
ON CONFLICT (address, token) DO UPDATE
SET count = txs_count.count + CASE WHEN EXISTS(SELECT 1 FROM tx_filters WHERE address = NEW.address AND tx_hash = NEW.tx_hash) THEN 0 ELSE 1 END;
UPDATE txs_count SET count = txs_count.count +
CASE WHEN EXISTS(SELECT 1 FROM tx_filters WHERE address = NEW.address AND tx_hash = NEW.tx_hash FOR UPDATE) THEN 0 ELSE 1 END
WHERE address = NEW.address AND token = -1;

UPDATE txs_count SET count = txs_count.count +
CASE WHEN EXISTS(SELECT 1 FROM tx_filters WHERE address = NEW.address AND tx_hash = NEW.tx_hash AND token = NEW.token) THEN 0 ELSE 1 END
WHERE address = NEW.address AND token = NEW.token;

INSERT INTO txs_count (address, token, count) VALUES (NEW.address, NEW.token, 1)
ON CONFLICT (address, token) DO UPDATE
SET count = txs_count.count + CASE WHEN EXISTS(SELECT 1 FROM tx_filters WHERE address = NEW.address AND tx_hash = NEW.tx_hash AND token = NEW.token) THEN 0 ELSE 1 END;
END IF;
RETURN NEW;
END;
Expand Down

0 comments on commit d4415c4

Please sign in to comment.