@secondlayer/shared 15.3.1 → 15.3.2
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
|
+
}
|