@secondlayer/shared 13.7.0 → 13.7.1
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.
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Billing reads filter `usage_ledger` by `unit = 'rows.delivered'` (or
|
|
6
|
+
* `memory.gb_hour`) per account over a time window. The existing
|
|
7
|
+
* `(account_id, occurred_at)` index reads every unit's rows and discards
|
|
8
|
+
* most of them, so `(account_id, unit, occurred_at)` lets those reads touch
|
|
9
|
+
* only the matching rows. The old index stays: unit-less month and burn
|
|
10
|
+
* reads still use it.
|
|
11
|
+
*
|
|
12
|
+
* `api_failed_requests` is filtered by `account_id` + `created_at` but only
|
|
13
|
+
* indexed on `created_at`.
|
|
14
|
+
*
|
|
15
|
+
* On prod the operator pre-creates both indexes CONCURRENTLY with these
|
|
16
|
+
* exact names before deploy, so IF NOT EXISTS makes this a no-op there and
|
|
17
|
+
* a cheap build on fresh/dev databases.
|
|
18
|
+
*/
|
|
19
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
20
|
+
await onControlPlane(async () => {
|
|
21
|
+
await sql`
|
|
22
|
+
CREATE INDEX IF NOT EXISTS usage_ledger_account_unit_occurred_at_idx
|
|
23
|
+
ON usage_ledger (account_id, unit, occurred_at)
|
|
24
|
+
`.execute(db);
|
|
25
|
+
await sql`
|
|
26
|
+
CREATE INDEX IF NOT EXISTS api_failed_requests_account_created_at_idx
|
|
27
|
+
ON api_failed_requests (account_id, created_at)
|
|
28
|
+
`.execute(db);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
33
|
+
await onControlPlane(async () => {
|
|
34
|
+
await sql`DROP INDEX IF EXISTS usage_ledger_account_unit_occurred_at_idx`.execute(
|
|
35
|
+
db,
|
|
36
|
+
);
|
|
37
|
+
await sql`DROP INDEX IF EXISTS api_failed_requests_account_created_at_idx`.execute(
|
|
38
|
+
db,
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
}
|