@secondlayer/shared 15.3.1 → 15.3.3
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,73 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Owner-set billing thresholds can never be negative: the auto-refill trigger
|
|
6
|
+
* and the monthly spend cap. Mirrors the route validation (`parseRefillInput`,
|
|
7
|
+
* `parseCapsInput` in routes/billing.ts) so a write that skips the route still
|
|
8
|
+
* cannot store one.
|
|
9
|
+
*
|
|
10
|
+
* Each CHECK is added `NOT VALID` then validated, and skipped when its table
|
|
11
|
+
* is absent. A negative value already stored could only have come from the
|
|
12
|
+
* unvalidated route, so it is cleared first: a null threshold is "off".
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
interface NonNegativeCheck {
|
|
16
|
+
table: string;
|
|
17
|
+
column: string;
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const CHECKS: NonNegativeCheck[] = [
|
|
22
|
+
{
|
|
23
|
+
table: "account_credits",
|
|
24
|
+
column: "refill_below_usd_micros",
|
|
25
|
+
name: "account_credits_refill_below_nonnegative_check",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
table: "account_spend_caps",
|
|
29
|
+
column: "monthly_cap_cents",
|
|
30
|
+
name: "account_spend_caps_monthly_cap_nonnegative_check",
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
35
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
36
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
37
|
+
`.execute(db);
|
|
38
|
+
return rows[0]?.exists === true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
42
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
43
|
+
await onControlPlane(async () => {
|
|
44
|
+
for (const { table, column, name } of CHECKS) {
|
|
45
|
+
if (!(await tableExists(db, table))) continue;
|
|
46
|
+
await sql`
|
|
47
|
+
UPDATE ${sql.table(table)} SET ${sql.id(column)} = NULL
|
|
48
|
+
WHERE ${sql.id(column)} < 0
|
|
49
|
+
`.execute(db);
|
|
50
|
+
await sql`ALTER TABLE ${sql.table(table)} DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
51
|
+
db,
|
|
52
|
+
);
|
|
53
|
+
await sql`
|
|
54
|
+
ALTER TABLE ${sql.table(table)}
|
|
55
|
+
ADD CONSTRAINT ${sql.id(name)} CHECK (${sql.id(column)} >= 0) NOT VALID
|
|
56
|
+
`.execute(db);
|
|
57
|
+
await sql`ALTER TABLE ${sql.table(table)} VALIDATE CONSTRAINT ${sql.id(name)}`.execute(
|
|
58
|
+
db,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
65
|
+
await onControlPlane(async () => {
|
|
66
|
+
for (const { table, name } of CHECKS) {
|
|
67
|
+
if (!(await tableExists(db, table))) continue;
|
|
68
|
+
await sql`ALTER TABLE ${sql.table(table)} DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
69
|
+
db,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
import { onControlPlane } from "../src/db/migration-role.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The ledger's shape, enforced where it is written. Mirrors what `meter()`,
|
|
6
|
+
* `recordTopup`, `grantCredits` and the refund path assert in code
|
|
7
|
+
* (`packages/platform/src/billing/meter.ts`):
|
|
8
|
+
*
|
|
9
|
+
* - `unit` is a known unit: every priced `MeterUnit` (retired `sentinel.*`
|
|
10
|
+
* included, their history rows still exist), `topup`, `grant`, `refund`.
|
|
11
|
+
* - `quantity` is never negative.
|
|
12
|
+
* - Sign: charges and refunds (money out) are `usd_micros >= 0`; top-ups and
|
|
13
|
+
* grants (money in) are `usd_micros <= 0`.
|
|
14
|
+
*
|
|
15
|
+
* Each CHECK is added `NOT VALID`: it binds every new write at once and never
|
|
16
|
+
* blocks the deploy on a historical row. Run
|
|
17
|
+
* `ALTER TABLE usage_ledger VALIDATE CONSTRAINT <name>` once the table has been
|
|
18
|
+
* checked clean. Skipped when the table is absent.
|
|
19
|
+
*
|
|
20
|
+
* Adding a unit means adding it to `prices.ts` AND to a new migration that
|
|
21
|
+
* replaces `usage_ledger_unit_check`; `meter-invariants.test.ts` fails when the
|
|
22
|
+
* two drift.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const METER_UNITS = [
|
|
26
|
+
"archive.partition",
|
|
27
|
+
"archive.partition.events",
|
|
28
|
+
"rows.delivered",
|
|
29
|
+
"memory.gb_hour",
|
|
30
|
+
"storage.gb_day",
|
|
31
|
+
"webhook.event",
|
|
32
|
+
"sentinel.run",
|
|
33
|
+
"sentinel.deep_audit",
|
|
34
|
+
"sentinel.check_run",
|
|
35
|
+
"sentinel.monitored_event",
|
|
36
|
+
];
|
|
37
|
+
const UNITS = [...METER_UNITS, "topup", "grant", "refund"];
|
|
38
|
+
|
|
39
|
+
const unitList = UNITS.map((u) => `'${u}'`).join(", ");
|
|
40
|
+
|
|
41
|
+
const CHECKS: { name: string; expression: string }[] = [
|
|
42
|
+
{ name: "usage_ledger_unit_check", expression: `unit IN (${unitList})` },
|
|
43
|
+
{
|
|
44
|
+
name: "usage_ledger_quantity_nonnegative_check",
|
|
45
|
+
expression: "quantity >= 0",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "usage_ledger_usd_micros_sign_check",
|
|
49
|
+
expression: `CASE WHEN unit IN ('topup', 'grant') THEN usd_micros <= 0 ELSE usd_micros >= 0 END`,
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
async function tableExists(db: Kysely<unknown>, table: string) {
|
|
54
|
+
const { rows } = await sql<{ exists: boolean }>`
|
|
55
|
+
SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
|
|
56
|
+
`.execute(db);
|
|
57
|
+
return rows[0]?.exists === true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
61
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
62
|
+
await onControlPlane(async () => {
|
|
63
|
+
if (!(await tableExists(db, "usage_ledger"))) return;
|
|
64
|
+
for (const { name, expression } of CHECKS) {
|
|
65
|
+
await sql`ALTER TABLE usage_ledger DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
66
|
+
db,
|
|
67
|
+
);
|
|
68
|
+
await sql`
|
|
69
|
+
ALTER TABLE usage_ledger
|
|
70
|
+
ADD CONSTRAINT ${sql.id(name)} CHECK (${sql.raw(expression)}) NOT VALID
|
|
71
|
+
`.execute(db);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
77
|
+
await onControlPlane(async () => {
|
|
78
|
+
if (!(await tableExists(db, "usage_ledger"))) return;
|
|
79
|
+
for (const { name } of CHECKS) {
|
|
80
|
+
await sql`ALTER TABLE usage_ledger DROP CONSTRAINT IF EXISTS ${sql.id(name)}`.execute(
|
|
81
|
+
db,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|