@secondlayer/shared 15.3.2 → 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,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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/shared",
3
- "version": "15.3.2",
3
+ "version": "15.3.3",
4
4
  "description": "Internal building blocks shared across Secondlayer packages.",
5
5
  "license": "MIT",
6
6
  "repository": {