From 738ef14ad65732f446e67d1f33345218f304d8be Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Wed, 21 Aug 2024 13:07:20 -0700 Subject: [PATCH] Add UNIQUE INDEXes to replace previous primary keys This was causing duplicate rows to get inserted during a replay instead of it being an UPDATE --- .../migrate/migrations/0248_log_poller_primary_keys.sql | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/store/migrate/migrations/0248_log_poller_primary_keys.sql b/core/store/migrate/migrations/0248_log_poller_primary_keys.sql index b12f2b9c143..ba56991f980 100644 --- a/core/store/migrate/migrations/0248_log_poller_primary_keys.sql +++ b/core/store/migrate/migrations/0248_log_poller_primary_keys.sql @@ -2,12 +2,20 @@ ALTER TABLE evm.logs DROP CONSTRAINT logs_pkey; ALTER TABLE evm.logs ADD COLUMN id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY; +CREATE UNIQUE INDEX idx_evm_logs_block_hash_log_index_evm_chain_id ON evm.logs (block_hash, log_index, evm_chain_id); ALTER TABLE evm.log_poller_blocks DROP CONSTRAINT log_poller_blocks_pkey; ALTER TABLE evm.log_poller_blocks ADD COLUMN id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY; +CREATE UNIQUE INDEX idx_evm_log_poller_blocks_block_number_evm_chain_id ON evm.log_poller_blocks (block_number, evm_chain_id); + +-- TODO: restore UNIQUE constratint on original pkey! -- +goose Down +DROP INDEX IF EXISTS evm.idx_evm_log_poller_blocks_block_number_evm_chain_id; ALTER TABLE evm.log_poller_blocks DROP COLUMN id; ALTER TABLE evm.log_poller_blocks ADD PRIMARY KEY (block_number, evm_chain_id); +DROP INDEX IF EXISTS evm.idx_evm_logs_block_hash_log_index_evm_chain_id; ALTER TABLE evm.logs DROP COLUMN id; ALTER TABLE evm.logs ADD PRIMARY KEY (block_hash, log_index, evm_chain_id); + +