Skip to content

Commit

Permalink
Backup batches on fork id update (#128)
Browse files Browse the repository at this point in the history
* Add batch backup

* Unit test

* Update the diff

* Apply feedback

Co-authored-by: Goran Rojovic <[email protected]>

---------

Co-authored-by: bros <[email protected]>
Co-authored-by: Goran Rojovic <[email protected]>
  • Loading branch information
3 people authored Apr 16, 2024
1 parent 4bb2522 commit 85b880f
Show file tree
Hide file tree
Showing 4 changed files with 43,677 additions and 30,855 deletions.
32 changes: 32 additions & 0 deletions db/migrations/state/validium-001.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- +migrate Up

CREATE TABLE IF NOT EXISTS state.batch_data_backup
(
batch_num BIGINT,
data BYTEA,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (batch_num, created_at)
);

-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION backup_batch() RETURNS trigger AS $$
BEGIN
INSERT INTO state.batch_data_backup (batch_num, data)
VALUES (OLD.batch_num, OLD.raw_txs_data)
ON CONFLICT (batch_num, created_at) DO UPDATE SET
data = EXCLUDED.data;
RETURN OLD;
END;
$$
LANGUAGE plpgsql;
-- +migrate StatementEnd

CREATE TRIGGER backup_batch
BEFORE DELETE ON state.batch FOR EACH ROW
EXECUTE PROCEDURE backup_batch();

-- +migrate Down

DROP TRIGGER IF EXISTS backup_batch ON state.batch;
DROP FUNCTION IF EXISTS backup_batch();
DROP TABLE IF EXISTS state.batch_data_backup;
Loading

0 comments on commit 85b880f

Please sign in to comment.