-
Notifications
You must be signed in to change notification settings - Fork 173
[backport release-mainnet-1.2.0-rc] Hash id migration #4384
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
base: release-mainnet-1.2.0-rc
Are you sure you want to change the base?
Changes from all commits
9132801
b0ed7c8
e99ee1c
dca3635
dfbbc86
89ede6d
2c2934e
5c02df3
63c1e6e
038de43
910f7fe
2bea604
ac322b1
945f808
402ad04
784cf19
4049af6
bbcd29b
6d794d7
a8f43c8
f3db054
27e5cad
ef8dc5d
a823f07
2cda1a3
9d6763c
5bfb3d7
5ef0bf9
6a54800
424e78f
e6020be
8647739
9461a0f
68adf63
f0d0519
2c1f93a
dc85dab
6df71ba
d4974cf
63c74fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| -- Expand phase: create new BIGINT-keyed tables alongside the existing ones. | ||
| -- Old tables (hash, fee_merkle_tree, block_merkle_tree) are left untouched and | ||
| -- serve as the read fallback during the backfill window. | ||
| -- The contract phase (dropping old tables and renaming *_bigint to canonical names) | ||
| -- is a separate follow-up migration. | ||
|
|
||
| -- Drop reward merkle tree tables — unused and always empty across all deployments. | ||
| DROP TABLE reward_merkle_tree; | ||
| DROP TABLE reward_merkle_tree_v2; | ||
|
|
||
| -- New hash table with BIGSERIAL. | ||
| CREATE TABLE hash_bigint ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| value BYTEA NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| -- Seed the sequence above the old max so new auto-IDs never collide | ||
| -- with rows we backfill from hash (which preserve their original INT ids). | ||
| SELECT setval( | ||
| pg_get_serial_sequence('hash_bigint', 'id'), | ||
| GREATEST(COALESCE((SELECT MAX(id) FROM hash), 1), 1) | ||
| ); | ||
|
|
||
| -- New Merkle-tree tables with BIGINT FK. | ||
| CREATE TABLE fee_merkle_tree_bigint ( | ||
| path JSONB NOT NULL, | ||
| created BIGINT NOT NULL, | ||
| hash_id BIGINT NOT NULL REFERENCES hash_bigint(id), | ||
| children JSONB, | ||
| children_bitvec BIT VARYING, | ||
| idx JSONB, | ||
| entry JSONB, | ||
| PRIMARY KEY (path, created) | ||
| ); | ||
| CREATE INDEX fee_merkle_tree_bigint_created ON fee_merkle_tree_bigint (created); | ||
|
|
||
| CREATE TABLE block_merkle_tree_bigint ( | ||
| path JSONB NOT NULL, | ||
| created BIGINT NOT NULL, | ||
| hash_id BIGINT NOT NULL REFERENCES hash_bigint(id), | ||
| children JSONB, | ||
| children_bitvec BIT VARYING, | ||
| idx JSONB, | ||
| entry JSONB, | ||
| PRIMARY KEY (path, created) | ||
| ); | ||
| CREATE INDEX block_merkle_tree_bigint_created ON block_merkle_tree_bigint (created); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| -- Progress-tracking table for background DataBackfill migrations. | ||
| CREATE TABLE deferred_migrations ( | ||
| name TEXT PRIMARY KEY, | ||
| started_at TIMESTAMPTZ NOT NULL, | ||
| completed_at TIMESTAMPTZ, | ||
| error TEXT, | ||
|
Comment on lines
+4
to
+6
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The If keeping it: writing the error in // After logging the error, persist it:
sqlx::query("UPDATE deferred_migrations SET error = $1 WHERE name = $2")
.bind(format!("{e:#}"))
.bind(name)
.execute(db.pool()) // or open a new transaction
... |
||
| last_offset BIGINT | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| -- Rename tables to *_bigint for consistency with the postgres schema. | ||
| -- SQLite integers are already 64-bit so this is a naming-only change. | ||
| ALTER TABLE hash RENAME TO hash_bigint; | ||
| ALTER TABLE fee_merkle_tree RENAME TO fee_merkle_tree_bigint; | ||
| ALTER TABLE block_merkle_tree RENAME TO block_merkle_tree_bigint; | ||
|
|
||
| -- Drop reward merkle tree tables — unused and always empty across all deployments. | ||
| DROP TABLE IF EXISTS reward_merkle_tree; | ||
| DROP TABLE IF EXISTS reward_merkle_tree_v2; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| -- Progress-tracking table for background DataBackfill migrations. | ||
| CREATE TABLE deferred_migrations ( | ||
| name TEXT PRIMARY KEY, | ||
| started_at TEXT NOT NULL, | ||
| completed_at TEXT, | ||
| error TEXT, | ||
| last_offset INTEGER | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor:
DROP TABLEwithoutIF EXISTScould fail on fresh or partially-migrated databases.If for any reason a node's database doesn't have these tables (e.g., a schema was manually modified or an earlier migration was partially applied), this migration will fail hard. Using
DROP TABLE IF EXISTSwould be more defensive. That said, since these tables are created by earlier numbered migrations that must have run before V1302, the practical risk is low.