Skip to content

Commit 84e9e02

Browse files
committed
fix comments
1 parent 5a9602d commit 84e9e02

File tree

2 files changed

+68
-49
lines changed

2 files changed

+68
-49
lines changed

db/pgstorage/migrations/0023.sql

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ ON sync.deposit(network_id, deposit_cnt);
2222
CREATE INDEX IF NOT EXISTS idx_deposit_dest_addr
2323
ON sync.deposit(dest_addr);
2424

25-
-- Index for pending deposits query: WHERE dest_net = $1 AND ready_for_claim = true AND ignore = false
26-
-- Supports GetPendingDepositsToClaim with multiple conditions
25+
-- Index for pending deposits query: WHERE dest_net = $1 AND ready_for_claim = true/false AND ignore = false
26+
-- Supports GetPendingDepositsToClaim and UpdateL1DepositsStatus/UpdateL2DepositsStatus queries
27+
-- This composite index covers all query patterns:
28+
-- 1. dest_net = $X AND ready_for_claim = true AND ignore = false (GetPendingDepositsToClaim)
29+
-- 2. dest_net = $X AND ready_for_claim = false (UpdateL1/L2DepositsStatus)
2730
CREATE INDEX IF NOT EXISTS idx_deposit_dest_net_ready_ignore
2831
ON sync.deposit(dest_net, ready_for_claim, ignore);
2932

30-
-- Index for deposits ready to claim: WHERE ready_for_claim = true AND dest_net = $1
31-
-- Supports UpdateL1DepositsStatus and UpdateL2DepositsStatus queries
32-
CREATE INDEX IF NOT EXISTS idx_deposit_ready_dest_net
33-
ON sync.deposit(ready_for_claim, dest_net) WHERE ready_for_claim = true;
34-
3533
-- Index for deposit metadata lookups: WHERE network_id = $1 AND orig_addr = $2 AND dest_net = $3
3634
-- Used in GetTokenMetadata
3735
CREATE INDEX IF NOT EXISTS idx_deposit_metadata_lookup
@@ -316,7 +314,6 @@ DROP INDEX IF EXISTS sync.idx_deposit_network_id_block_id;
316314
DROP INDEX IF EXISTS sync.idx_deposit_network_id_deposit_cnt;
317315
DROP INDEX IF EXISTS sync.idx_deposit_dest_addr;
318316
DROP INDEX IF EXISTS sync.idx_deposit_dest_net_ready_ignore;
319-
DROP INDEX IF EXISTS sync.idx_deposit_ready_dest_net;
320317
DROP INDEX IF EXISTS sync.idx_deposit_metadata_lookup;
321318
DROP INDEX IF EXISTS sync.idx_deposit_l2_claim_status;
322319
DROP INDEX IF EXISTS sync.idx_deposit_id_network_dest;

db/pgstorage/migrations/0023_test.go

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ func (m migrationTest0023) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
3232
tableName string
3333
indexName string
3434
}{
35-
// sync.deposit indexes
35+
// sync.deposit indexes (8 indexes)
3636
{"sync", "deposit", "idx_deposit_network_id_block_id"},
3737
{"sync", "deposit", "idx_deposit_network_id_deposit_cnt"},
3838
{"sync", "deposit", "idx_deposit_dest_addr"},
3939
{"sync", "deposit", "idx_deposit_dest_net_ready_ignore"},
40-
{"sync", "deposit", "idx_deposit_ready_dest_net"},
4140
{"sync", "deposit", "idx_deposit_metadata_lookup"},
4241
{"sync", "deposit", "idx_deposit_l2_claim_status"},
4342
{"sync", "deposit", "idx_deposit_id_network_dest"},
@@ -119,44 +118,7 @@ func (m migrationTest0023) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
119118
assert.True(t, indexExists, "Index %s.%s.%s should exist after migration up", idx.schema, idx.tableName, idx.indexName)
120119
}
121120

122-
// Verify that redundant indexes do NOT exist
123-
redundantIndexes := []struct {
124-
schema string
125-
tableName string
126-
indexName string
127-
}{
128-
{"sync", "claim", "idx_claim_network_id"}, // Removed as redundant
129-
{"sync", "block", "idx_block_block_hash"}, // Removed as covered by UNIQUE constraint
130-
{"mt", "rollup_exit", "idx_rollup_exit_root_rollup"}, // Removed as duplicate
131-
}
132-
133-
for _, idx := range redundantIndexes {
134-
var indexExists bool
135-
checkIndex := `SELECT EXISTS (
136-
SELECT 1 FROM pg_indexes
137-
WHERE schemaname = $1
138-
AND tablename = $2
139-
AND indexname = $3
140-
)`
141-
err := db.QueryRow(checkIndex, idx.schema, idx.tableName, idx.indexName).Scan(&indexExists)
142-
assert.NoError(t, err, "Error checking index %s.%s.%s", idx.schema, idx.tableName, idx.indexName)
143-
assert.False(t, indexExists, "Redundant index %s.%s.%s should NOT exist after migration up", idx.schema, idx.tableName, idx.indexName)
144-
}
145-
146121
// Verify specific index properties
147-
// Test partial index on sync.deposit with WHERE clause
148-
var hasWhereClause bool
149-
checkPartialIndex := `SELECT EXISTS (
150-
SELECT 1 FROM pg_indexes
151-
WHERE schemaname = 'sync'
152-
AND tablename = 'deposit'
153-
AND indexname = 'idx_deposit_ready_dest_net'
154-
AND indexdef LIKE '%WHERE%ready_for_claim = true%'
155-
)`
156-
err := db.QueryRow(checkPartialIndex).Scan(&hasWhereClause)
157-
assert.NoError(t, err)
158-
assert.True(t, hasWhereClause, "Partial index idx_deposit_ready_dest_net should have WHERE clause")
159-
160122
// Test GIN index on sync.exit_root
161123
var isGinIndex bool
162124
checkGinIndex := `SELECT EXISTS (
@@ -166,7 +128,7 @@ func (m migrationTest0023) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
166128
AND indexname = 'idx_exit_root_exit_roots_array'
167129
AND indexdef LIKE '%USING gin%'
168130
)`
169-
err = db.QueryRow(checkGinIndex).Scan(&isGinIndex)
131+
err := db.QueryRow(checkGinIndex).Scan(&isGinIndex)
170132
assert.NoError(t, err)
171133
assert.True(t, isGinIndex, "Index idx_exit_root_exit_roots_array should be a GIN index")
172134

@@ -184,25 +146,85 @@ func (m migrationTest0023) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
184146
}
185147

186148
func (m migrationTest0023) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
187-
// List of all indexes that should NOT exist after migration down
149+
// List of ALL indexes that should NOT exist after migration down
150+
// This must match exactly the indexes created in the UP migration
188151
indexesToCheck := []struct {
189152
schema string
190153
tableName string
191154
indexName string
192155
}{
156+
// sync.deposit indexes (8 indexes)
193157
{"sync", "deposit", "idx_deposit_network_id_block_id"},
194158
{"sync", "deposit", "idx_deposit_network_id_deposit_cnt"},
159+
{"sync", "deposit", "idx_deposit_dest_addr"},
160+
{"sync", "deposit", "idx_deposit_dest_net_ready_ignore"},
161+
{"sync", "deposit", "idx_deposit_metadata_lookup"},
162+
{"sync", "deposit", "idx_deposit_l2_claim_status"},
163+
{"sync", "deposit", "idx_deposit_id_network_dest"},
164+
{"sync", "deposit", "idx_deposit_orig_addr"},
165+
166+
// sync.claim indexes (5 indexes)
195167
{"sync", "claim", "idx_claim_mainnet_network"},
168+
{"sync", "claim", "idx_claim_rollup_network"},
169+
{"sync", "claim", "idx_claim_dest_addr"},
170+
{"sync", "claim", "idx_claim_network_id_index"},
171+
{"sync", "claim", "idx_claim_global_index_network"},
172+
173+
// sync.block indexes (2 indexes)
196174
{"sync", "block", "idx_block_network_id_block_num_desc"},
175+
{"sync", "block", "idx_block_num_network_id"},
176+
177+
// sync.exit_root indexes (7 indexes)
197178
{"sync", "exit_root", "idx_exit_root_allowed_block_id_network"},
179+
{"sync", "exit_root", "idx_exit_root_network_allowed_id_desc"},
180+
{"sync", "exit_root", "idx_exit_root_ger_lookup"},
181+
{"sync", "exit_root", "idx_exit_root_l2_ger"},
182+
{"sync", "exit_root", "idx_exit_root_exit_roots_array"},
183+
{"sync", "exit_root", "idx_exit_root_network_id_desc"},
184+
{"sync", "exit_root", "idx_exit_root_ger_network"},
185+
186+
// sync.token_wrapped indexes (1 index)
187+
{"sync", "token_wrapped", "idx_token_wrapped_orig_net_addr"},
188+
189+
// mt.root indexes (2 indexes)
198190
{"mt", "root", "idx_root_deposit_id_network"},
191+
{"mt", "root", "idx_root_network"},
192+
193+
// mt.rollup_exit indexes (3 indexes)
199194
{"mt", "rollup_exit", "idx_rollup_exit_root_rollup_id"},
195+
{"mt", "rollup_exit", "idx_rollup_exit_root"},
196+
{"mt", "rollup_exit", "idx_rollup_exit_rollup_id_id"},
197+
198+
// sync.monitored_txs indexes (3 indexes)
200199
{"sync", "monitored_txs", "idx_monitored_txs_deposit_id"},
200+
{"sync", "monitored_txs", "idx_monitored_txs_status_created"},
201+
{"sync", "monitored_txs", "idx_monitored_txs_group_id"},
202+
203+
// sync.remove_exit_root indexes (2 indexes)
204+
{"sync", "remove_exit_root", "idx_remove_exit_root_ger_network"},
205+
{"sync", "remove_exit_root", "idx_remove_exit_root_block_id"},
206+
207+
// sync.backward_let indexes (3 indexes)
201208
{"sync", "backward_let", "idx_backward_let_new_root"},
209+
{"sync", "backward_let", "idx_backward_let_previous_root"},
210+
{"sync", "backward_let", "idx_backward_let_block_id"},
211+
212+
// sync.forward_let indexes (3 indexes)
202213
{"sync", "forward_let", "idx_forward_let_new_root"},
214+
{"sync", "forward_let", "idx_forward_let_previous_root"},
215+
{"sync", "forward_let", "idx_forward_let_block_id"},
216+
217+
// sync.set_unset_claim indexes (4 indexes)
203218
{"sync", "set_unset_claim", "idx_set_unset_claim_index_rollup"},
219+
{"sync", "set_unset_claim", "idx_set_unset_claim_global_index"},
220+
{"sync", "set_unset_claim", "idx_set_unset_claim_type"},
221+
{"sync", "set_unset_claim", "idx_set_unset_claim_block_id"},
222+
223+
// sync.deposit_backup indexes (2 indexes)
204224
{"sync", "deposit_backup", "idx_deposit_backup_deposit_cnt"},
225+
{"sync", "deposit_backup", "idx_deposit_backup_network_id"},
205226
}
227+
// Total: 45 indexes
206228

207229
for _, idx := range indexesToCheck {
208230
var indexExists bool

0 commit comments

Comments
 (0)