@secondlayer/shared 17.0.0 → 18.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/archive/range-digest.d.ts +2 -65
- package/dist/src/archive/semantic-digest-builder.d.ts +2 -65
- package/dist/src/coverage/index.d.ts +12 -76
- package/dist/src/coverage/index.js +19 -60
- package/dist/src/coverage/index.js.map +3 -3
- package/dist/src/db/index.d.ts +3 -111
- package/dist/src/db/index.js +1 -34
- package/dist/src/db/index.js.map +5 -5
- package/dist/src/db/queries/chain-reorgs.d.ts +2 -65
- package/dist/src/db/queries/chain-reorgs.js +1 -34
- package/dist/src/db/queries/chain-reorgs.js.map +5 -5
- package/dist/src/db/queries/contracts.d.ts +2 -65
- package/dist/src/db/queries/contracts.js.map +2 -2
- package/dist/src/db/queries/instance.d.ts +2 -65
- package/dist/src/db/queries/integrity.d.ts +2 -65
- package/dist/src/db/queries/subgraph-gaps.d.ts +2 -65
- package/dist/src/db/queries/subgraph-operations.d.ts +2 -65
- package/dist/src/db/queries/subgraphs.d.ts +2 -65
- package/dist/src/db/queries/sync-scope.d.ts +2 -65
- package/dist/src/db/queries/webhook-replays.d.ts +2 -65
- package/dist/src/db/queries/webhooks.d.ts +2 -65
- package/dist/src/db/schema.d.ts +3 -64
- package/dist/src/db/schema.js.map +2 -2
- package/dist/src/index.d.ts +20 -111
- package/dist/src/index.js +51 -40
- package/dist/src/index.js.map +9 -7
- package/dist/src/insights/index.d.ts +2 -65
- package/dist/src/leader.js +1 -34
- package/dist/src/leader.js.map +5 -5
- package/dist/src/node/local-client.d.ts +2 -65
- package/dist/src/node/tx-summary.d.ts +7 -1
- package/dist/src/node/tx-summary.js +17 -2
- package/dist/src/node/tx-summary.js.map +3 -3
- package/dist/src/queue/listener.js +1 -34
- package/dist/src/queue/listener.js.map +5 -5
- package/dist/src/schemas/index.js +28 -7
- package/dist/src/schemas/index.js.map +5 -4
- package/dist/src/schemas/webhooks.js +28 -7
- package/dist/src/schemas/webhooks.js.map +5 -4
- package/dist/src/streams-rows/index.d.ts +35 -12
- package/dist/src/streams-rows/index.js +25 -21
- package/dist/src/streams-rows/index.js.map +5 -5
- package/migrations/0207_bns_buy_listing_price.ts +39 -0
- package/migrations/0208_pox5_calculate_rewards_cycle.ts +31 -0
- package/migrations/0209_chain_reorgs_index_block_hash.ts +51 -0
- package/migrations/0210_mempool_evict_superseded_nonces.ts +71 -0
- package/migrations/0211_decoded_events_slim.ts +114 -0
- package/migrations/0212_events_transactions_redundant_indexes.ts +71 -0
- package/migrations/0213_drop_sbtc_token_events.ts +32 -0
- package/migrations/0214_drop_coverage_receipts.ts +24 -0
- package/migrations/0215_decoder_checkpoints_last_output_at.ts +33 -0
- package/package.json +1 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// `calculate-rewards` prints its reward cycle as `stx-cycle`, but the decoder
|
|
5
|
+
// left `reward_cycle` null, so `?reward_cycle=` never found those rows. The
|
|
6
|
+
// decoder now promotes it; fill history from the stored `data` (a few dozen
|
|
7
|
+
// rows). Skipped when the table is absent.
|
|
8
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
9
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
10
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
11
|
+
`.execute(db);
|
|
12
|
+
return rows[0]?.exists === true;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
16
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
17
|
+
await onChainPlane(async () => {
|
|
18
|
+
if (!(await tableExists(db, "pox5_events"))) return;
|
|
19
|
+
await sql`
|
|
20
|
+
UPDATE pox5_events
|
|
21
|
+
SET reward_cycle = (data->>'stx-cycle')::integer
|
|
22
|
+
WHERE topic = 'calculate-rewards'
|
|
23
|
+
AND reward_cycle IS NULL
|
|
24
|
+
AND data->>'stx-cycle' ~ '^[0-9]{1,9}$'
|
|
25
|
+
`.execute(db);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function down(): Promise<void> {
|
|
30
|
+
// Data-only fill of values the decoder now writes; nothing to undo.
|
|
31
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// `chain_reorgs.old_index_block_hash` / `new_index_block_hash` held the Stacks
|
|
5
|
+
// block hash (`blocks.hash`), not the index block hash they promise: every row
|
|
6
|
+
// written before this migration did. Rewrite each side to the index hash of the
|
|
7
|
+
// block it names: from `blocks` when that block still holds the fork height,
|
|
8
|
+
// else from a staged `pending_fork_blocks` payload. A side neither holds goes
|
|
9
|
+
// null (unknown) rather than keep a hash of the wrong kind. A side that already
|
|
10
|
+
// names an index hash at its height (written by the fixed indexer) is left
|
|
11
|
+
// alone. Skipped when a table is absent.
|
|
12
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
13
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
14
|
+
SELECT to_regclass(${table}) IS NOT NULL AS exists
|
|
15
|
+
`.execute(db);
|
|
16
|
+
return rows[0]?.exists === true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
20
|
+
await onChainPlane(async () => {
|
|
21
|
+
for (const table of ["chain_reorgs", "blocks", "pending_fork_blocks"]) {
|
|
22
|
+
if (!(await tableExists(db, table))) return;
|
|
23
|
+
}
|
|
24
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
25
|
+
for (const side of ["old", "new"] as const) {
|
|
26
|
+
const column = sql.ref(`${side}_index_block_hash`);
|
|
27
|
+
const named = sql.ref(`r.${side}_index_block_hash`);
|
|
28
|
+
await sql`
|
|
29
|
+
UPDATE chain_reorgs r
|
|
30
|
+
SET ${column} = COALESCE(
|
|
31
|
+
(SELECT b.index_block_hash FROM blocks b
|
|
32
|
+
WHERE b.height = r.fork_point_height AND b.hash = ${named}),
|
|
33
|
+
(SELECT NULLIF(p.payload->>'index_block_hash', '')
|
|
34
|
+
FROM pending_fork_blocks p
|
|
35
|
+
WHERE p.height = r.fork_point_height AND p.block_hash = ${named}
|
|
36
|
+
LIMIT 1))
|
|
37
|
+
WHERE ${named} IS NOT NULL
|
|
38
|
+
AND NOT EXISTS (SELECT 1 FROM blocks b
|
|
39
|
+
WHERE b.height = r.fork_point_height
|
|
40
|
+
AND b.index_block_hash = ${named})
|
|
41
|
+
AND NOT EXISTS (SELECT 1 FROM pending_fork_blocks p
|
|
42
|
+
WHERE p.height = r.fork_point_height
|
|
43
|
+
AND p.payload->>'index_block_hash' = ${named})
|
|
44
|
+
`.execute(db);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function down(): Promise<void> {
|
|
50
|
+
// Data repair: the block hashes it replaced were wrong, nothing to restore.
|
|
51
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Kysely, type RawBuilder, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// Block ingest now evicts a pending tx once a confirmed tx from its sender
|
|
5
|
+
// spends its nonce. Rows already in `mempool_transactions` predate that and
|
|
6
|
+
// most can never confirm (prod: 173 of 207), so clear them once: a pending tx
|
|
7
|
+
// whose origin nonce is at or below a nonce its sender confirmed is dead.
|
|
8
|
+
//
|
|
9
|
+
// The node admitted each pending tx with a live nonce, so the tx that spent it
|
|
10
|
+
// landed after the oldest receipt: only blocks since then are scanned (an hour
|
|
11
|
+
// of slack, within the last 100k heights), never a sender's whole history.
|
|
12
|
+
// Skipped when a table is absent.
|
|
13
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
14
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
15
|
+
SELECT to_regclass(${table}) IS NOT NULL AS exists
|
|
16
|
+
`.execute(db);
|
|
17
|
+
return rows[0]?.exists === true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// SIP-005: the origin nonce is the 8 bytes after version(1) chain_id(4)
|
|
21
|
+
// auth_type(1) hash_mode(1) signer(20), for standard (0x04) and sponsored
|
|
22
|
+
// (0x05) auth alike. Same layout `rawTxNonce` reads.
|
|
23
|
+
function hexOf(column: RawBuilder<unknown>) {
|
|
24
|
+
return sql`regexp_replace(${column}, '^0x', '')`;
|
|
25
|
+
}
|
|
26
|
+
function hasNonce(column: RawBuilder<unknown>) {
|
|
27
|
+
return sql`(substr(${hexOf(column)}, 11, 2) IN ('04', '05')
|
|
28
|
+
AND substr(${hexOf(column)}, 55, 16) ~ '^[0-9a-fA-F]{16}$')`;
|
|
29
|
+
}
|
|
30
|
+
function nonceOf(column: RawBuilder<unknown>) {
|
|
31
|
+
return sql`('x' || substr(${hexOf(column)}, 55, 16))::bit(64)::bigint`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
35
|
+
await onChainPlane(async () => {
|
|
36
|
+
for (const table of ["mempool_transactions", "transactions", "blocks"]) {
|
|
37
|
+
if (!(await tableExists(db, table))) return;
|
|
38
|
+
}
|
|
39
|
+
const { rows } = await sql<{ floor: string | number | null }>`
|
|
40
|
+
SELECT min(height) AS floor
|
|
41
|
+
FROM blocks
|
|
42
|
+
WHERE height > (SELECT max(height) - 100000 FROM blocks)
|
|
43
|
+
AND timestamp >= (
|
|
44
|
+
SELECT extract(epoch FROM min(received_at) - interval '1 hour')
|
|
45
|
+
FROM mempool_transactions)
|
|
46
|
+
`.execute(db);
|
|
47
|
+
if (rows[0]?.floor == null) return;
|
|
48
|
+
const floor = Number(rows[0].floor);
|
|
49
|
+
const pending = sql.ref("m.raw_tx");
|
|
50
|
+
const confirmed = sql.ref("t.raw_tx");
|
|
51
|
+
await sql`
|
|
52
|
+
DELETE FROM mempool_transactions m
|
|
53
|
+
USING (
|
|
54
|
+
SELECT t.sender, max(${nonceOf(confirmed)}) AS spent
|
|
55
|
+
FROM transactions t
|
|
56
|
+
JOIN blocks b ON b.height = t.block_height AND b.canonical
|
|
57
|
+
WHERE t.block_height >= ${floor}
|
|
58
|
+
AND t.sender IN (SELECT sender FROM mempool_transactions)
|
|
59
|
+
AND ${hasNonce(confirmed)}
|
|
60
|
+
GROUP BY t.sender
|
|
61
|
+
) c
|
|
62
|
+
WHERE m.sender = c.sender
|
|
63
|
+
AND ${hasNonce(pending)}
|
|
64
|
+
AND ${nonceOf(pending)} <= c.spent
|
|
65
|
+
`.execute(db);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function down(): Promise<void> {
|
|
70
|
+
// Data cleanup: the evicted txs could never confirm, nothing to restore.
|
|
71
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Slim `decoded_events` (chain plane / SOURCE).
|
|
6
|
+
*
|
|
7
|
+
* Drops what no query uses: the `(tx_id, event_index)` and bare `event_type`
|
|
8
|
+
* indexes (every event_type read is served by a composite), and the
|
|
9
|
+
* `source_cursor` (always equal to `cursor`) and `microblock_hash` (never
|
|
10
|
+
* written) columns. Both are metadata-only on any table size; the deploy
|
|
11
|
+
* stops lock holders before migrating.
|
|
12
|
+
*
|
|
13
|
+
* The contract/sender/recipient composites become partial on their own
|
|
14
|
+
* column being NOT NULL: equality and IN filters imply it, so every reader
|
|
15
|
+
* still uses them, and rows without the column (prints have no sender) stop
|
|
16
|
+
* costing index space. Rebuilding them is a full index build, so it only
|
|
17
|
+
* runs inline while the table is small (fresh installs, and the v2 database
|
|
18
|
+
* before its decoder rebuild). On a large table it skips with a NOTICE and
|
|
19
|
+
* the old full indexes stay until the table is rebuilt.
|
|
20
|
+
*
|
|
21
|
+
* `payload` new values compress with lz4 (metadata-only; existing rows keep
|
|
22
|
+
* pglz until rewritten).
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const INLINE_BUILD_MAX_BYTES = 1024 ** 3; // 1 GiB
|
|
26
|
+
// column -> index name (the contract index predates the `<column>_` naming)
|
|
27
|
+
const FILTER_INDEXES = [
|
|
28
|
+
["contract_id", "decoded_events_contract_height_event_idx"],
|
|
29
|
+
["sender", "decoded_events_sender_height_event_idx"],
|
|
30
|
+
["recipient", "decoded_events_recipient_height_event_idx"],
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
export function shouldRebuildFilterIndexes(bytes: number): boolean {
|
|
34
|
+
return bytes <= INLINE_BUILD_MAX_BYTES;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
38
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
39
|
+
SELECT to_regclass(${table}) IS NOT NULL AS exists
|
|
40
|
+
`.execute(db);
|
|
41
|
+
return rows[0]?.exists === true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
45
|
+
await onChainPlane(async () => {
|
|
46
|
+
if (!(await tableExists(db, "decoded_events"))) return;
|
|
47
|
+
|
|
48
|
+
await sql`DROP INDEX IF EXISTS decoded_events_tx_id_event_index_idx`.execute(
|
|
49
|
+
db,
|
|
50
|
+
);
|
|
51
|
+
await sql`DROP INDEX IF EXISTS decoded_events_event_type_idx`.execute(db);
|
|
52
|
+
await sql`
|
|
53
|
+
ALTER TABLE decoded_events
|
|
54
|
+
DROP COLUMN IF EXISTS source_cursor,
|
|
55
|
+
DROP COLUMN IF EXISTS microblock_hash
|
|
56
|
+
`.execute(db);
|
|
57
|
+
await sql`ALTER TABLE decoded_events ALTER COLUMN payload SET COMPRESSION lz4`.execute(
|
|
58
|
+
db,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const size = await sql<{ bytes: string }>`
|
|
62
|
+
SELECT pg_total_relation_size('decoded_events')::text AS bytes
|
|
63
|
+
`.execute(db);
|
|
64
|
+
const bytes = Number(size.rows[0]?.bytes ?? 0);
|
|
65
|
+
if (!shouldRebuildFilterIndexes(bytes)) {
|
|
66
|
+
await sql`DO $$ BEGIN RAISE NOTICE ${sql.lit(
|
|
67
|
+
`decoded_events partial filter indexes SKIPPED: table is ${bytes} bytes — they become partial when the table is rebuilt (see this migration's header).`,
|
|
68
|
+
)}; END $$`.execute(db);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const [column, name] of FILTER_INDEXES) {
|
|
72
|
+
const index = sql.ref(name);
|
|
73
|
+
const col = sql.ref(column);
|
|
74
|
+
await sql`DROP INDEX IF EXISTS ${index}`.execute(db);
|
|
75
|
+
await sql`
|
|
76
|
+
CREATE INDEX ${index} ON decoded_events (${col}, block_height, event_index)
|
|
77
|
+
WHERE ${col} IS NOT NULL
|
|
78
|
+
`.execute(db);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
84
|
+
await onChainPlane(async () => {
|
|
85
|
+
if (!(await tableExists(db, "decoded_events"))) return;
|
|
86
|
+
await sql`
|
|
87
|
+
ALTER TABLE decoded_events
|
|
88
|
+
ADD COLUMN IF NOT EXISTS source_cursor TEXT,
|
|
89
|
+
ADD COLUMN IF NOT EXISTS microblock_hash TEXT
|
|
90
|
+
`.execute(db);
|
|
91
|
+
await sql`UPDATE decoded_events SET source_cursor = cursor WHERE source_cursor IS NULL`.execute(
|
|
92
|
+
db,
|
|
93
|
+
);
|
|
94
|
+
await sql`ALTER TABLE decoded_events ALTER COLUMN source_cursor SET NOT NULL`.execute(
|
|
95
|
+
db,
|
|
96
|
+
);
|
|
97
|
+
await sql`ALTER TABLE decoded_events ALTER COLUMN payload SET COMPRESSION pglz`.execute(
|
|
98
|
+
db,
|
|
99
|
+
);
|
|
100
|
+
await sql`CREATE INDEX IF NOT EXISTS decoded_events_event_type_idx ON decoded_events (event_type)`.execute(
|
|
101
|
+
db,
|
|
102
|
+
);
|
|
103
|
+
await sql`CREATE INDEX IF NOT EXISTS decoded_events_tx_id_event_index_idx ON decoded_events (tx_id, event_index)`.execute(
|
|
104
|
+
db,
|
|
105
|
+
);
|
|
106
|
+
for (const [column, name] of FILTER_INDEXES) {
|
|
107
|
+
const index = sql.ref(name);
|
|
108
|
+
await sql`DROP INDEX IF EXISTS ${index}`.execute(db);
|
|
109
|
+
await sql`CREATE INDEX ${index} ON decoded_events (${sql.ref(column)}, block_height, event_index)`.execute(
|
|
110
|
+
db,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Drop raw-table indexes another index already covers (chain plane / SOURCE).
|
|
6
|
+
*
|
|
7
|
+
* - `events_type_idx`: no query filters `events` by type alone; every type
|
|
8
|
+
* predicate rides a block_height range (`events_height_type_idx`).
|
|
9
|
+
* - `events_block_height_idx`: `events_height_type_idx` and
|
|
10
|
+
* `events_logical_id_uniq` both lead with block_height.
|
|
11
|
+
* - `transactions_block_height_idx`: `transactions_height_tx_index_uniq`
|
|
12
|
+
* leads with block_height. That one is built by hand on large tables (0199
|
|
13
|
+
* skips them), so this drop only runs where it exists and is valid.
|
|
14
|
+
*
|
|
15
|
+
* Each drop is guarded on its covering index being present and valid; a
|
|
16
|
+
* skipped drop raises a NOTICE. Plain DROP INDEX is metadata-only; the deploy
|
|
17
|
+
* stops lock holders before migrating.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
async function validIndex(db: Kysely<unknown>, name: string): Promise<boolean> {
|
|
21
|
+
const { rows } = await sql<{ ok: boolean }>`
|
|
22
|
+
SELECT i.indisvalid AND i.indisready AS ok
|
|
23
|
+
FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid
|
|
24
|
+
WHERE c.relname = ${name}
|
|
25
|
+
`.execute(db);
|
|
26
|
+
return rows[0]?.ok === true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function dropIfCovered(
|
|
30
|
+
db: Kysely<unknown>,
|
|
31
|
+
index: string,
|
|
32
|
+
coveredBy: readonly string[],
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
for (const cover of coveredBy) {
|
|
35
|
+
if (await validIndex(db, cover)) {
|
|
36
|
+
await sql`DROP INDEX IF EXISTS ${sql.ref(index)}`.execute(db);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
await sql`DO $$ BEGIN RAISE NOTICE ${sql.lit(
|
|
41
|
+
`${index} kept: none of ${coveredBy.join(", ")} exists and is valid — rerun this drop by hand once it does.`,
|
|
42
|
+
)}; END $$`.execute(db);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
46
|
+
await onChainPlane(async () => {
|
|
47
|
+
await sql`DROP INDEX IF EXISTS events_type_idx`.execute(db);
|
|
48
|
+
await dropIfCovered(db, "events_block_height_idx", [
|
|
49
|
+
"events_height_type_idx",
|
|
50
|
+
"events_logical_id_uniq",
|
|
51
|
+
]);
|
|
52
|
+
await dropIfCovered(db, "transactions_block_height_idx", [
|
|
53
|
+
"transactions_height_tx_index_uniq",
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
59
|
+
await onChainPlane(async () => {
|
|
60
|
+
// Inline builds: fine on dev/test; on a large table build them CONCURRENTLY by hand first.
|
|
61
|
+
await sql`CREATE INDEX IF NOT EXISTS events_type_idx ON events (type)`.execute(
|
|
62
|
+
db,
|
|
63
|
+
);
|
|
64
|
+
await sql`CREATE INDEX IF NOT EXISTS events_block_height_idx ON events (block_height)`.execute(
|
|
65
|
+
db,
|
|
66
|
+
);
|
|
67
|
+
await sql`CREATE INDEX IF NOT EXISTS transactions_block_height_idx ON transactions (block_height)`.execute(
|
|
68
|
+
db,
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// `sbtc_token_events` has no reader: its API went away with the datasets
|
|
5
|
+
// product surface. Every row duplicates a `decoded_events` ft_transfer /
|
|
6
|
+
// ft_mint / ft_burn row for the sBTC asset (the classic decoders write those
|
|
7
|
+
// for every contract), and the one extra column, `memo`, is the next print
|
|
8
|
+
// event, which `decoded_events` also holds. Chain-plane only; each step is
|
|
9
|
+
// skipped when its table is absent.
|
|
10
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
11
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
12
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
13
|
+
`.execute(db);
|
|
14
|
+
return rows[0]?.exists === true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
18
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
19
|
+
await onChainPlane(async () => {
|
|
20
|
+
if (await tableExists(db, "sbtc_token_events")) {
|
|
21
|
+
await sql`DROP TABLE IF EXISTS sbtc_token_events`.execute(db);
|
|
22
|
+
}
|
|
23
|
+
if (await tableExists(db, "decoder_checkpoints")) {
|
|
24
|
+
await sql`DELETE FROM decoder_checkpoints WHERE decoder_name IN ('decode.sbtc_token.v1', 'backfill.sbtc_token')`.execute(
|
|
25
|
+
db,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The rows can be rebuilt from `decoded_events` or Streams if ever needed.
|
|
32
|
+
export async function down(_db: Kysely<unknown>): Promise<void> {}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// Per-block coverage receipts were written on every decoder commit but never
|
|
5
|
+
// read, and nothing ever wrote `coverage_segments` or `stage_runs`.
|
|
6
|
+
// `stage_failures` and `stage_registry` stay: they are the durable fault
|
|
7
|
+
// record. Chain-plane only; every step is a no-op when its table is absent.
|
|
8
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
9
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
10
|
+
await onChainPlane(async () => {
|
|
11
|
+
// Dropping run_id also drops its foreign key to stage_runs.
|
|
12
|
+
await sql`ALTER TABLE IF EXISTS stage_failures DROP COLUMN IF EXISTS run_id`.execute(
|
|
13
|
+
db,
|
|
14
|
+
);
|
|
15
|
+
// Receipts reference stage_runs, so they go before it.
|
|
16
|
+
await sql`DROP TABLE IF EXISTS stage_block_receipts`.execute(db);
|
|
17
|
+
await sql`DROP TABLE IF EXISTS coverage_segments`.execute(db);
|
|
18
|
+
await sql`DROP TABLE IF EXISTS stage_runs`.execute(db);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// No-op: the receipts were never read. 0116_coverage_schema documents the
|
|
23
|
+
// original DDL.
|
|
24
|
+
export async function down(_db: Kysely<unknown>): Promise<void> {}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
// Decoder health now reads `decoder_checkpoints.last_output_at`, stamped in the
|
|
5
|
+
// commit transaction when a commit wrote rows. The composite index only served
|
|
6
|
+
// the old per-table "latest created_at" health query (see 0080), and cost a
|
|
7
|
+
// write on every decoded row. Chain-plane only; no-ops when a table is absent.
|
|
8
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
9
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
10
|
+
await onChainPlane(async () => {
|
|
11
|
+
// Nullable, no default: metadata-only change.
|
|
12
|
+
await sql`ALTER TABLE IF EXISTS decoder_checkpoints ADD COLUMN IF NOT EXISTS last_output_at TIMESTAMPTZ`.execute(
|
|
13
|
+
db,
|
|
14
|
+
);
|
|
15
|
+
await sql`DROP INDEX IF EXISTS decoded_events_evt_canon_created_idx`.execute(
|
|
16
|
+
db,
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
22
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
23
|
+
await onChainPlane(async () => {
|
|
24
|
+
await sql`ALTER TABLE IF EXISTS decoder_checkpoints DROP COLUMN IF EXISTS last_output_at`.execute(
|
|
25
|
+
db,
|
|
26
|
+
);
|
|
27
|
+
await sql`DO $$ BEGIN
|
|
28
|
+
IF to_regclass('public.decoded_events') IS NOT NULL THEN
|
|
29
|
+
CREATE INDEX IF NOT EXISTS decoded_events_evt_canon_created_idx ON decoded_events (event_type, canonical, created_at DESC);
|
|
30
|
+
END IF;
|
|
31
|
+
END $$`.execute(db);
|
|
32
|
+
});
|
|
33
|
+
}
|