@secondlayer/shared 15.2.0 → 15.3.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 +61 -9
- package/dist/src/archive/semantic-digest-builder.d.ts +61 -9
- package/dist/src/coverage/index.d.ts +64 -9
- package/dist/src/coverage/index.js.map +2 -2
- package/dist/src/db/index.d.ts +63 -10
- package/dist/src/db/index.js +19 -1
- package/dist/src/db/index.js.map +5 -4
- package/dist/src/db/queries/chain-reorgs.d.ts +61 -9
- package/dist/src/db/queries/chain-reorgs.js +17 -1
- package/dist/src/db/queries/chain-reorgs.js.map +5 -4
- package/dist/src/db/queries/contracts.d.ts +108 -28
- package/dist/src/db/queries/contracts.js +115 -15
- package/dist/src/db/queries/contracts.js.map +7 -5
- package/dist/src/db/queries/instance.d.ts +69 -10
- package/dist/src/db/queries/instance.js +147 -3
- package/dist/src/db/queries/instance.js.map +6 -4
- package/dist/src/db/queries/integrity.d.ts +77 -10
- package/dist/src/db/queries/integrity.js +236 -2
- package/dist/src/db/queries/integrity.js.map +7 -4
- package/dist/src/db/queries/subgraph-gaps.d.ts +69 -12
- package/dist/src/db/queries/subgraph-gaps.js +7 -3
- package/dist/src/db/queries/subgraph-gaps.js.map +3 -3
- package/dist/src/db/queries/subgraph-operations.d.ts +61 -9
- package/dist/src/db/queries/subgraphs.d.ts +122 -42
- package/dist/src/db/queries/subgraphs.js +85 -27
- package/dist/src/db/queries/subgraphs.js.map +7 -6
- package/dist/src/db/queries/sync-scope.d.ts +61 -9
- package/dist/src/db/queries/webhook-replays.d.ts +73 -13
- package/dist/src/db/queries/webhook-replays.js +468 -19
- package/dist/src/db/queries/webhook-replays.js.map +9 -4
- package/dist/src/db/queries/webhooks.d.ts +96 -21
- package/dist/src/db/queries/webhooks.js +72 -17
- package/dist/src/db/queries/webhooks.js.map +6 -5
- package/dist/src/db/schema.d.ts +62 -10
- package/dist/src/db/schema.js +20 -1
- package/dist/src/db/schema.js.map +4 -3
- package/dist/src/db/transition.d.ts +46 -0
- package/dist/src/db/transition.js +38 -0
- package/dist/src/db/transition.js.map +10 -0
- package/dist/src/email/index.d.ts +16 -1
- package/dist/src/email/index.js +186 -130
- package/dist/src/email/index.js.map +5 -4
- package/dist/src/errors.d.ts +7 -1
- package/dist/src/errors.js +12 -1
- package/dist/src/errors.js.map +3 -3
- package/dist/src/index-http.d.ts +19 -10
- package/dist/src/index-http.js +5 -4
- package/dist/src/index-http.js.map +3 -3
- package/dist/src/index.d.ts +98 -19
- package/dist/src/index.js +41 -8
- package/dist/src/index.js.map +8 -7
- package/dist/src/insights/index.d.ts +62 -9
- package/dist/src/insights/index.js +55 -5
- package/dist/src/insights/index.js.map +7 -6
- package/dist/src/node/local-client.d.ts +61 -9
- package/dist/src/queue/listener.js +17 -1
- package/dist/src/queue/listener.js.map +5 -4
- package/dist/src/runtime/index.d.ts +0 -4
- package/dist/src/runtime/index.js +54 -4
- package/dist/src/runtime/index.js.map +6 -5
- package/dist/src/schemas/index.d.ts +36 -10
- package/dist/src/schemas/index.js +12 -8
- package/dist/src/schemas/index.js.map +4 -4
- package/dist/src/schemas/subgraphs.d.ts +22 -9
- package/dist/src/schemas/subgraphs.js +10 -6
- package/dist/src/schemas/subgraphs.js.map +3 -3
- package/dist/src/schemas/webhooks.d.ts +15 -2
- package/dist/src/schemas/webhooks.js +2 -2
- package/dist/src/schemas/webhooks.js.map +3 -3
- package/dist/src/subgraphs/spec.d.ts +5 -1
- package/dist/src/webhooks/health.d.ts +27 -0
- package/dist/src/webhooks/health.js +84 -0
- package/dist/src/webhooks/health.js.map +11 -0
- package/migrations/0184_status_checks.ts +92 -0
- package/migrations/0185_reorg_generation_settlement_seq.ts +89 -0
- package/migrations/0186_api_keys_one_active_tenant_per_name.ts +43 -0
- package/migrations/0187_accounts_email_lower_unique.ts +27 -0
- package/migrations/0188_webhook_reasons.ts +124 -0
- package/migrations/0189_subgraph_lifecycle.ts +83 -0
- package/migrations/0190_usage_ledger_debited_usd_micros.ts +59 -0
- package/migrations/0191_indexer_state_machines.ts +83 -0
- package/migrations/0192_hosted_stack_state.ts +37 -0
- package/migrations/0193_accounts_ghost_has_no_email.ts +34 -0
- package/package.json +9 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* One address, one account: `Foo@x.com` and `foo@x.com` collide. Every
|
|
6
|
+
* writer lowercases (`normalizeEmail`); this index is the backstop. The
|
|
7
|
+
* migration runner wraps each file in a transaction, so the index is built
|
|
8
|
+
* plainly rather than CONCURRENTLY. It fails loudly if a database already
|
|
9
|
+
* holds two accounts whose lowercased emails match. Ghost accounts have a
|
|
10
|
+
* NULL email, which a unique index ignores.
|
|
11
|
+
*/
|
|
12
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
13
|
+
await onControlPlane(async () => {
|
|
14
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
15
|
+
await sql`
|
|
16
|
+
CREATE UNIQUE INDEX IF NOT EXISTS accounts_email_lower_idx
|
|
17
|
+
ON accounts (lower(email))
|
|
18
|
+
`.execute(db);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
23
|
+
await onControlPlane(async () => {
|
|
24
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
25
|
+
await sql`DROP INDEX IF EXISTS accounts_email_lower_idx`.execute(db);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Two reasons that used to be inferred from other columns become columns:
|
|
6
|
+
*
|
|
7
|
+
* - `webhooks.pause_reason` (`user` | `circuit`): was `circuit_opened_at IS NOT
|
|
8
|
+
* NULL` on a paused row. A paused webhook always has one; an active one never.
|
|
9
|
+
* The never-written `error` status goes away.
|
|
10
|
+
* - `webhook_outbox.dead_reason` (`exhausted` | `orphaned`): was a prefix match
|
|
11
|
+
* on `last_error`. A dead row always has one; a live one never.
|
|
12
|
+
*
|
|
13
|
+
* Both are backfilled from the old signals before their CHECKs are added.
|
|
14
|
+
* Tables are skipped when absent (not every database carries them). Constraints
|
|
15
|
+
* must match `WebhookStatus`/`WebhookPauseReason` and `OutboxStatus`/`DeadReason`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const ORPHANED_PREFIX = "orphaned by reorg at ";
|
|
19
|
+
|
|
20
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
21
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
22
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
23
|
+
`.execute(db);
|
|
24
|
+
return rows[0]?.exists === true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function addCheck(
|
|
28
|
+
db: Kysely<unknown>,
|
|
29
|
+
table: string,
|
|
30
|
+
name: string,
|
|
31
|
+
expression: string,
|
|
32
|
+
) {
|
|
33
|
+
await sql`ALTER TABLE ${sql.table(table)} DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
34
|
+
db,
|
|
35
|
+
);
|
|
36
|
+
await sql`
|
|
37
|
+
ALTER TABLE ${sql.table(table)}
|
|
38
|
+
ADD CONSTRAINT ${sql.id(name)} CHECK (${sql.raw(expression)}) NOT VALID
|
|
39
|
+
`.execute(db);
|
|
40
|
+
await sql`ALTER TABLE ${sql.table(table)} VALIDATE CONSTRAINT ${sql.id(name)}`.execute(
|
|
41
|
+
db,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
46
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
47
|
+
await onControlPlane(async () => {
|
|
48
|
+
if (await tableExists(db, "webhooks")) {
|
|
49
|
+
await sql`ALTER TABLE webhooks ADD COLUMN IF NOT EXISTS pause_reason text`.execute(
|
|
50
|
+
db,
|
|
51
|
+
);
|
|
52
|
+
await sql`UPDATE webhooks SET status = 'active' WHERE status NOT IN ('active', 'paused')`.execute(
|
|
53
|
+
db,
|
|
54
|
+
);
|
|
55
|
+
await sql`
|
|
56
|
+
UPDATE webhooks
|
|
57
|
+
SET pause_reason = CASE WHEN circuit_opened_at IS NOT NULL THEN 'circuit' ELSE 'user' END
|
|
58
|
+
WHERE status = 'paused' AND pause_reason IS NULL
|
|
59
|
+
`.execute(db);
|
|
60
|
+
await sql`UPDATE webhooks SET pause_reason = NULL WHERE status <> 'paused'`.execute(
|
|
61
|
+
db,
|
|
62
|
+
);
|
|
63
|
+
await addCheck(
|
|
64
|
+
db,
|
|
65
|
+
"webhooks",
|
|
66
|
+
"webhooks_status_check",
|
|
67
|
+
"status IN ('active', 'paused')",
|
|
68
|
+
);
|
|
69
|
+
await addCheck(
|
|
70
|
+
db,
|
|
71
|
+
"webhooks",
|
|
72
|
+
"webhooks_pause_reason_check",
|
|
73
|
+
"(status = 'paused') = (pause_reason IS NOT NULL) AND (pause_reason IS NULL OR pause_reason IN ('user', 'circuit'))",
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (await tableExists(db, "webhook_outbox")) {
|
|
78
|
+
await sql`ALTER TABLE webhook_outbox ADD COLUMN IF NOT EXISTS dead_reason text`.execute(
|
|
79
|
+
db,
|
|
80
|
+
);
|
|
81
|
+
await sql`
|
|
82
|
+
UPDATE webhook_outbox
|
|
83
|
+
SET dead_reason = CASE
|
|
84
|
+
WHEN last_error LIKE ${`${ORPHANED_PREFIX}%`} THEN 'orphaned'
|
|
85
|
+
ELSE 'exhausted'
|
|
86
|
+
END
|
|
87
|
+
WHERE status = 'dead' AND dead_reason IS NULL
|
|
88
|
+
`.execute(db);
|
|
89
|
+
await sql`UPDATE webhook_outbox SET dead_reason = NULL WHERE status <> 'dead'`.execute(
|
|
90
|
+
db,
|
|
91
|
+
);
|
|
92
|
+
await addCheck(
|
|
93
|
+
db,
|
|
94
|
+
"webhook_outbox",
|
|
95
|
+
"webhook_outbox_dead_reason_check",
|
|
96
|
+
"(status = 'dead') = (dead_reason IS NOT NULL) AND (dead_reason IS NULL OR dead_reason IN ('exhausted', 'orphaned'))",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
103
|
+
await onControlPlane(async () => {
|
|
104
|
+
if (await tableExists(db, "webhooks")) {
|
|
105
|
+
await sql`ALTER TABLE webhooks DROP CONSTRAINT IF EXISTS webhooks_pause_reason_check`.execute(
|
|
106
|
+
db,
|
|
107
|
+
);
|
|
108
|
+
await sql`ALTER TABLE webhooks DROP CONSTRAINT IF EXISTS webhooks_status_check`.execute(
|
|
109
|
+
db,
|
|
110
|
+
);
|
|
111
|
+
await sql`ALTER TABLE webhooks DROP COLUMN IF EXISTS pause_reason`.execute(
|
|
112
|
+
db,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (await tableExists(db, "webhook_outbox")) {
|
|
116
|
+
await sql`ALTER TABLE webhook_outbox DROP CONSTRAINT IF EXISTS webhook_outbox_dead_reason_check`.execute(
|
|
117
|
+
db,
|
|
118
|
+
);
|
|
119
|
+
await sql`ALTER TABLE webhook_outbox DROP COLUMN IF EXISTS dead_reason`.execute(
|
|
120
|
+
db,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Subgraph lifecycle columns.
|
|
6
|
+
*
|
|
7
|
+
* - `status` gets a CHECK mirroring `SUBGRAPH_STATES` (schemas/subgraphs.ts).
|
|
8
|
+
* `paused` and `deploying` were never written, so no row can carry them.
|
|
9
|
+
* - `billing_paused_at` / `billing_paused_code` replace the `billing_paused:`
|
|
10
|
+
* prefix that used to live in `last_error`, where the next processed block
|
|
11
|
+
* clobbered it. Both set or both null.
|
|
12
|
+
* - `expires_at` (paid-deploy expiry) had no reader and no writer: dropped.
|
|
13
|
+
*
|
|
14
|
+
* Skipped where `subgraphs` is absent (the chain DB under the source/target split).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const CHECKS = [
|
|
18
|
+
["subgraphs_status_check", "status IN ('active', 'reindexing', 'error')"],
|
|
19
|
+
[
|
|
20
|
+
"subgraphs_billing_paused_check",
|
|
21
|
+
"(billing_paused_at IS NULL) = (billing_paused_code IS NULL)",
|
|
22
|
+
],
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
async function subgraphsExist(db: Kysely<unknown>) {
|
|
26
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
27
|
+
SELECT to_regclass('public.subgraphs') IS NOT NULL AS exists
|
|
28
|
+
`.execute(db);
|
|
29
|
+
return rows[0]?.exists === true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
33
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
34
|
+
await onControlPlane(async () => {
|
|
35
|
+
if (!(await subgraphsExist(db))) return;
|
|
36
|
+
await sql`
|
|
37
|
+
ALTER TABLE subgraphs
|
|
38
|
+
ADD COLUMN IF NOT EXISTS billing_paused_at timestamptz,
|
|
39
|
+
ADD COLUMN IF NOT EXISTS billing_paused_code text
|
|
40
|
+
`.execute(db);
|
|
41
|
+
// Existing pauses carried the code in last_error; move them over.
|
|
42
|
+
await sql`
|
|
43
|
+
UPDATE subgraphs
|
|
44
|
+
SET billing_paused_at = COALESCE(last_error_at, now()),
|
|
45
|
+
billing_paused_code = substr(last_error, length('billing_paused: ') + 1),
|
|
46
|
+
last_error = NULL,
|
|
47
|
+
last_error_at = NULL
|
|
48
|
+
WHERE last_error LIKE 'billing_paused: %'
|
|
49
|
+
`.execute(db);
|
|
50
|
+
await sql`ALTER TABLE subgraphs DROP COLUMN IF EXISTS expires_at`.execute(
|
|
51
|
+
db,
|
|
52
|
+
);
|
|
53
|
+
for (const [name, expression] of CHECKS) {
|
|
54
|
+
await sql`ALTER TABLE subgraphs DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
55
|
+
db,
|
|
56
|
+
);
|
|
57
|
+
await sql`
|
|
58
|
+
ALTER TABLE subgraphs
|
|
59
|
+
ADD CONSTRAINT ${sql.id(name)} CHECK (${sql.raw(expression)}) NOT VALID
|
|
60
|
+
`.execute(db);
|
|
61
|
+
await sql`ALTER TABLE subgraphs VALIDATE CONSTRAINT ${sql.id(name)}`.execute(
|
|
62
|
+
db,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
69
|
+
await onControlPlane(async () => {
|
|
70
|
+
if (!(await subgraphsExist(db))) return;
|
|
71
|
+
for (const [name] of CHECKS) {
|
|
72
|
+
await sql`ALTER TABLE subgraphs DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
73
|
+
db,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
await sql`
|
|
77
|
+
ALTER TABLE subgraphs
|
|
78
|
+
ADD COLUMN IF NOT EXISTS expires_at timestamptz,
|
|
79
|
+
DROP COLUMN IF EXISTS billing_paused_at,
|
|
80
|
+
DROP COLUMN IF EXISTS billing_paused_code
|
|
81
|
+
`.execute(db);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `usage_ledger.debited_usd_micros` — the amount actually taken from the
|
|
6
|
+
* balance for the row. A clamped stack charge can take part of its price, which
|
|
7
|
+
* the boolean `debited` could not say; unpaid = `usd_micros - debited_usd_micros`.
|
|
8
|
+
*
|
|
9
|
+
* Existing rows backfill from the boolean: `debited` → the whole price, else 0.
|
|
10
|
+
* (A pre-existing partial take was recorded as `debited = false` with the full
|
|
11
|
+
* price, so those rows read as fully unpaid; the true partial amount was never
|
|
12
|
+
* stored and cannot be recovered.) Top-up and grant rows carry a negative
|
|
13
|
+
* `usd_micros`, so the CHECK bounds the column between 0 and the price in
|
|
14
|
+
* either direction.
|
|
15
|
+
*
|
|
16
|
+
* The column defaults to 0 so a writer that predates it (a deploy that lags the
|
|
17
|
+
* migration) still inserts. Re-run the backfill `UPDATE` below after the new
|
|
18
|
+
* code is live to repair any such rows.
|
|
19
|
+
*/
|
|
20
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
21
|
+
await onControlPlane(async () => {
|
|
22
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
23
|
+
await sql`
|
|
24
|
+
ALTER TABLE usage_ledger
|
|
25
|
+
ADD COLUMN IF NOT EXISTS debited_usd_micros bigint NOT NULL DEFAULT 0
|
|
26
|
+
`.execute(db);
|
|
27
|
+
await sql`
|
|
28
|
+
UPDATE usage_ledger
|
|
29
|
+
SET debited_usd_micros = usd_micros
|
|
30
|
+
WHERE debited AND debited_usd_micros = 0 AND usd_micros <> 0
|
|
31
|
+
`.execute(db);
|
|
32
|
+
await sql`
|
|
33
|
+
ALTER TABLE usage_ledger
|
|
34
|
+
DROP CONSTRAINT IF EXISTS usage_ledger_debited_usd_micros_check
|
|
35
|
+
`.execute(db);
|
|
36
|
+
await sql`
|
|
37
|
+
ALTER TABLE usage_ledger
|
|
38
|
+
ADD CONSTRAINT usage_ledger_debited_usd_micros_check
|
|
39
|
+
CHECK (debited_usd_micros BETWEEN LEAST(0, usd_micros) AND GREATEST(0, usd_micros))
|
|
40
|
+
NOT VALID
|
|
41
|
+
`.execute(db);
|
|
42
|
+
await sql`
|
|
43
|
+
ALTER TABLE usage_ledger
|
|
44
|
+
VALIDATE CONSTRAINT usage_ledger_debited_usd_micros_check
|
|
45
|
+
`.execute(db);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
50
|
+
await onControlPlane(async () => {
|
|
51
|
+
await sql`
|
|
52
|
+
ALTER TABLE usage_ledger
|
|
53
|
+
DROP CONSTRAINT IF EXISTS usage_ledger_debited_usd_micros_check
|
|
54
|
+
`.execute(db);
|
|
55
|
+
await sql`ALTER TABLE usage_ledger DROP COLUMN IF EXISTS debited_usd_micros`.execute(
|
|
56
|
+
db,
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onChainPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Indexer state machines.
|
|
6
|
+
*
|
|
7
|
+
* - `observer_journal.status` gains `poisoned` (deterministic apply failure,
|
|
8
|
+
* skipped after N tries) and `skipped` (receipt deliberately never applied:
|
|
9
|
+
* a refused bootstrap spool, a superseded burn block). Mirrors
|
|
10
|
+
* `OBSERVER_JOURNAL_STATUSES` (db/types.ts).
|
|
11
|
+
* - `contracts` gains `abi_attempts` / `abi_next_attempt_at` so `failed` is a
|
|
12
|
+
* retryable state with backoff instead of a terminal one. Every existing
|
|
13
|
+
* `failed` row re-polls once (attempts = 1, due now); the registry's poll
|
|
14
|
+
* batch size bounds how fast.
|
|
15
|
+
*
|
|
16
|
+
* Skipped where the tables are absent (not every database carries them).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const JOURNAL_STATUSES_OLD =
|
|
20
|
+
"status IN ('received', 'processed', 'failed', 'retried')";
|
|
21
|
+
const JOURNAL_STATUSES_NEW =
|
|
22
|
+
"status IN ('received', 'processed', 'failed', 'retried', 'poisoned', 'skipped')";
|
|
23
|
+
|
|
24
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
25
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
26
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
27
|
+
`.execute(db);
|
|
28
|
+
return rows[0]?.exists === true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function replaceJournalCheck(db: Kysely<unknown>, expression: string) {
|
|
32
|
+
await sql`ALTER TABLE observer_journal DROP CONSTRAINT IF EXISTS observer_journal_status_check`.execute(
|
|
33
|
+
db,
|
|
34
|
+
);
|
|
35
|
+
await sql`
|
|
36
|
+
ALTER TABLE observer_journal
|
|
37
|
+
ADD CONSTRAINT observer_journal_status_check CHECK (${sql.raw(expression)}) NOT VALID
|
|
38
|
+
`.execute(db);
|
|
39
|
+
await sql`ALTER TABLE observer_journal VALIDATE CONSTRAINT observer_journal_status_check`.execute(
|
|
40
|
+
db,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
45
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
46
|
+
await onChainPlane(async () => {
|
|
47
|
+
if (await tableExists(db, "observer_journal")) {
|
|
48
|
+
await replaceJournalCheck(db, JOURNAL_STATUSES_NEW);
|
|
49
|
+
}
|
|
50
|
+
if (await tableExists(db, "contracts")) {
|
|
51
|
+
await sql`
|
|
52
|
+
ALTER TABLE contracts
|
|
53
|
+
ADD COLUMN IF NOT EXISTS abi_attempts integer NOT NULL DEFAULT 0,
|
|
54
|
+
ADD COLUMN IF NOT EXISTS abi_next_attempt_at timestamptz
|
|
55
|
+
`.execute(db);
|
|
56
|
+
await sql`
|
|
57
|
+
UPDATE contracts
|
|
58
|
+
SET abi_attempts = 1, abi_next_attempt_at = now()
|
|
59
|
+
WHERE abi_status = 'failed'
|
|
60
|
+
`.execute(db);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
66
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
67
|
+
await onChainPlane(async () => {
|
|
68
|
+
if (await tableExists(db, "observer_journal")) {
|
|
69
|
+
await sql`
|
|
70
|
+
UPDATE observer_journal SET status = 'failed'
|
|
71
|
+
WHERE status IN ('poisoned', 'skipped')
|
|
72
|
+
`.execute(db);
|
|
73
|
+
await replaceJournalCheck(db, JOURNAL_STATUSES_OLD);
|
|
74
|
+
}
|
|
75
|
+
if (await tableExists(db, "contracts")) {
|
|
76
|
+
await sql`
|
|
77
|
+
ALTER TABLE contracts
|
|
78
|
+
DROP COLUMN IF EXISTS abi_attempts,
|
|
79
|
+
DROP COLUMN IF EXISTS abi_next_attempt_at
|
|
80
|
+
`.execute(db);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `hosted_stack_state`: the platform's copy of each hosted stack's real state,
|
|
6
|
+
* reported by the workload host (the owner of `tenants.state`). Replaces the
|
|
7
|
+
* guess from `memory.gb_hour` heartbeats. No row = no stack reported yet.
|
|
8
|
+
*
|
|
9
|
+
* The CHECKs mirror `TENANT_STATES` and `STOP_REASONS` (workload
|
|
10
|
+
* `control-db.ts`) and `HOSTED_STACK_STATES` (platform `hosted-stack-state.ts`).
|
|
11
|
+
* `reported_at` is the workload host's observation time; an older report never
|
|
12
|
+
* overwrites a newer one.
|
|
13
|
+
*/
|
|
14
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
15
|
+
await onControlPlane(async () => {
|
|
16
|
+
await sql`
|
|
17
|
+
CREATE TABLE IF NOT EXISTS hosted_stack_state (
|
|
18
|
+
account_id uuid PRIMARY KEY REFERENCES accounts(id) ON DELETE CASCADE,
|
|
19
|
+
state text NOT NULL
|
|
20
|
+
CONSTRAINT hosted_stack_state_state_check
|
|
21
|
+
CHECK (state IN ('provisioning', 'running', 'stopped', 'destroyed')),
|
|
22
|
+
stop_reason text
|
|
23
|
+
CONSTRAINT hosted_stack_state_stop_reason_check
|
|
24
|
+
CHECK (stop_reason IN ('credits', 'cap', 'idle')),
|
|
25
|
+
reported_at timestamptz NOT NULL,
|
|
26
|
+
CONSTRAINT hosted_stack_state_stop_reason_iff_stopped
|
|
27
|
+
CHECK ((state = 'stopped') = (stop_reason IS NOT NULL))
|
|
28
|
+
)
|
|
29
|
+
`.execute(db);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
34
|
+
await onControlPlane(async () => {
|
|
35
|
+
await sql`DROP TABLE IF EXISTS hosted_stack_state`.execute(db);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A ghost account (the owner of a signed-out reader's docs key) never has an
|
|
6
|
+
* email: an account that gains one stops being a ghost. The CHECK is added
|
|
7
|
+
* `NOT VALID` then validated, like the status checks in 0184; it fails
|
|
8
|
+
* loudly if a database already holds a ghost with an email.
|
|
9
|
+
*/
|
|
10
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
11
|
+
await onControlPlane(async () => {
|
|
12
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
13
|
+
await sql`
|
|
14
|
+
ALTER TABLE accounts DROP CONSTRAINT IF EXISTS accounts_ghost_no_email_check
|
|
15
|
+
`.execute(db);
|
|
16
|
+
await sql`
|
|
17
|
+
ALTER TABLE accounts
|
|
18
|
+
ADD CONSTRAINT accounts_ghost_no_email_check
|
|
19
|
+
CHECK (NOT ghost OR email IS NULL) NOT VALID
|
|
20
|
+
`.execute(db);
|
|
21
|
+
await sql`
|
|
22
|
+
ALTER TABLE accounts VALIDATE CONSTRAINT accounts_ghost_no_email_check
|
|
23
|
+
`.execute(db);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
28
|
+
await onControlPlane(async () => {
|
|
29
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
30
|
+
await sql`
|
|
31
|
+
ALTER TABLE accounts DROP CONSTRAINT IF EXISTS accounts_ghost_no_email_check
|
|
32
|
+
`.execute(db);
|
|
33
|
+
});
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@secondlayer/shared",
|
|
3
|
-
"version": "15.
|
|
3
|
+
"version": "15.3.0",
|
|
4
4
|
"description": "Internal building blocks shared across Secondlayer packages.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,6 +21,10 @@
|
|
|
21
21
|
"types": "./dist/src/db/index.d.ts",
|
|
22
22
|
"import": "./dist/src/db/index.js"
|
|
23
23
|
},
|
|
24
|
+
"./db/transition": {
|
|
25
|
+
"types": "./dist/src/db/transition.d.ts",
|
|
26
|
+
"import": "./dist/src/db/transition.js"
|
|
27
|
+
},
|
|
24
28
|
"./db/queries/instance": {
|
|
25
29
|
"types": "./dist/src/db/queries/instance.d.ts",
|
|
26
30
|
"import": "./dist/src/db/queries/instance.js"
|
|
@@ -249,6 +253,10 @@
|
|
|
249
253
|
"types": "./dist/src/insights/thresholds.d.ts",
|
|
250
254
|
"import": "./dist/src/insights/thresholds.js"
|
|
251
255
|
},
|
|
256
|
+
"./webhooks/health": {
|
|
257
|
+
"types": "./dist/src/webhooks/health.d.ts",
|
|
258
|
+
"import": "./dist/src/webhooks/health.js"
|
|
259
|
+
},
|
|
252
260
|
"./schemas/insights": {
|
|
253
261
|
"types": "./dist/src/schemas/insights.d.ts",
|
|
254
262
|
"import": "./dist/src/schemas/insights.js"
|