@flytedesk/app-kit 16.6.0 → 16.7.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/README.md +23 -0
- package/dist/migrate/allOrNothing.d.ts +14 -0
- package/dist/migrate/allOrNothing.js +84 -0
- package/dist/migrate/allOrNothing.js.map +1 -0
- package/dist/migrate/checks.d.ts +24 -0
- package/dist/migrate/checks.js +59 -0
- package/dist/migrate/checks.js.map +1 -0
- package/dist/migrate/cli.d.ts +2 -0
- package/dist/migrate/cli.js +39 -0
- package/dist/migrate/cli.js.map +1 -0
- package/dist/migrate/runner.d.ts +9 -0
- package/dist/migrate/runner.js +138 -0
- package/dist/migrate/runner.js.map +1 -0
- package/dist/migrate/state.d.ts +11 -0
- package/dist/migrate/state.js +32 -0
- package/dist/migrate/state.js.map +1 -0
- package/dist/sql-guard/run.d.ts +8 -3
- package/dist/sql-guard/run.js +19 -10
- package/dist/sql-guard/run.js.map +1 -1
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -563,6 +563,29 @@ system's notice reads it. The api client does the rest:
|
|
|
563
563
|
|
|
564
564
|
An app's unit tests reset the watch with `resetAuthSessionForTests`.
|
|
565
565
|
|
|
566
|
+
## Applying migrations (`app-kit-migrate`)
|
|
567
|
+
|
|
568
|
+
An app's migrate job runs `npx app-kit-migrate` in place of `prisma migrate deploy`, from the directory holding its `prisma.config.ts`. It
|
|
569
|
+
takes no arguments: the migrations folder is `prisma/migrations` and the database URL is `DATABASE_URL`, which is what every flytedesk app's
|
|
570
|
+
`prisma.config.ts` already says. It needs `pg`, an optional peer dependency every app already has through `@prisma/adapter-pg`. Each step logs
|
|
571
|
+
one JSON line (`level`, `event`, ...) on stdout or stderr.
|
|
572
|
+
|
|
573
|
+
1. **Look before.** A migration may ship read-only checks beside its `migration.sql`, named `check-<name>.sql`: each is one SELECT (no
|
|
574
|
+
trailing statement) whose rows are the offenders and which has a column named `id`, the offending row's id. Before applying anything, the
|
|
575
|
+
runner runs every check of every pending migration (one with no finished, non-rolled-back row in `_prisma_migrations`) in a read-only
|
|
576
|
+
transaction, so a check sees the **pre-apply schema only**: it cannot refer to a column or table a pending migration adds. A check that
|
|
577
|
+
returns rows stops the run with a `migration_check_failed` line naming the migration, the check, the count and the first 100 ids; a check
|
|
578
|
+
that errors (invalid SQL, more than one statement, a write the read-only transaction refuses, a timeout) stops it the same way and is never
|
|
579
|
+
read as a pass. Nothing is applied and `_prisma_migrations` is not written.
|
|
580
|
+
2. **Apply.** With no failing check the runner runs `prisma migrate deploy` with your environment and output and exits with its code.
|
|
581
|
+
3. **Repair after.** When `migrate deploy` fails, the migration it left failed is marked rolled back (`prisma migrate resolve --rolled-back`)
|
|
582
|
+
when its `migration.sql`, comments aside, is exactly one `DO $tag$ ... $tag$` statement (the all-or-nothing block each app's rule requires),
|
|
583
|
+
so the next run applies it again once fixed. Any other file (several statements, a plain statement, a `CONCURRENTLY` build, which a kill can
|
|
584
|
+
leave half done) stays failed: the run exits non-zero with a `migration_not_repaired` line saying why. A migration already failed when the
|
|
585
|
+
runner starts is classified the same way before anything is applied.
|
|
586
|
+
|
|
587
|
+
Set the job's retries to 0: a stopped runner should not run again by itself.
|
|
588
|
+
|
|
566
589
|
## Auth (`@flytedesk/app-kit/auth`)
|
|
567
590
|
|
|
568
591
|
`idpRequestTimeoutMs` (`FlytedeskAuthOptions`/`OidcClientOptions`, default 5s) bounds
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AK-89: whether a migration.sql is one all-or-nothing block, the only shape the runner marks rolled back by itself.
|
|
3
|
+
* A file is all-or-nothing when, comments aside, it is exactly one `DO $tag$ ... $tag$` statement: Postgres runs it
|
|
4
|
+
* in one implicit transaction, so a failure leaves nothing behind. Several statements, a single plain statement and
|
|
5
|
+
* a `CREATE INDEX CONCURRENTLY` are not (a killed concurrent build leaves an INVALID index), and neither is a
|
|
6
|
+
* hand-written BEGIN/COMMIT around anything.
|
|
7
|
+
*/
|
|
8
|
+
export type AllOrNothing = {
|
|
9
|
+
atomic: true;
|
|
10
|
+
} | {
|
|
11
|
+
atomic: false;
|
|
12
|
+
reason: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function classifyMigrationSql(sql: string): AllOrNothing;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AK-89: whether a migration.sql is one all-or-nothing block, the only shape the runner marks rolled back by itself.
|
|
3
|
+
* A file is all-or-nothing when, comments aside, it is exactly one `DO $tag$ ... $tag$` statement: Postgres runs it
|
|
4
|
+
* in one implicit transaction, so a failure leaves nothing behind. Several statements, a single plain statement and
|
|
5
|
+
* a `CREATE INDEX CONCURRENTLY` are not (a killed concurrent build leaves an INVALID index), and neither is a
|
|
6
|
+
* hand-written BEGIN/COMMIT around anything.
|
|
7
|
+
*/
|
|
8
|
+
const WORD = /[A-Za-z_][A-Za-z0-9_]*/y;
|
|
9
|
+
const DOLLAR_TAG = /\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/y;
|
|
10
|
+
function skipTrivia(sql, from) {
|
|
11
|
+
let i = from;
|
|
12
|
+
for (;;) {
|
|
13
|
+
if (/\s/.test(sql[i] ?? "x")) {
|
|
14
|
+
i += 1;
|
|
15
|
+
}
|
|
16
|
+
else if (sql.startsWith("--", i)) {
|
|
17
|
+
const end = sql.indexOf("\n", i);
|
|
18
|
+
i = end === -1 ? sql.length : end + 1;
|
|
19
|
+
}
|
|
20
|
+
else if (sql.startsWith("/*", i)) {
|
|
21
|
+
let depth = 1;
|
|
22
|
+
i += 2;
|
|
23
|
+
while (depth > 0 && i < sql.length) {
|
|
24
|
+
if (sql.startsWith("/*", i)) {
|
|
25
|
+
depth += 1;
|
|
26
|
+
i += 2;
|
|
27
|
+
}
|
|
28
|
+
else if (sql.startsWith("*/", i)) {
|
|
29
|
+
depth -= 1;
|
|
30
|
+
i += 2;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
i += 1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return i;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function wordAt(sql, i) {
|
|
43
|
+
WORD.lastIndex = i;
|
|
44
|
+
return WORD.exec(sql)?.[0];
|
|
45
|
+
}
|
|
46
|
+
export function classifyMigrationSql(sql) {
|
|
47
|
+
let i = skipTrivia(sql, 0);
|
|
48
|
+
if (i >= sql.length)
|
|
49
|
+
return { atomic: false, reason: "the file holds no statement" };
|
|
50
|
+
const first = wordAt(sql, i);
|
|
51
|
+
if (first?.toUpperCase() !== "DO") {
|
|
52
|
+
return {
|
|
53
|
+
atomic: false,
|
|
54
|
+
reason: `it starts with ${first ?? JSON.stringify(sql[i])}, not a DO block`,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
i = skipTrivia(sql, i + first.length);
|
|
58
|
+
i = skipLanguage(sql, i);
|
|
59
|
+
DOLLAR_TAG.lastIndex = i;
|
|
60
|
+
const tag = DOLLAR_TAG.exec(sql)?.[0];
|
|
61
|
+
if (tag === undefined)
|
|
62
|
+
return { atomic: false, reason: "its DO has no dollar-quoted body" };
|
|
63
|
+
const close = sql.indexOf(tag, i + tag.length);
|
|
64
|
+
if (close === -1)
|
|
65
|
+
return { atomic: false, reason: `its DO body is not closed by ${tag}` };
|
|
66
|
+
i = skipLanguage(sql, skipTrivia(sql, close + tag.length));
|
|
67
|
+
if (sql[i] === ";")
|
|
68
|
+
i = skipTrivia(sql, i + 1);
|
|
69
|
+
if (i < sql.length)
|
|
70
|
+
return { atomic: false, reason: "it holds more than one statement" };
|
|
71
|
+
return { atomic: true };
|
|
72
|
+
}
|
|
73
|
+
/** An optional `LANGUAGE name` clause of a DO statement, which may come before or after the body. */
|
|
74
|
+
function skipLanguage(sql, from) {
|
|
75
|
+
const word = wordAt(sql, from);
|
|
76
|
+
if (word?.toUpperCase() !== "LANGUAGE")
|
|
77
|
+
return from;
|
|
78
|
+
const name = skipTrivia(sql, from + word.length);
|
|
79
|
+
const language = wordAt(sql, name);
|
|
80
|
+
return language === undefined
|
|
81
|
+
? from
|
|
82
|
+
: skipTrivia(sql, name + language.length);
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=allOrNothing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allOrNothing.js","sourceRoot":"","sources":["../../src/migrate/allOrNothing.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,MAAM,IAAI,GAAG,yBAAyB,CAAC;AACvC,MAAM,UAAU,GAAG,kCAAkC,CAAC;AAEtD,SAAS,UAAU,CAAC,GAAW,EAAE,IAAY;IAC3C,IAAI,CAAC,GAAG,IAAI,CAAC;IACb,SAAS,CAAC;QACR,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;YAC7B,CAAC,IAAI,CAAC,CAAC;QACT,CAAC;aAAM,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;YACnC,MAAM,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YACjC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;YACnC,IAAI,KAAK,GAAG,CAAC,CAAC;YACd,CAAC,IAAI,CAAC,CAAC;YACP,OAAO,KAAK,GAAG,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;gBACnC,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;oBAC5B,KAAK,IAAI,CAAC,CAAC;oBAC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AAU,CAAC,GAAG,EAAE,IAAI,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;AAC9C,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type PostgresClientLike } from "../sql-guard/run.js";
|
|
2
|
+
export declare const REPORTED_OFFENDER_IDS = 100;
|
|
3
|
+
export interface CheckClient extends PostgresClientLike {
|
|
4
|
+
query(query: string | {
|
|
5
|
+
text: string;
|
|
6
|
+
queryMode: "extended";
|
|
7
|
+
}): Promise<{
|
|
8
|
+
rows: unknown[];
|
|
9
|
+
}>;
|
|
10
|
+
}
|
|
11
|
+
export type CheckFailure = {
|
|
12
|
+
migration: string;
|
|
13
|
+
check: string;
|
|
14
|
+
kind: "offenders";
|
|
15
|
+
count: number;
|
|
16
|
+
ids: unknown[];
|
|
17
|
+
} | {
|
|
18
|
+
migration: string;
|
|
19
|
+
check: string;
|
|
20
|
+
kind: "errored";
|
|
21
|
+
message: string;
|
|
22
|
+
};
|
|
23
|
+
/** Runs every check of every migration in `pending`, each in its own read-only transaction; returns every failure. */
|
|
24
|
+
export declare function runChecks(client: CheckClient, migrationsDir: string, pending: string[], timeoutMs: number): Promise<CheckFailure[]>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AK-89: the read-only pre-checks a migration ships beside its migration.sql. A check is a committed, human-written
|
|
3
|
+
* `check-<name>.sql`: one SELECT whose rows are the offenders and which has a column named `id`, the offending row's
|
|
4
|
+
* id. The read-only transaction is what makes it safe to run, so these files do not go through `guardSql`.
|
|
5
|
+
*/
|
|
6
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { inReadOnlyPostgresTransaction, } from "../sql-guard/run.js";
|
|
9
|
+
export const REPORTED_OFFENDER_IDS = 100;
|
|
10
|
+
function checkFiles(migrationsDir, migration) {
|
|
11
|
+
return readdirSync(join(migrationsDir, migration))
|
|
12
|
+
.filter((file) => /^check-.+\.sql$/.test(file))
|
|
13
|
+
.sort();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The check as the one statement that runs: its text is the subquery of a SELECT, so more than its one query is
|
|
17
|
+
* a syntax error, and it is sent in pg's `extended` query mode (the extended protocol), which Postgres refuses to parse as
|
|
18
|
+
* several commands before running any of them. A `ROLLBACK` or `COMMIT` inside a check file therefore never runs.
|
|
19
|
+
* The offenders are counted by the server and only the first `REPORTED_OFFENDER_IDS` come back.
|
|
20
|
+
*/
|
|
21
|
+
function offendersQuery(sql) {
|
|
22
|
+
const check = sql.replace(/[\s;]+$/, "");
|
|
23
|
+
return {
|
|
24
|
+
text: `SELECT offenders.id, count(*) OVER () AS offender_count FROM (\n${check}\n) AS offenders LIMIT ${REPORTED_OFFENDER_IDS}`,
|
|
25
|
+
queryMode: "extended",
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Runs every check of every migration in `pending`, each in its own read-only transaction; returns every failure. */
|
|
29
|
+
export async function runChecks(client, migrationsDir, pending, timeoutMs) {
|
|
30
|
+
const failures = [];
|
|
31
|
+
for (const migration of pending) {
|
|
32
|
+
for (const check of checkFiles(migrationsDir, migration)) {
|
|
33
|
+
const sql = readFileSync(join(migrationsDir, migration, check), "utf8");
|
|
34
|
+
try {
|
|
35
|
+
const { rows } = await inReadOnlyPostgresTransaction(client, timeoutMs, () => client.query(offendersQuery(sql)));
|
|
36
|
+
if (rows.length > 0) {
|
|
37
|
+
const found = rows;
|
|
38
|
+
failures.push({
|
|
39
|
+
migration,
|
|
40
|
+
check,
|
|
41
|
+
kind: "offenders",
|
|
42
|
+
count: Number(found[0].offender_count),
|
|
43
|
+
ids: found.map((row) => row.id),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
failures.push({
|
|
49
|
+
migration,
|
|
50
|
+
check,
|
|
51
|
+
kind: "errored",
|
|
52
|
+
message: error instanceof Error ? error.message : String(error),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return failures;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=checks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checks.js","sourceRoot":"","sources":["../../src/migrate/checks.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EACL,6BAA6B,GAE9B,MAAM,qBAAqB,CAAC;AAE7B,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAkBzC,SAAS,UAAU,CAAC,aAAqB,EAAE,SAAiB;IAC1D,OAAO,WAAW,CAAC,IAAI,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;SAC/C,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SAC9C,IAAI,EAAE,CAAC;AACZ,CAAC;AAED;;;;;GAKG;AACH,SAAS,cAAc,CAAC,GAAW;IACjC,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IACzC,OAAO;QACL,IAAI,EAAE,mEAAmE,KAAK,0BAA0B,qBAAqB,EAAE;QAC/H,SAAS,EAAE,UAAU;KACtB,CAAC;AACJ,CAAC;AAED,sHAAsH;AACtH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,MAAmB,EACnB,aAAqB,EACrB,OAAiB,EACjB,SAAiB;IAEjB,MAAM,QAAQ,GAAmB,EAAE,CAAC;IACpC,KAAK,MAAM,SAAS,IAAI,OAAO,EAAE,CAAC;QAChC,KAAK,MAAM,KAAK,IAAI,UAAU,CAAC,aAAa,EAAE,SAAS,CAAC,EAAE,CAAC;YACzD,MAAM,GAAG,GAAG,YAAY,CAAC,IAAI,CAAC,aAAa,EAAE,SAAS,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC;YACxE,IAAI,CAAC;gBACH,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,6BAA6B,CAClD,MAAM,EACN,SAAS,EACT,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CACxC,CAAC;gBACF,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACpB,MAAM,KAAK,GAAG,IAAsD,CAAC;oBACrE,QAAQ,CAAC,IAAI,CAAC;wBACZ,SAAS;wBACT,KAAK;wBACL,IAAI,EAAE,WAAW;wBACjB,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,cAAc,CAAC;wBACvC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC;qBAChC,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,QAAQ,CAAC,IAAI,CAAC;oBACZ,SAAS;oBACT,KAAK;oBACL,IAAI,EAAE,SAAS;oBACf,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;iBAChE,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* app-kit-migrate
|
|
4
|
+
*
|
|
5
|
+
* Applies an app's Prisma migrations in place of `prisma migrate deploy`: runs each pending migration's
|
|
6
|
+
* `check-*.sql` read-only checks first, then `prisma migrate deploy`, and marks a failed all-or-nothing
|
|
7
|
+
* migration rolled back by itself. Run it from the directory holding the app's `prisma.config.ts`; it takes no
|
|
8
|
+
* arguments (see `runMigrate` for where it reads the migrations and the database URL).
|
|
9
|
+
*/
|
|
10
|
+
import { isRunningAsMain } from "../cli/is-running-as-main.js";
|
|
11
|
+
import { runMigrate } from "./runner.js";
|
|
12
|
+
/** How long one check may run before it counts as an error. */
|
|
13
|
+
const CHECK_TIMEOUT_MS = 60_000;
|
|
14
|
+
function logLine(event) {
|
|
15
|
+
const write = event.level === "error" ? console.error : console.log;
|
|
16
|
+
write(JSON.stringify(event));
|
|
17
|
+
}
|
|
18
|
+
export async function main(argv, env = process.env) {
|
|
19
|
+
if (argv.length > 0) {
|
|
20
|
+
console.error(`unexpected argument "${argv[0]}"`);
|
|
21
|
+
console.error("usage: app-kit-migrate");
|
|
22
|
+
return 2;
|
|
23
|
+
}
|
|
24
|
+
return runMigrate({
|
|
25
|
+
env,
|
|
26
|
+
cwd: process.cwd(),
|
|
27
|
+
checkTimeoutMs: CHECK_TIMEOUT_MS,
|
|
28
|
+
log: logLine,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (isRunningAsMain(import.meta.url)) {
|
|
32
|
+
main(process.argv.slice(2)).then((code) => {
|
|
33
|
+
process.exitCode = code;
|
|
34
|
+
}, (error) => {
|
|
35
|
+
console.error(error);
|
|
36
|
+
process.exitCode = 1;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/migrate/cli.ts"],"names":[],"mappings":";AACA;;;;;;;GAOG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,+DAA+D;AAC/D,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,SAAS,OAAO,CAAC,KAA8B;IAC7C,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;IACpE,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;AAC/B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,IAAc,EACd,MAAyB,OAAO,CAAC,GAAG;IAEpC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpB,OAAO,CAAC,KAAK,CAAC,wBAAwB,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAClD,OAAO,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;QACxC,OAAO,CAAC,CAAC;IACX,CAAC;IACD,OAAO,UAAU,CAAC;QAChB,GAAG;QACH,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;QAClB,cAAc,EAAE,gBAAgB;QAChC,GAAG,EAAE,OAAO;KACb,CAAC,CAAC;AACL,CAAC;AAED,IAAI,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;IACrC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAC9B,CAAC,IAAI,EAAE,EAAE;QACP,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAC1B,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;QACjB,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;IACvB,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface MigrateOptions {
|
|
2
|
+
env: NodeJS.ProcessEnv;
|
|
3
|
+
/** The directory holding prisma.config.ts, where `prisma` is run and `prisma/migrations` is found. */
|
|
4
|
+
cwd: string;
|
|
5
|
+
/** How long one check may run before it counts as an error. */
|
|
6
|
+
checkTimeoutMs: number;
|
|
7
|
+
log: (event: Record<string, unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function runMigrate(options: MigrateOptions): Promise<number>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AK-89: the migrate runner an app's migrate job runs in place of `prisma migrate deploy`.
|
|
3
|
+
*
|
|
4
|
+
* 1. A migration already left failed is repaired when it is all-or-nothing, else the run stops naming it.
|
|
5
|
+
* 2. Every read-only check of every pending migration runs against the pre-apply schema; a check that returns
|
|
6
|
+
* rows, or errors, stops the run before anything is applied.
|
|
7
|
+
* 3. `prisma migrate deploy` runs with the caller's environment and output; its exit code is the run's.
|
|
8
|
+
* 4. When it fails, the migration it left failed is marked rolled back if (and only if) it is all-or-nothing.
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import spawn from "cross-spawn";
|
|
13
|
+
import { Client } from "pg";
|
|
14
|
+
import { classifyMigrationSql } from "./allOrNothing.js";
|
|
15
|
+
import { runChecks } from "./checks.js";
|
|
16
|
+
import { pendingMigrations, readMigrationState } from "./state.js";
|
|
17
|
+
/**
|
|
18
|
+
* The migrations folder and the database URL are fixed, not options: they are what `prisma migrate deploy` reads from
|
|
19
|
+
* every flytedesk app's prisma.config.ts, and one place for them means the checks, the repair and the deploy
|
|
20
|
+
* can never look at different ones.
|
|
21
|
+
*/
|
|
22
|
+
const MIGRATIONS_PATH = "prisma/migrations";
|
|
23
|
+
const DATABASE_URL_VARIABLE = "DATABASE_URL";
|
|
24
|
+
function runPrisma(args, { env, cwd }) {
|
|
25
|
+
const result = spawn.sync("npx", ["prisma", ...args], {
|
|
26
|
+
cwd,
|
|
27
|
+
env,
|
|
28
|
+
stdio: "inherit",
|
|
29
|
+
});
|
|
30
|
+
if (result.error)
|
|
31
|
+
throw new Error(`failed to run "npx prisma ${args.join(" ")}": ${result.error.message}`);
|
|
32
|
+
return result.status ?? 1;
|
|
33
|
+
}
|
|
34
|
+
async function withClient(url, run) {
|
|
35
|
+
const client = new Client({ connectionString: url });
|
|
36
|
+
await client.connect();
|
|
37
|
+
try {
|
|
38
|
+
return await run(client);
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
await client.end();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Marks `name` rolled back when it is all-or-nothing. Returns whether the database is clear of it. */
|
|
45
|
+
function repairIfAtomic(name, why, options) {
|
|
46
|
+
let sql;
|
|
47
|
+
try {
|
|
48
|
+
sql = readFileSync(join(options.cwd, MIGRATIONS_PATH, name, "migration.sql"), "utf8");
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
options.log({
|
|
52
|
+
level: "error",
|
|
53
|
+
event: "migration_not_repaired",
|
|
54
|
+
migration: name,
|
|
55
|
+
why,
|
|
56
|
+
reason: "its migration.sql is not in the migrations folder",
|
|
57
|
+
});
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const verdict = classifyMigrationSql(sql);
|
|
61
|
+
if (!verdict.atomic) {
|
|
62
|
+
options.log({
|
|
63
|
+
level: "error",
|
|
64
|
+
event: "migration_not_repaired",
|
|
65
|
+
migration: name,
|
|
66
|
+
why,
|
|
67
|
+
reason: verdict.reason,
|
|
68
|
+
});
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
const command = `prisma migrate resolve --rolled-back ${name}`;
|
|
72
|
+
if (runPrisma(["migrate", "resolve", "--rolled-back", name], options) !== 0) {
|
|
73
|
+
options.log({
|
|
74
|
+
level: "error",
|
|
75
|
+
event: "migration_resolve_failed",
|
|
76
|
+
migration: name,
|
|
77
|
+
command,
|
|
78
|
+
});
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
options.log({
|
|
82
|
+
level: "info",
|
|
83
|
+
event: "migration_rolled_back",
|
|
84
|
+
migration: name,
|
|
85
|
+
why,
|
|
86
|
+
command,
|
|
87
|
+
});
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
/** Repairs every failed migration; true when none is left failed. */
|
|
91
|
+
function repairAll(failed, why, options) {
|
|
92
|
+
return failed
|
|
93
|
+
.map((name) => repairIfAtomic(name, why, options))
|
|
94
|
+
.every(Boolean);
|
|
95
|
+
}
|
|
96
|
+
export async function runMigrate(options) {
|
|
97
|
+
const url = options.env[DATABASE_URL_VARIABLE];
|
|
98
|
+
if (!url) {
|
|
99
|
+
options.log({
|
|
100
|
+
level: "error",
|
|
101
|
+
event: "database_url_missing",
|
|
102
|
+
variable: DATABASE_URL_VARIABLE,
|
|
103
|
+
});
|
|
104
|
+
return 2;
|
|
105
|
+
}
|
|
106
|
+
const stopped = await withClient(url, async (client) => {
|
|
107
|
+
const before = await readMigrationState(client);
|
|
108
|
+
if (!repairAll(before.failed, "failed before this run", options))
|
|
109
|
+
return true;
|
|
110
|
+
const state = before.failed.length > 0 ? await readMigrationState(client) : before;
|
|
111
|
+
const migrationsDir = join(options.cwd, MIGRATIONS_PATH);
|
|
112
|
+
const failures = await runChecks(client, migrationsDir, pendingMigrations(migrationsDir, state), options.checkTimeoutMs);
|
|
113
|
+
for (const failure of failures)
|
|
114
|
+
options.log({
|
|
115
|
+
level: "error",
|
|
116
|
+
event: "migration_check_failed",
|
|
117
|
+
...failure,
|
|
118
|
+
});
|
|
119
|
+
return failures.length > 0;
|
|
120
|
+
});
|
|
121
|
+
if (stopped)
|
|
122
|
+
return 1;
|
|
123
|
+
const code = runPrisma(["migrate", "deploy"], options);
|
|
124
|
+
if (code === 0)
|
|
125
|
+
return 0;
|
|
126
|
+
const failed = (await withClient(url, readMigrationState)).failed;
|
|
127
|
+
if (failed.length === 0)
|
|
128
|
+
options.log({
|
|
129
|
+
level: "error",
|
|
130
|
+
event: "deploy_failed",
|
|
131
|
+
exitCode: code,
|
|
132
|
+
reason: "no migration was left failed",
|
|
133
|
+
});
|
|
134
|
+
else
|
|
135
|
+
repairAll(failed, "failed in this run", options);
|
|
136
|
+
return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D,MAAM,QAAQ,GAAG,MAAM,SAAS,CAC9B,MAAM,EACN,aAAa,EACb,iBAAiB,CAAC,aAAa,EAAE,KAAK,CAAC,EACvC,OAAO,CAAC,cAAc,CACvB,CAAC;QACF,KAAK,MAAM,OAAO,IAAI,QAAQ;YAC5B,OAAO,CAAC,GAAG,CAAC;gBACV,KAAK,EAAE,OAAO;gBACd,KAAK,EAAE,wBAAwB;gBAC/B,GAAG,OAAO;aACX,CAAC,CAAC;QACL,OAAO,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;IACH,IAAI,OAAO;QAAE,OAAO,CAAC,CAAC;IAEtB,MAAM,IAAI,GAAG,SAAS,CAAC,CAAC,SAAS,EAAE,QAAQ,CAAC,EAAE,OAAO,CAAC,CAAC;IACvD,IAAI,IAAI,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IACzB,MAAM,MAAM,GAAG,CAAC,MAAM,UAAU,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAC,CAAC,MAAM,CAAC;IAClE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QACrB,OAAO,CAAC,GAAG,CAAC;YACV,KAAK,EAAE,OAAO;YACd,KAAK,EAAE,eAAe;YACtB,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,8BAA8B;SACvC,CAAC,CAAC;;QACA,SAAS,CAAC,MAAM,EAAE,oBAAoB,EAAE,OAAO,CAAC,CAAC;IACtD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PostgresClientLike } from "../sql-guard/run.js";
|
|
2
|
+
export interface MigrationState {
|
|
3
|
+
/** Names with a finished, non-rolled-back row in `_prisma_migrations`. */
|
|
4
|
+
applied: Set<string>;
|
|
5
|
+
/** Names with a row that never finished and was never rolled back: what Prisma refuses to go past (P3009). */
|
|
6
|
+
failed: string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function readMigrationState(client: PostgresClientLike): Promise<MigrationState>;
|
|
9
|
+
/** The migration folders (those holding a migration.sql), in the order Prisma applies them. */
|
|
10
|
+
export declare function listMigrationFolders(migrationsDir: string): string[];
|
|
11
|
+
export declare function pendingMigrations(migrationsDir: string, state: MigrationState): string[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AK-89: what the database and the migrations folder say about which migrations still have to run.
|
|
3
|
+
*/
|
|
4
|
+
import { readdirSync, existsSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
export async function readMigrationState(client) {
|
|
7
|
+
const present = await client.query("SELECT to_regclass('_prisma_migrations') IS NOT NULL AS present");
|
|
8
|
+
if (!present.rows[0].present)
|
|
9
|
+
return { applied: new Set(), failed: [] };
|
|
10
|
+
const { rows } = await client.query("SELECT migration_name, finished_at IS NOT NULL AS finished, rolled_back_at IS NOT NULL AS rolled_back FROM _prisma_migrations ORDER BY started_at");
|
|
11
|
+
const applied = new Set();
|
|
12
|
+
const failed = new Set();
|
|
13
|
+
for (const row of rows) {
|
|
14
|
+
if (row.finished && !row.rolled_back)
|
|
15
|
+
applied.add(row.migration_name);
|
|
16
|
+
else if (!row.finished && !row.rolled_back)
|
|
17
|
+
failed.add(row.migration_name);
|
|
18
|
+
}
|
|
19
|
+
return { applied, failed: [...failed].filter((name) => !applied.has(name)) };
|
|
20
|
+
}
|
|
21
|
+
/** The migration folders (those holding a migration.sql), in the order Prisma applies them. */
|
|
22
|
+
export function listMigrationFolders(migrationsDir) {
|
|
23
|
+
return readdirSync(migrationsDir, { withFileTypes: true })
|
|
24
|
+
.filter((entry) => entry.isDirectory() &&
|
|
25
|
+
existsSync(join(migrationsDir, entry.name, "migration.sql")))
|
|
26
|
+
.map((entry) => entry.name)
|
|
27
|
+
.sort();
|
|
28
|
+
}
|
|
29
|
+
export function pendingMigrations(migrationsDir, state) {
|
|
30
|
+
return listMigrationFolders(migrationsDir).filter((name) => !state.applied.has(name));
|
|
31
|
+
}
|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MAAM,UAAU,oBAAoB,CAAC,aAAqB;IACxD,OAAO,WAAW,CAAC,aAAa,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC;SACvD,MAAM,CACL,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,WAAW,EAAE;QACnB,UAAU,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC,CAC/D;SACA,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC;SAC1B,IAAI,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,aAAqB,EACrB,KAAqB;IAErB,OAAO,oBAAoB,CAAC,aAAa,CAAC,CAAC,MAAM,CAC/C,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CACnC,CAAC;AACJ,CAAC"}
|
package/dist/sql-guard/run.d.ts
CHANGED
|
@@ -25,13 +25,18 @@ export interface MysqlConnectionLike {
|
|
|
25
25
|
query(sql: string): Promise<[unknown, unknown]>;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
28
|
+
* AK-89: the transaction half of `runReadOnlyPostgres`, shared with the migrate runner's pre-checks: `BEGIN READ ONLY`,
|
|
29
|
+
* `SET LOCAL statement_timeout` and `standard_conforming_strings`, then `body`, then always `ROLLBACK`. What `body`
|
|
30
|
+
* reads or fails on is the caller's; the transaction never commits.
|
|
31
31
|
*
|
|
32
32
|
* AK-57: `standard_conforming_strings = on` is pinned because the guard's parser reads a quote closed by a
|
|
33
33
|
* backslash as a complete string; a server with it off reads a backslash as an escape and sees a different statement.
|
|
34
34
|
*/
|
|
35
|
+
export declare function inReadOnlyPostgresTransaction<T>(client: PostgresClientLike, timeoutMs: number, body: () => Promise<T>): Promise<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Postgres: the statement as a `NO SCROLL` cursor in a read-only transaction (`inReadOnlyPostgresTransaction`), from
|
|
38
|
+
* which `maxRows + 1` rows are fetched, so the cap needs no rewrite of the SQL.
|
|
39
|
+
*/
|
|
35
40
|
export declare function runReadOnlyPostgres<Row = Record<string, unknown>>(client: PostgresClientLike, statement: GuardedSql, options: ReadOnlyRunOptions): Promise<ReadOnlyResult<Row>>;
|
|
36
41
|
/**
|
|
37
42
|
* MySQL: `START TRANSACTION READ ONLY` with the session's `max_execution_time` and
|
package/dist/sql-guard/run.js
CHANGED
|
@@ -20,28 +20,37 @@ function requireGuarded(statement, dialect) {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* AK-89: the transaction half of `runReadOnlyPostgres`, shared with the migrate runner's pre-checks: `BEGIN READ ONLY`,
|
|
24
|
+
* `SET LOCAL statement_timeout` and `standard_conforming_strings`, then `body`, then always `ROLLBACK`. What `body`
|
|
25
|
+
* reads or fails on is the caller's; the transaction never commits.
|
|
26
26
|
*
|
|
27
27
|
* AK-57: `standard_conforming_strings = on` is pinned because the guard's parser reads a quote closed by a
|
|
28
28
|
* backslash as a complete string; a server with it off reads a backslash as an escape and sees a different statement.
|
|
29
29
|
*/
|
|
30
|
-
export async function
|
|
31
|
-
requireGuarded(statement, "postgres");
|
|
32
|
-
validate(options);
|
|
30
|
+
export async function inReadOnlyPostgresTransaction(client, timeoutMs, body) {
|
|
33
31
|
await client.query("BEGIN READ ONLY");
|
|
34
32
|
try {
|
|
35
|
-
await client.query(`SET LOCAL statement_timeout = ${
|
|
33
|
+
await client.query(`SET LOCAL statement_timeout = ${timeoutMs}`);
|
|
36
34
|
await client.query("SET LOCAL standard_conforming_strings = on");
|
|
37
|
-
await
|
|
38
|
-
const fetched = await client.query(`FETCH FORWARD ${options.maxRows + 1} FROM guarded_rows`);
|
|
39
|
-
return capped(fetched.rows, options.maxRows);
|
|
35
|
+
return await body();
|
|
40
36
|
}
|
|
41
37
|
finally {
|
|
42
38
|
await client.query("ROLLBACK");
|
|
43
39
|
}
|
|
44
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Postgres: the statement as a `NO SCROLL` cursor in a read-only transaction (`inReadOnlyPostgresTransaction`), from
|
|
43
|
+
* which `maxRows + 1` rows are fetched, so the cap needs no rewrite of the SQL.
|
|
44
|
+
*/
|
|
45
|
+
export async function runReadOnlyPostgres(client, statement, options) {
|
|
46
|
+
requireGuarded(statement, "postgres");
|
|
47
|
+
validate(options);
|
|
48
|
+
return inReadOnlyPostgresTransaction(client, options.timeoutMs, async () => {
|
|
49
|
+
await client.query(`DECLARE guarded_rows NO SCROLL CURSOR FOR ${statement.sql}\n`);
|
|
50
|
+
const fetched = await client.query(`FETCH FORWARD ${options.maxRows + 1} FROM guarded_rows`);
|
|
51
|
+
return capped(fetched.rows, options.maxRows);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
45
54
|
/**
|
|
46
55
|
* MySQL: `START TRANSACTION READ ONLY` with the session's `max_execution_time` and
|
|
47
56
|
* `sql_select_limit` (the row cap, `maxRows + 1`, applied by the server to the top-level SELECT)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/sql-guard/run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAuBxC,SAAS,QAAQ,CAAC,EAAE,SAAS,EAAE,OAAO,EAAsB;IAC1D,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,SAAS,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAU,EAAE,CAAC;QACtF,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,UAAU,CAAC,GAAG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CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;AAC/C,CAAC;AAED,MAAM,4BAA4B,GAAG,IAAI,GAAG,CAAC,CAAC,sBAAsB,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC,CAAC;AAE9F,oFAAoF;AACpF,SAAS,SAAS,CAAC,KAAa;IAC9B,OAAO,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC;AAC5C,CAAC;AAED,SAAS,MAAM,CAAM,IAAW,EAAE,OAAe;IAC/C,OAAO,IAAI,CAAC,MAAM,GAAG,OAAO;QAC1B,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE;QACnD,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACjC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flytedesk/app-kit",
|
|
3
|
-
"version": "16.
|
|
3
|
+
"version": "16.7.0",
|
|
4
4
|
"description": "Shared platform kit for flytedesk apps: flytedesk-id auth (BFF/OIDC client) and a Postgres-native trace/audit layer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"spa-server": "./dist/spa-server/cli.js",
|
|
35
35
|
"app-kit-chat-mcp": "./dist/chat/mcp/entry.js",
|
|
36
36
|
"app-kit-repo-check": "./dist/repo-check/cli.js",
|
|
37
|
+
"app-kit-migrate": "./dist/migrate/cli.js",
|
|
37
38
|
"auth-sync": "./dist/cli/auth-sync.js",
|
|
38
39
|
"owed-reply-sync": "./dist/cli/owed-reply-sync.js"
|
|
39
40
|
},
|
|
@@ -154,12 +155,16 @@
|
|
|
154
155
|
"peerDependencies": {
|
|
155
156
|
"@prisma/client": "^7",
|
|
156
157
|
"fastify": "^4 || ^5",
|
|
158
|
+
"pg": "^8",
|
|
157
159
|
"react": "^18 || ^19"
|
|
158
160
|
},
|
|
159
161
|
"peerDependenciesMeta": {
|
|
160
162
|
"@prisma/client": {
|
|
161
163
|
"optional": true
|
|
162
164
|
},
|
|
165
|
+
"pg": {
|
|
166
|
+
"optional": true
|
|
167
|
+
},
|
|
163
168
|
"react": {
|
|
164
169
|
"optional": true
|
|
165
170
|
}
|