@flytedesk/app-kit 16.5.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 +75 -0
- package/dist/auth/client/browserEnvironment.js +3 -6
- package/dist/auth/client/browserEnvironment.js.map +1 -1
- package/dist/auth/client/buildWatch.d.ts +29 -0
- package/dist/auth/client/buildWatch.js +69 -0
- package/dist/auth/client/buildWatch.js.map +1 -0
- package/dist/auth/client/httpClient.d.ts +17 -12
- package/dist/auth/client/httpClient.js +57 -6
- package/dist/auth/client/httpClient.js.map +1 -1
- package/dist/auth/client/index.d.ts +1 -1
- package/dist/auth/client/index.js +1 -1
- package/dist/auth/client/index.js.map +1 -1
- package/dist/auth/client/testing.d.ts +1 -1
- package/dist/auth/client/testing.js +3 -2
- package/dist/auth/client/testing.js.map +1 -1
- package/dist/build/client/buildStore.d.ts +21 -0
- package/dist/build/client/buildStore.js +35 -0
- package/dist/build/client/buildStore.js.map +1 -0
- package/dist/build/client/index.d.ts +8 -0
- package/dist/build/client/index.js +9 -0
- package/dist/build/client/index.js.map +1 -0
- package/dist/build/client/useNewerBuildLive.d.ts +3 -0
- package/dist/build/client/useNewerBuildLive.js +8 -0
- package/dist/build/client/useNewerBuildLive.js.map +1 -0
- package/dist/build/index.d.ts +21 -0
- package/dist/build/index.js +50 -0
- package/dist/build/index.js.map +1 -0
- package/dist/build/shared.d.ts +14 -0
- package/dist/build/shared.js +11 -0
- package/dist/build/shared.js.map +1 -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/page/tabVisibility.d.ts +3 -0
- package/dist/page/tabVisibility.js +14 -0
- package/dist/page/tabVisibility.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 +14 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNewerBuildLive.js","sourceRoot":"","sources":["../../../src/build/client/useNewerBuildLive.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;yEACyE;AACzE,MAAM,UAAU,iBAAiB;IAC/B,OAAO,KAAK,CAAC,oBAAoB,CAAC,UAAU,CAAC,SAAS,EAAE,UAAU,CAAC,WAAW,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;AAC1G,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { BUILD_HEADER, BUILD_ROUTE_PATH, type BuildProbeBody } from "./shared.js";
|
|
2
|
+
export interface BuildPluginOptions {
|
|
3
|
+
/** The deployed commit this server runs. Required and never defaulted: a server that
|
|
4
|
+
* cannot name its build must not start. */
|
|
5
|
+
build: string;
|
|
6
|
+
/** The URL prefix the app's api client's `baseUrl` points at (e.g. "/api/v1"): the probe
|
|
7
|
+
* route is mounted at `${routePrefix}${BUILD_ROUTE_PATH}`. */
|
|
8
|
+
routePrefix: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Register once at the ROOT Fastify scope: `app.register(buildPlugin, { build, routePrefix })`.
|
|
12
|
+
* Registration order against the app's other scopes does not matter: Fastify's `addHook`
|
|
13
|
+
* hands a root hook to the child scopes that already exist (fastify.js `_addHook`).
|
|
14
|
+
* Its `onSend` hook sets `BUILD_HEADER` on every answer of the app (success, error, 404,
|
|
15
|
+
* the SPA fallback, a streamed body sent through `reply.send`), and it mounts the
|
|
16
|
+
* unauthenticated probe `GET ${routePrefix}${BUILD_ROUTE_PATH}` at the root, outside every
|
|
17
|
+
* auth hook. When the answer is cross-origin readable (CORS allowed its origin) the
|
|
18
|
+
* header's name is appended to `Access-Control-Expose-Headers`, so the page can read it.
|
|
19
|
+
* No route sets the header itself.
|
|
20
|
+
*/
|
|
21
|
+
export declare const buildPlugin: import("fastify").FastifyPluginCallback<BuildPluginOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fp from "fastify-plugin";
|
|
2
|
+
import { BUILD_HEADER, BUILD_ROUTE_PATH } from "./shared.js";
|
|
3
|
+
export { BUILD_HEADER, BUILD_ROUTE_PATH } from "./shared.js";
|
|
4
|
+
const EXPOSE_HEADERS = "access-control-expose-headers";
|
|
5
|
+
const ALLOW_ORIGIN = "access-control-allow-origin";
|
|
6
|
+
/** The header's current value with `name` added, unless it is already listed. */
|
|
7
|
+
function withExposedHeader(current, name) {
|
|
8
|
+
const listed = (Array.isArray(current) ? current.join(",") : String(current ?? ""))
|
|
9
|
+
.split(",")
|
|
10
|
+
.map((item) => item.trim())
|
|
11
|
+
.filter((item) => item !== "");
|
|
12
|
+
if (listed.includes("*") || listed.some((item) => item.toLowerCase() === name)) {
|
|
13
|
+
return listed.join(", ");
|
|
14
|
+
}
|
|
15
|
+
return [...listed, name].join(", ");
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Register once at the ROOT Fastify scope: `app.register(buildPlugin, { build, routePrefix })`.
|
|
19
|
+
* Registration order against the app's other scopes does not matter: Fastify's `addHook`
|
|
20
|
+
* hands a root hook to the child scopes that already exist (fastify.js `_addHook`).
|
|
21
|
+
* Its `onSend` hook sets `BUILD_HEADER` on every answer of the app (success, error, 404,
|
|
22
|
+
* the SPA fallback, a streamed body sent through `reply.send`), and it mounts the
|
|
23
|
+
* unauthenticated probe `GET ${routePrefix}${BUILD_ROUTE_PATH}` at the root, outside every
|
|
24
|
+
* auth hook. When the answer is cross-origin readable (CORS allowed its origin) the
|
|
25
|
+
* header's name is appended to `Access-Control-Expose-Headers`, so the page can read it.
|
|
26
|
+
* No route sets the header itself.
|
|
27
|
+
*/
|
|
28
|
+
export const buildPlugin = fp(async (app, options) => {
|
|
29
|
+
const { build, routePrefix } = options;
|
|
30
|
+
if (typeof build !== "string" || build.trim() === "") {
|
|
31
|
+
throw new Error("buildPlugin: `build` (the deployed commit) is required and must not be empty");
|
|
32
|
+
}
|
|
33
|
+
if (typeof routePrefix !== "string" || routePrefix.trim() === "") {
|
|
34
|
+
throw new Error("buildPlugin: `routePrefix` (the URL prefix of the api client's baseUrl) is required and must not be empty");
|
|
35
|
+
}
|
|
36
|
+
app.addHook("onSend", async (_request, reply, payload) => {
|
|
37
|
+
reply.header(BUILD_HEADER, build);
|
|
38
|
+
if (reply.hasHeader(ALLOW_ORIGIN)) {
|
|
39
|
+
const current = reply.getHeader(EXPOSE_HEADERS);
|
|
40
|
+
reply.header(EXPOSE_HEADERS, withExposedHeader(current, BUILD_HEADER));
|
|
41
|
+
}
|
|
42
|
+
return payload;
|
|
43
|
+
});
|
|
44
|
+
app.get(`${routePrefix}${BUILD_ROUTE_PATH}`, async (_request, reply) => {
|
|
45
|
+
// AK-88: a cached answer would hide the deploy the probe exists to detect.
|
|
46
|
+
reply.header("cache-control", "no-store");
|
|
47
|
+
return { build };
|
|
48
|
+
});
|
|
49
|
+
}, { name: "@flytedesk/app-kit/build", fastify: "4.x || 5.x" });
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/build/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAChC,OAAO,E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,KAAK,CAAC,8EAA8E,CAAC,CAAC;IAClG,CAAC;IACD,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACjE,MAAM,IAAI,KAAK,CACb,2GAA2G,CAC5G,CAAC;IACJ,CAAC;IAED,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;QACvD,KAAK,CAAC,MAAM,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QAClC,IAAI,KAAK,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE,CAAC;YAClC,MAAM,OAAO,GAAG,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC;YAChD,KAAK,CAAC,MAAM,CAAC,cAAc,EAAE,iBAAiB,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC;QACzE,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC,CAAC;IAEH,GAAG,CAAC,GAAG,CAAC,GAAG,WAAW,GAAG,gBAAgB,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAA2B,EAAE;QAC9F,2EAA2E;QAC3E,KAAK,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;QAC1C,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC,EACD,EAAE,IAAI,EAAE,0BAA0B,EAAE,OAAO,EAAE,YAAY,EAAE,CAC5D,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the build plugin (`./index.ts`, server) and the build watch (`./client/`, browser)
|
|
3
|
+
* agree on (AK-88). Browser-safe: no Node import, so the page can import it.
|
|
4
|
+
*/
|
|
5
|
+
/** The response header that names the build (deployed commit) of the app server that
|
|
6
|
+
* answered, set on every answer of an app that registers the build plugin. */
|
|
7
|
+
export declare const BUILD_HEADER = "x-app-build";
|
|
8
|
+
/** The path of the build probe, joined to the api's URL prefix: the server mounts
|
|
9
|
+
* `${routePrefix}${BUILD_ROUTE_PATH}`, the api client fetches `${baseUrl}${BUILD_ROUTE_PATH}`. */
|
|
10
|
+
export declare const BUILD_ROUTE_PATH = "/build";
|
|
11
|
+
/** The JSON body the probe answers. */
|
|
12
|
+
export interface BuildProbeBody {
|
|
13
|
+
build: string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the build plugin (`./index.ts`, server) and the build watch (`./client/`, browser)
|
|
3
|
+
* agree on (AK-88). Browser-safe: no Node import, so the page can import it.
|
|
4
|
+
*/
|
|
5
|
+
/** The response header that names the build (deployed commit) of the app server that
|
|
6
|
+
* answered, set on every answer of an app that registers the build plugin. */
|
|
7
|
+
export const BUILD_HEADER = "x-app-build";
|
|
8
|
+
/** The path of the build probe, joined to the api's URL prefix: the server mounts
|
|
9
|
+
* `${routePrefix}${BUILD_ROUTE_PATH}`, the api client fetches `${baseUrl}${BUILD_ROUTE_PATH}`. */
|
|
10
|
+
export const BUILD_ROUTE_PATH = "/build";
|
|
11
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/build/shared.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;+EAC+E;AAC/E,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,CAAC;AAE1C;mGACmG;AACnG,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAQ,CAAC"}
|
|
@@ -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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MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACnC,OAAO,QAAQ,KAAK,SAAS;QAC3B,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,UAAU,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,M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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 code;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/migrate/runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,KAAK,MAAM,aAAa,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEnE;;;;GAIG;AACH,MAAM,eAAe,GAAG,mBAAmB,CAAC;AAC5C,MAAM,qBAAqB,GAAG,cAAc,CAAC;AAW7C,SAAS,SAAS,CAAC,IAAc,EAAE,EAAE,GAAG,EAAE,GAAG,EAAkB;IAC7D,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC,EAAE;QACpD,GAAG;QACH,GAAG;QACH,KAAK,EAAE,SAAS;KACjB,CAAC,CAAC;IACH,IAAI,MAAM,CAAC,KAAK;QACd,MAAM,IAAI,KAAK,CACb,6BAA6B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,CACxE,CAAC;IACJ,OAAO,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;AAC5B,CAAC;AAED,KAAK,UAAU,UAAU,CACvB,GAAW,EACX,GAAmC;I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AAC,CAAC,CAAC,CAAC,MAAM,CAAC;QACvE,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;QACzD,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
|
+
}
|
|
32
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/migrate/state.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAUjC,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,MAA0B;IAE1B,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,KAAK,CAChC,iEAAiE,CAClE,CAAC;IACF,IAAI,CAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAA0B,CAAC,OAAO;QACpD,OAAO,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;IAC5C,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,KAAK,CACjC,mJAAmJ,CACpJ,CAAC;IACF,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;IACjC,KAAK,MAAM,GAAG,IAAI,IAIhB,EAAE,CAAC;QACH,IAAI,GAAG,CAAC,QAAQ,IAAI,CAAC,GAAG,CAAC,WAAW;YAAE,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;aACjE,IAAI,CAAC,GAAG,CAAC,QAAQ,IAAI,CAAC,GAAG,CAAC,WAAW;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAC7E,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;AAC/E,CAAC;AAED,+FAA+F;AAC/F,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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Calls `listener` whenever a hidden tab becomes visible again. Returns the stop
|
|
2
|
+
* function; outside a browser (no `document`) it listens to nothing. */
|
|
3
|
+
export function onTabVisible(listener) {
|
|
4
|
+
if (typeof document === "undefined")
|
|
5
|
+
return () => { };
|
|
6
|
+
const tab = document;
|
|
7
|
+
const onChange = () => {
|
|
8
|
+
if (tab.visibilityState === "visible")
|
|
9
|
+
listener();
|
|
10
|
+
};
|
|
11
|
+
tab.addEventListener("visibilitychange", onChange);
|
|
12
|
+
return () => tab.removeEventListener("visibilitychange", onChange);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=tabVisibility.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tabVisibility.js","sourceRoot":"","sources":["../../src/page/tabVisibility.ts"],"names":[],"mappings":"AAcA;yEACyE;AACzE,MAAM,UAAU,YAAY,CAAC,QAAoB;IAC/C,IAAI,OAAO,QAAQ,KAAK,WAAW;QAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IACrD,MAAM,GAAG,GAAG,QAAQ,CAAC;IACrB,MAAM,QAAQ,GAAG,GAAG,EAAE;QACpB,IAAI,GAAG,CAAC,eAAe,KAAK,SAAS;YAAE,QAAQ,EAAE,CAAC;IACpD,CAAC,CAAC;IACF,GAAG,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAC;IACnD,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,mBAAmB,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAC;AACrE,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
|