forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2333: Fix triggers r=Deniallugo a=Deniallugo Co-authored-by: Danil <[email protected]>
- Loading branch information
Showing
5 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
core/lib/storage/migrations/2023-01-18-164812_txs_count/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
DROP TRIGGER increase_txs_count_for_address_tr ON tx_filters; | ||
DROP TRIGGER decrease_txs_count_for_address_tr ON tx_filters; | ||
DROP TRIGGER IF EXISTS increase_txs_count_for_address_tr ON tx_filters; | ||
DROP TRIGGER IF EXISTS decrease_txs_count_for_address_tr ON tx_filters; | ||
|
||
DROP FUNCTION increase_txs_count_for_address; | ||
DROP FUNCTION decrease_txs_count_for_address; | ||
DROP FUNCTION IF EXISTS increase_txs_count_for_address; | ||
DROP FUNCTION IF EXISTS decrease_txs_count_for_address; | ||
|
||
DROP TABLE txs_count; |
5 changes: 5 additions & 0 deletions
5
core/lib/storage/migrations/2023-02-21-204507_fix-triggers/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
DROP TRIGGER IF EXISTS increase_txs_count_for_address_tr ON tx_filters; | ||
DROP TRIGGER IF EXISTS decrease_txs_count_for_address_tr ON tx_filters; | ||
|
||
DROP FUNCTION IF EXISTS increase_txs_count_for_address; | ||
DROP FUNCTION IF EXISTS decrease_txs_count_for_address; |
45 changes: 45 additions & 0 deletions
45
core/lib/storage/migrations/2023-02-21-204507_fix-triggers/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
DROP TRIGGER IF EXISTS increase_txs_count_for_address_tr ON tx_filters; | ||
DROP TRIGGER IF EXISTS decrease_txs_count_for_address_tr ON tx_filters; | ||
|
||
CREATE OR REPLACE FUNCTION decrease_txs_count_for_address() RETURNS TRIGGER AS $$ | ||
BEGIN | ||
IF (TG_OP = 'DELETE') THEN | ||
-- 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 (SELECT count(*) = 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 (SELECT count(*) = 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 OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION increase_txs_count_for_address() RETURNS TRIGGER AS $$ | ||
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; | ||
|
||
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; | ||
|
||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER increase_txs_count_for_address_tr BEFORE INSERT ON tx_filters | ||
FOR EACH ROW EXECUTE FUNCTION increase_txs_count_for_address(); | ||
|
||
CREATE TRIGGER decrease_txs_count_for_address_tr BEFORE DELETE ON tx_filters | ||
FOR EACH ROW EXECUTE FUNCTION decrease_txs_count_for_address(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters