Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New indexes + tests #515

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions db/pgstorage/migrations/0002_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func (m migrationTest0002) InsertData(db *sql.DB) error {
return nil
}

var indexes = []string{"root_network_idx", "deposit_idx", "block_idx", "root_idx", "exit_roots_idx"}

func (m migrationTest0002) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Insert a new root
const addRoot = "INSERT INTO mt.root (root, deposit_cnt, network) VALUES ($1, $2, $3) RETURNING id"
Expand All @@ -52,6 +50,7 @@ func (m migrationTest0002) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
}, rootID)
assert.NoError(t, err)

indexes := []string{"root_network_idx", "deposit_idx", "block_idx", "root_idx", "exit_roots_idx"}
// Check indexes adding
for _, idx := range indexes {
// getIndex
Expand All @@ -78,6 +77,7 @@ func (m migrationTest0002) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
_, err = db.Exec(addOldRoot, common.FromHex("0xf4418588ed35a2458cffeb39b93d26f18d2ab13bdce6aee58e7b99359ec2dfd9"), 2, 0)
assert.NoError(t, err)

indexes := []string{"root_network_idx", "deposit_idx", "block_idx", "root_idx", "exit_roots_idx"}
// Check indexes removing
for _, idx := range indexes {
// getIndex
Expand Down
10 changes: 10 additions & 0 deletions db/pgstorage/migrations/0006.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ ALTER TABLE mt.root ADD primary key(deposit_id);
ALTER TABLE sync.token_wrapped ADD primary key(block_id);
DROP TABLE IF EXISTS mt.rht_temp;

CREATE INDEX IF NOT EXISTS claim_block_id ON sync.claim USING btree (block_id);
CREATE INDEX IF NOT EXISTS deposit_block_id ON sync.deposit USING btree (block_id);
CREATE INDEX IF NOT EXISTS monitored_txs_block_id ON sync.monitored_txs USING btree (block_id);
CREATE INDEX IF NOT EXISTS token_wrapped_block_id ON sync.token_wrapped USING btree (block_id);

-- +migrate Down
ALTER TABLE mt.rht DROP CONSTRAINT rht_pkey;
ALTER TABLE mt.root DROP CONSTRAINT root_pkey;
ALTER TABLE sync.token_wrapped DROP CONSTRAINT token_wrapped_pkey;
CREATE TABLE IF NOT EXISTS mt.rht_temp AS (SELECT key, min(value), max(deposit_id) FROM mt.rht GROUP BY key HAVING count(key) > 1);

DROP INDEX IF EXISTS sync.claim_block_id;
DROP INDEX IF EXISTS sync.deposit_block_id;
DROP INDEX IF EXISTS sync.monitored_txs_block_id;
DROP INDEX IF EXISTS sync.token_wrapped_block_id;
22 changes: 22 additions & 0 deletions db/pgstorage/migrations/0006_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ func (m migrationTest0006) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
row4 := db.QueryRow(rhtTemp)
var count4 int
assert.Error(t, row4.Scan(&count4))

indexes := []string{"claim_block_id", "deposit_block_id", "monitored_txs_block_id", "token_wrapped_block_id"}
// Check indexes adding
for _, idx := range indexes {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)
}
}

func (m migrationTest0006) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
Expand Down Expand Up @@ -66,6 +77,17 @@ func (m migrationTest0006) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
var count4 int
assert.NoError(t, row4.Scan(&count4))
assert.Equal(t, 0, count4)

indexes := []string{"claim_block_id", "deposit_block_id", "monitored_txs_block_id", "token_wrapped_block_id"}
// Check indexes removing
for _, idx := range indexes {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}
}

func TestMigration0006(t *testing.T) {
Expand Down
Loading