@softure-ai/db 0.0.0-stage → 0.1.6
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/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +383 -2
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +5 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/command.d.ts +8 -0
- package/dist/cli/command.d.ts.map +1 -0
- package/dist/cli/command.js +38 -0
- package/dist/cli/command.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +4 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/run.d.ts +27 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +205 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/client.d.ts +73 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +90 -0
- package/dist/client.js.map +1 -0
- package/dist/configured.d.ts +31 -0
- package/dist/configured.d.ts.map +1 -0
- package/dist/configured.js +74 -0
- package/dist/configured.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/migrations/adopt.d.ts +33 -0
- package/dist/migrations/adopt.d.ts.map +1 -0
- package/dist/migrations/adopt.js +97 -0
- package/dist/migrations/adopt.js.map +1 -0
- package/dist/migrations/apply.d.ts +35 -0
- package/dist/migrations/apply.d.ts.map +1 -0
- package/dist/migrations/apply.js +115 -0
- package/dist/migrations/apply.js.map +1 -0
- package/dist/migrations/export.d.ts +10 -0
- package/dist/migrations/export.d.ts.map +1 -0
- package/dist/migrations/export.js +58 -0
- package/dist/migrations/export.js.map +1 -0
- package/dist/migrations/exported.d.ts +20 -0
- package/dist/migrations/exported.d.ts.map +1 -0
- package/dist/migrations/exported.js +61 -0
- package/dist/migrations/exported.js.map +1 -0
- package/dist/migrations/files.d.ts +21 -0
- package/dist/migrations/files.d.ts.map +1 -0
- package/dist/migrations/files.js +112 -0
- package/dist/migrations/files.js.map +1 -0
- package/dist/migrations/introspect.d.ts +6 -0
- package/dist/migrations/introspect.d.ts.map +1 -0
- package/dist/migrations/introspect.js +117 -0
- package/dist/migrations/introspect.js.map +1 -0
- package/dist/migrations/ledger.d.ts +27 -0
- package/dist/migrations/ledger.d.ts.map +1 -0
- package/dist/migrations/ledger.js +45 -0
- package/dist/migrations/ledger.js.map +1 -0
- package/dist/migrations/migrator.d.ts +97 -0
- package/dist/migrations/migrator.d.ts.map +1 -0
- package/dist/migrations/migrator.js +301 -0
- package/dist/migrations/migrator.js.map +1 -0
- package/dist/migrations/problems.d.ts +93 -0
- package/dist/migrations/problems.d.ts.map +1 -0
- package/dist/migrations/problems.js +62 -0
- package/dist/migrations/problems.js.map +1 -0
- package/dist/migrations/reference.d.ts +23 -0
- package/dist/migrations/reference.d.ts.map +1 -0
- package/dist/migrations/reference.js +64 -0
- package/dist/migrations/reference.js.map +1 -0
- package/dist/migrations/session.d.ts +15 -0
- package/dist/migrations/session.d.ts.map +1 -0
- package/dist/migrations/session.js +56 -0
- package/dist/migrations/session.js.map +1 -0
- package/dist/migrations/stubs.d.ts +23 -0
- package/dist/migrations/stubs.d.ts.map +1 -0
- package/dist/migrations/stubs.js +99 -0
- package/dist/migrations/stubs.js.map +1 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +32 -0
- package/dist/shared.js.map +1 -0
- package/dist/testing.d.ts +23 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +62 -0
- package/dist/testing.js.map +1 -0
- package/package.json +73 -4
- package/src/cli/bin.ts +5 -0
- package/src/cli/command.ts +47 -0
- package/src/cli/index.ts +3 -0
- package/src/cli/run.ts +250 -0
- package/src/client.ts +151 -0
- package/src/configured.ts +109 -0
- package/src/index.ts +46 -0
- package/src/migrations/adopt.ts +136 -0
- package/src/migrations/apply.ts +135 -0
- package/src/migrations/export.ts +66 -0
- package/src/migrations/exported.ts +82 -0
- package/src/migrations/files.ts +129 -0
- package/src/migrations/introspect.ts +165 -0
- package/src/migrations/ledger.ts +75 -0
- package/src/migrations/migrator.ts +403 -0
- package/src/migrations/problems.ts +99 -0
- package/src/migrations/reference.ts +78 -0
- package/src/migrations/session.ts +74 -0
- package/src/migrations/stubs.ts +139 -0
- package/src/shared.ts +36 -0
- package/src/testing.ts +80 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Err, Ok } from "@softure-ai/core";
|
|
2
|
+
export type MigrationProblem = {
|
|
3
|
+
readonly code: "db.invalid_migration_file";
|
|
4
|
+
readonly module: string;
|
|
5
|
+
readonly file: string;
|
|
6
|
+
readonly reason: string;
|
|
7
|
+
} | {
|
|
8
|
+
readonly code: "db.migrations_unreadable";
|
|
9
|
+
readonly module: string;
|
|
10
|
+
readonly dir: string;
|
|
11
|
+
readonly reason: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly code: "db.migration_changed";
|
|
14
|
+
readonly module: string;
|
|
15
|
+
readonly version: number;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
} | {
|
|
18
|
+
readonly code: "db.migration_missing";
|
|
19
|
+
readonly module: string;
|
|
20
|
+
readonly version: number;
|
|
21
|
+
readonly name: string;
|
|
22
|
+
} | {
|
|
23
|
+
readonly code: "db.migration_out_of_order";
|
|
24
|
+
readonly module: string;
|
|
25
|
+
readonly version: number;
|
|
26
|
+
readonly appliedVersion: number;
|
|
27
|
+
} | {
|
|
28
|
+
readonly code: "db.migration_failed";
|
|
29
|
+
readonly module: string;
|
|
30
|
+
readonly version: number;
|
|
31
|
+
readonly name: string;
|
|
32
|
+
readonly reason: string;
|
|
33
|
+
} | {
|
|
34
|
+
readonly code: "db.app_migration_failed";
|
|
35
|
+
readonly phase: "before" | "after";
|
|
36
|
+
readonly reason: string;
|
|
37
|
+
} | {
|
|
38
|
+
readonly code: "db.reserved_module";
|
|
39
|
+
readonly module: string;
|
|
40
|
+
} | {
|
|
41
|
+
readonly code: "db.migrations_without_schema";
|
|
42
|
+
readonly module: string;
|
|
43
|
+
} | {
|
|
44
|
+
readonly code: "db.dependency_cycle";
|
|
45
|
+
readonly modules: readonly string[];
|
|
46
|
+
} | {
|
|
47
|
+
readonly code: "db.adopt_unknown_module";
|
|
48
|
+
readonly module: string;
|
|
49
|
+
} | {
|
|
50
|
+
readonly code: "db.adopt_version_mismatch";
|
|
51
|
+
readonly module: string;
|
|
52
|
+
readonly requested: string;
|
|
53
|
+
readonly enabled: string;
|
|
54
|
+
} | {
|
|
55
|
+
readonly code: "db.adopt_already_applied";
|
|
56
|
+
readonly module: string;
|
|
57
|
+
} | {
|
|
58
|
+
readonly code: "db.adopt_dependency_pending";
|
|
59
|
+
readonly module: string;
|
|
60
|
+
readonly dependency: string;
|
|
61
|
+
} | {
|
|
62
|
+
readonly code: "db.adopt_no_schema";
|
|
63
|
+
readonly module: string;
|
|
64
|
+
} | {
|
|
65
|
+
readonly code: "db.adopt_through_out_of_range";
|
|
66
|
+
readonly module: string;
|
|
67
|
+
readonly through: number;
|
|
68
|
+
readonly files: number;
|
|
69
|
+
} | {
|
|
70
|
+
readonly code: "db.adopt_reference_failed";
|
|
71
|
+
readonly module: string;
|
|
72
|
+
readonly reason: string;
|
|
73
|
+
} | {
|
|
74
|
+
readonly code: "db.export_target_not_empty";
|
|
75
|
+
readonly module: string;
|
|
76
|
+
readonly dir: string;
|
|
77
|
+
readonly entries: readonly string[];
|
|
78
|
+
} | {
|
|
79
|
+
readonly code: "db.schema_mismatch";
|
|
80
|
+
readonly module: string;
|
|
81
|
+
readonly schema: string;
|
|
82
|
+
readonly differences: readonly string[];
|
|
83
|
+
};
|
|
84
|
+
export type MigrationErrorCode = MigrationProblem["code"];
|
|
85
|
+
/** A failed migration operation: the code of its first problem, and every problem found. */
|
|
86
|
+
export interface MigrationFailure extends Err<MigrationErrorCode> {
|
|
87
|
+
readonly problems: readonly MigrationProblem[];
|
|
88
|
+
}
|
|
89
|
+
export type MigrationResult<T> = Ok<T> | MigrationFailure;
|
|
90
|
+
export declare function failWith(problems: readonly MigrationProblem[]): MigrationFailure;
|
|
91
|
+
/** One English line per problem, naming the module and the file or object involved. */
|
|
92
|
+
export declare function describeProblem(problem: MigrationProblem): string;
|
|
93
|
+
//# sourceMappingURL=problems.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"problems.d.ts","sourceRoot":"","sources":["../../src/migrations/problems.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAEhD,MAAM,MAAM,gBAAgB,GACxB;IAAE,QAAQ,CAAC,IAAI,EAAE,2BAA2B,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACvH;IAAE,QAAQ,CAAC,IAAI,EAAE,0BAA0B,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrH;IAAE,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACnH;IAAE,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,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AE,CAAC;CAChD;AAED,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC;AAE1D,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,SAAS,gBAAgB,EAAE,GAAG,gBAAgB,CAMhF;AAED,uFAAuF;AACvF,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,MAAM,CA8CjE"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export function failWith(problems) {
|
|
2
|
+
const [first] = problems;
|
|
3
|
+
if (first === undefined) {
|
|
4
|
+
throw new Error("failWith: a migration failure needs at least one problem");
|
|
5
|
+
}
|
|
6
|
+
return { ok: false, error: first.code, problems };
|
|
7
|
+
}
|
|
8
|
+
/** One English line per problem, naming the module and the file or object involved. */
|
|
9
|
+
export function describeProblem(problem) {
|
|
10
|
+
switch (problem.code) {
|
|
11
|
+
case "db.invalid_migration_file":
|
|
12
|
+
return `${problem.module}: migration file ${problem.file} is invalid: ${problem.reason}`;
|
|
13
|
+
case "db.migrations_unreadable":
|
|
14
|
+
return `${problem.module}: cannot read the migrations folder ${problem.dir}: ${problem.reason}`;
|
|
15
|
+
case "db.migration_changed":
|
|
16
|
+
return `${problem.module}: applied migration ${formatFile(problem.version, problem.name)} was edited (checksum differs); add a new migration instead`;
|
|
17
|
+
case "db.migration_missing":
|
|
18
|
+
return `${problem.module}: applied migration ${formatFile(problem.version, problem.name)} is missing from the module`;
|
|
19
|
+
case "db.migration_out_of_order":
|
|
20
|
+
return `${problem.module}: migration ${formatVersion(problem.version)} is pending but ${formatVersion(problem.appliedVersion)} is already applied`;
|
|
21
|
+
case "db.migration_failed":
|
|
22
|
+
return `${problem.module}: migration ${formatFile(problem.version, problem.name)} failed and was rolled back: ${problem.reason}`;
|
|
23
|
+
case "db.app_migration_failed":
|
|
24
|
+
return problem.phase === "before"
|
|
25
|
+
? `app: the before migrations failed: ${problem.reason}; no module migration ran`
|
|
26
|
+
: `app: the after migrations failed: ${problem.reason}; the module migrations before it stay applied`;
|
|
27
|
+
case "db.reserved_module":
|
|
28
|
+
return `${problem.module}: uses a reserved id or schema (the id "softure"; the schemas softure, public, information_schema, drizzle, pg_* and names over 63 bytes)`;
|
|
29
|
+
case "db.migrations_without_schema":
|
|
30
|
+
return `${problem.module}: has migrations but no dbSchema in its manifest`;
|
|
31
|
+
case "db.dependency_cycle":
|
|
32
|
+
return `modules ${problem.modules.join(", ")} depend on each other in a cycle`;
|
|
33
|
+
case "db.adopt_unknown_module":
|
|
34
|
+
return `${problem.module}: cannot adopt a module that is not enabled in the config`;
|
|
35
|
+
case "db.adopt_version_mismatch":
|
|
36
|
+
return `${problem.module}: --adopt asks for ${problem.requested} but ${problem.enabled} is enabled`;
|
|
37
|
+
case "db.adopt_already_applied":
|
|
38
|
+
return `${problem.module}: already has migrations in the ledger; adopt only works on a module the ledger has never seen`;
|
|
39
|
+
case "db.adopt_dependency_pending":
|
|
40
|
+
return `${problem.module}: depends on ${problem.dependency}, whose pending migrations could not be applied (see above); adopt ${problem.dependency} first`;
|
|
41
|
+
case "db.adopt_no_schema":
|
|
42
|
+
return `${problem.module}: has no dbSchema or no migrations, so there is nothing to adopt`;
|
|
43
|
+
case "db.adopt_through_out_of_range":
|
|
44
|
+
return `${problem.module}: cannot adopt through ${String(problem.through)}; it is the number of the last file to adopt, a whole number from 1 to ${problem.files}`;
|
|
45
|
+
case "db.adopt_reference_failed":
|
|
46
|
+
return `${problem.module}: could not build the reference schema: ${problem.reason}`;
|
|
47
|
+
case "db.export_target_not_empty":
|
|
48
|
+
return `${problem.module}: ${problem.dir} holds other files (${problem.entries.join(", ")}); export into an empty folder`;
|
|
49
|
+
case "db.schema_mismatch":
|
|
50
|
+
return [
|
|
51
|
+
`${problem.module}: schema "${problem.schema}" differs from what its migrations create:`,
|
|
52
|
+
...problem.differences.map((difference) => ` ${difference}`),
|
|
53
|
+
].join("\n");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function formatVersion(version) {
|
|
57
|
+
return String(version).padStart(4, "0");
|
|
58
|
+
}
|
|
59
|
+
function formatFile(version, name) {
|
|
60
|
+
return `${formatVersion(version)}_${name}.sql`;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=problems.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"problems.js","sourceRoot":"","sources":["../../src/migrations/problems.ts"],"names":[],"mappings":"AAmCA,MAAM,UAAU,QAAQ,CAAC,QAAqC;IAC5D,MAAM,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC;IACzB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;IAC9E,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC;AACpD,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,2BAA2B;YAC9B,OAAO,GAAG,OAAO,CAAC,MAAM,oBAAoB,OAAO,CAAC,IAAI,gBAAgB,OAAO,CAAC,MAAM,EAAE,CAAC;QAC3F,KAAK,0BAA0B;YAC7B,OAAO,GAAG,OAAO,CAAC,MAAM,uCAAuC,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC;QAClG,KAAK,sBAAsB;YACzB,OAAO,GAAG,OAAO,CAAC,MAAM,uBAAuB,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,6DAA6D,CAAC;QACxJ,KAAK,sBAAsB;YACzB,OAAO,GAAG,OAAO,CAAC,MAAM,uBAAuB,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,6BAA6B,CAAC;QACxH,KAAK,2BAA2B;YAC9B,OAA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AM,0BAA0B,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,0EAA0E,OAAO,CAAC,KAAK,EAAE,CAAC;QACrK,KAAK,2BAA2B;YAC9B,OAAO,GAAG,OAAO,CAAC,MAAM,2CAA2C,OAAO,CAAC,MAAM,EAAE,CAAC;QACtF,KAAK,4BAA4B;YAC/B,OAAO,GAAG,OAAO,CAAC,MAAM,KAAK,OAAO,CAAC,GAAG,uBAAuB,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,gCAAgC,CAAC;QAC5H,KAAK,oBAAoB;YACvB,OAAO;gBACL,GAAG,OAAO,CAAC,MAAM,aAAa,OAAO,CAAC,MAAM,4CAA4C;gBACxF,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,KAAK,UAAU,EAAE,CAAC;aAC9D,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,OAAe;IACpC,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;AAC1C,CAAC;AAED,SAAS,UAAU,CAAC,OAAe,EAAE,IAAY;IAC/C,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type AnySoftureModule } from "@softure-ai/core";
|
|
2
|
+
import { type MigrationUnit } from "./apply.js";
|
|
3
|
+
import { type MigrationResult } from "./problems.js";
|
|
4
|
+
import { type AppCatalog } from "./stubs.js";
|
|
5
|
+
export interface ReferenceInput {
|
|
6
|
+
/** The enabled modules, for the dependency graph. */
|
|
7
|
+
readonly modules: readonly AnySoftureModule[];
|
|
8
|
+
/** Their prepared units (`prepareUnits`), the ledger first, in dependency order. */
|
|
9
|
+
readonly units: readonly MigrationUnit[];
|
|
10
|
+
/** The module whose schema is described. */
|
|
11
|
+
readonly target: MigrationUnit;
|
|
12
|
+
/** The last file of the target to run; its dependencies run in full. */
|
|
13
|
+
readonly through: number;
|
|
14
|
+
/** Stubs of the app's tables (`readAppTables`), created before any file runs. */
|
|
15
|
+
readonly appTables?: AppCatalog;
|
|
16
|
+
}
|
|
17
|
+
/** Every line `describeSchema` prints for the target's schema after its files 1..through. */
|
|
18
|
+
export declare function buildReferenceSchema(input: ReferenceInput): Promise<MigrationResult<string[]>>;
|
|
19
|
+
/** The schemas the migrator owns, which hold no app table: the ledger's, every unit's and drizzle's. */
|
|
20
|
+
export declare function listOwnedSchemas(units: readonly MigrationUnit[]): string[];
|
|
21
|
+
/** The ids of the target and every enabled module it depends on, directly or not. */
|
|
22
|
+
export declare function collectWithDependencies(modules: readonly AnySoftureModule[], target: string): Set<string>;
|
|
23
|
+
//# sourceMappingURL=reference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference.d.ts","sourceRoot":"","sources":["../../src/migrations/reference.ts"],"names":[],"mappings":"AAKA,OAAO,EAAM,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE7D,OAAO,EAAa,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAmC,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAEtF,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAK7D,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,QAAQ,CAAC,OAAO,EAAE,SAAS,gBAAgB,EAAE,CAAC;IAC9C,oFAAoF;IACpF,QAAQ,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,CAAC;IACzC,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,wEAAwE;IACxE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,iFAAiF;IACjF,QAAQ,CAAC,SAAS,CAAC,EAAE,UAAU,CAAC;CACjC;AAED,6FAA6F;AAC7F,wBAAsB,oBAAoB,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,CAuBpG;AAED,wGAAwG;AACxG,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,GAAG,MAAM,EAAE,CAE1E;AAED,qFAAqF;AACrF,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,SAAS,gBAAgB,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAWzG"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The reference schema for adoption: what a module's files 1..N create, built on a scratch
|
|
2
|
+
// in-memory PGlite together with the modules it depends on (directly or not), because its SQL may
|
|
3
|
+
// reference them. `adoptModule` and a baseline in `migrate` compare a live schema with it. Module
|
|
4
|
+
// SQL may also reference the app's own tables; their stubs, copied from the live database
|
|
5
|
+
// (`stubs.ts`), are created first (issue #170).
|
|
6
|
+
import { ok } from "@softure-ai/core";
|
|
7
|
+
import { createDatabase } from "../client.js";
|
|
8
|
+
import { applyFile } from "./apply.js";
|
|
9
|
+
import { describeSchema } from "./introspect.js";
|
|
10
|
+
import { LEDGER_MODULE_ID, LEDGER_SCHEMA } from "./ledger.js";
|
|
11
|
+
import { failWith } from "./problems.js";
|
|
12
|
+
import { withSession } from "./session.js";
|
|
13
|
+
import { createAppStubs } from "./stubs.js";
|
|
14
|
+
// drizzle's ledger schema (`drizzle.__drizzle_migrations`): never an app table a module references.
|
|
15
|
+
const APP_MIGRATOR_SCHEMA = "drizzle";
|
|
16
|
+
/** Every line `describeSchema` prints for the target's schema after its files 1..through. */
|
|
17
|
+
export async function buildReferenceSchema(input) {
|
|
18
|
+
const included = collectWithDependencies(input.modules, input.target.module);
|
|
19
|
+
const units = input.units
|
|
20
|
+
.filter((unit) => unit.module === LEDGER_MODULE_ID || included.has(unit.module))
|
|
21
|
+
.map((unit) => (unit.module === input.target.module ? { ...unit, files: unit.files.filter((file) => file.version <= input.through) } : unit));
|
|
22
|
+
const scratch = await createDatabase("pglite://");
|
|
23
|
+
try {
|
|
24
|
+
return await withSession(scratch, async (session) => {
|
|
25
|
+
const skipped = input.appTables === undefined ? [] : await createAppStubs(session, input.appTables);
|
|
26
|
+
for (const unit of units) {
|
|
27
|
+
for (const file of unit.files) {
|
|
28
|
+
const failure = await applyFile(session, { unit, file, method: "applied" });
|
|
29
|
+
if (failure !== null) {
|
|
30
|
+
const reason = describeFailure(failure) + (skipped.length > 0 ? ` (app table stubs skipped: ${skipped.join("; ")})` : "");
|
|
31
|
+
return failWith([{ code: "db.adopt_reference_failed", module: input.target.module, reason }]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return ok(await describeSchema(session, input.target.schema));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
await scratch.close();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** The schemas the migrator owns, which hold no app table: the ledger's, every unit's and drizzle's. */
|
|
43
|
+
export function listOwnedSchemas(units) {
|
|
44
|
+
return [...new Set([LEDGER_SCHEMA, APP_MIGRATOR_SCHEMA, ...units.map((unit) => unit.schema)])];
|
|
45
|
+
}
|
|
46
|
+
/** The ids of the target and every enabled module it depends on, directly or not. */
|
|
47
|
+
export function collectWithDependencies(modules, target) {
|
|
48
|
+
const byId = new Map(modules.map((module) => [module.id, module]));
|
|
49
|
+
const collected = new Set();
|
|
50
|
+
const visit = (id) => {
|
|
51
|
+
const module = byId.get(id);
|
|
52
|
+
if (module === undefined || collected.has(id))
|
|
53
|
+
return;
|
|
54
|
+
collected.add(id);
|
|
55
|
+
for (const dependency of Object.keys(module.manifest.dependsOn))
|
|
56
|
+
visit(dependency);
|
|
57
|
+
};
|
|
58
|
+
visit(target);
|
|
59
|
+
return collected;
|
|
60
|
+
}
|
|
61
|
+
function describeFailure(problem) {
|
|
62
|
+
return problem.code === "db.migration_failed" ? `${problem.module} ${problem.name}: ${problem.reason}` : problem.code;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=reference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference.js","sourceRoot":"","sources":["../../src/migrations/reference.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,kGAAkG;AAClG,kGAAkG;AAClG,0FAA0F;AAC1F,gDAAgD;AAChD,OAAO,EAAE,EAAE,EAAyB,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAsB,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAA+C,MAAM,eAAe,CAAC;AACtF,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAmB,MAAM,YAAY,CAAC;AAE7D,oGAAoG;AACpG,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAetC,6FAA6F;AAC7F,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,KAAqB;IAC9D,MAAM,QAAQ,GAAG,uBAAuB,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC7E,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK;SACtB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,KAAK,gBAAgB,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SAC/E,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,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,CAAC,MAAM,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC;AACxH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DatabaseHandle } from "../client.js";
|
|
2
|
+
export interface MigrationSession {
|
|
3
|
+
/** A parameterised statement; returns its rows. */
|
|
4
|
+
readonly query: <T>(text: string, params?: readonly unknown[]) => Promise<T[]>;
|
|
5
|
+
/** One or more statements without parameters (a migration file). */
|
|
6
|
+
readonly exec: (sql: string) => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Runs `run` on one dedicated connection. A pg client is destroyed afterwards instead of going
|
|
10
|
+
* back to the pool, so a `SET` inside a migration file or a lock that failed to release cannot
|
|
11
|
+
* leak into the app's queries. PGlite has one connection, shared with the app, so the settings the
|
|
12
|
+
* run changed are put back afterwards; the app's own (`TimeZone`, `search_path`) stay as they were.
|
|
13
|
+
*/
|
|
14
|
+
export declare function withSession<T>(handle: DatabaseHandle, run: (session: MigrationSession) => Promise<T>): Promise<T>;
|
|
15
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/migrations/session.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAInD,MAAM,WAAW,gBAAgB;IAC/B,mDAAmD;IACnD,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/E,oEAAoE;IACpE,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C;AASD;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAgCvH"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const SESSION_SETTINGS_SQL = "SELECT name, current_setting(name) AS value FROM pg_settings WHERE source = 'session'";
|
|
2
|
+
/**
|
|
3
|
+
* Runs `run` on one dedicated connection. A pg client is destroyed afterwards instead of going
|
|
4
|
+
* back to the pool, so a `SET` inside a migration file or a lock that failed to release cannot
|
|
5
|
+
* leak into the app's queries. PGlite has one connection, shared with the app, so the settings the
|
|
6
|
+
* run changed are put back afterwards; the app's own (`TimeZone`, `search_path`) stay as they were.
|
|
7
|
+
*/
|
|
8
|
+
export async function withSession(handle, run) {
|
|
9
|
+
if (handle.kind === "pglite") {
|
|
10
|
+
const { client } = handle;
|
|
11
|
+
const before = (await client.query(SESSION_SETTINGS_SQL)).rows;
|
|
12
|
+
try {
|
|
13
|
+
return await run({
|
|
14
|
+
query: async (text, params) => (await client.query(text, params ? [...params] : undefined)).rows,
|
|
15
|
+
exec: async (sql) => {
|
|
16
|
+
await client.exec(sql);
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
await restoreSessionSettings(client, before);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const client = await handle.pool.connect();
|
|
25
|
+
// A dropped connection makes pg emit 'error' on the client; without a listener Node crashes the
|
|
26
|
+
// process. The pending query rejects on its own, so the listener only has to exist.
|
|
27
|
+
const ignoreClientError = () => undefined;
|
|
28
|
+
client.on("error", ignoreClientError);
|
|
29
|
+
try {
|
|
30
|
+
return await run({
|
|
31
|
+
query: async (text, params) => (await client.query(text, params ? [...params] : undefined)).rows,
|
|
32
|
+
exec: async (sql) => {
|
|
33
|
+
await client.query(sql);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
client.release(true);
|
|
39
|
+
client.off("error", ignoreClientError);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Puts back every value in `before` (also one the run reset) and resets every session setting the run added. */
|
|
43
|
+
async function restoreSessionSettings(client, before) {
|
|
44
|
+
const after = new Map((await client.query(SESSION_SETTINGS_SQL)).rows.map((setting) => [setting.name, setting.value]));
|
|
45
|
+
for (const setting of before) {
|
|
46
|
+
if (after.get(setting.name) !== setting.value) {
|
|
47
|
+
await client.query("SELECT set_config($1, $2, false)", [setting.name, setting.value]);
|
|
48
|
+
}
|
|
49
|
+
after.delete(setting.name);
|
|
50
|
+
}
|
|
51
|
+
for (const name of after.keys()) {
|
|
52
|
+
// Names come from pg_settings, not from input; quoted as identifiers all the same.
|
|
53
|
+
await client.exec(`RESET "${name.replaceAll('"', '""')}"`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/migrations/session.ts"],"names":[],"mappings":"AAkBA,MAAM,oBAAoB,GAAG,uFAAuF,CAAC;AAErH;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAI,MAAsB,EAAE,GAA8C;IACzG,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;QAC1B,MAAM,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,KAAK,CAAiB,oBAAoB,CAAC,CAAC,CAAC,IAAI,CAAC;QAC/E,IAAI,CAAC;YACH,OAAO,MAAM,GAAG,CAAC;gBACf,KAAK,EAAE,KAAK,EAAK,IAAY,EAAE,MAA2B,EAAE,EAAE,CAAC,CAAC,MAAM,MAAM,CAAC,KAAK,CAAI,IAAI,EAAE,MAAM,CAAC,CAAC,CA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oB,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACvI,KAAK,MAAM,OAAO,IAAI,MAAM,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC;YAC9C,MAAM,MAAM,CAAC,KAAK,CAAC,kCAAkC,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QACxF,CAAC;QACD,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;QAChC,mFAAmF;QACnF,MAAM,MAAM,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MigrationSession } from "./session.js";
|
|
2
|
+
/** One DDL statement for the scratch database, with the object it creates for reports. */
|
|
3
|
+
export interface StubStatement {
|
|
4
|
+
/** `schema.table`, `schema.table.column`, `schema.table.constraint` or `schema.type`, unquoted. */
|
|
5
|
+
readonly object: string;
|
|
6
|
+
readonly sql: string;
|
|
7
|
+
}
|
|
8
|
+
/** The app's tables as statements to replay, in a runnable order. */
|
|
9
|
+
export interface AppCatalog {
|
|
10
|
+
readonly statements: readonly StubStatement[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The app's tables in every schema except `pg_*`, `information_schema` and `excludedSchemas` (the
|
|
14
|
+
* ledger, the enabled modules' schemas, the app migrator's own). Runs in its own read-only transaction.
|
|
15
|
+
*/
|
|
16
|
+
export declare function readAppTables(session: MigrationSession, excludedSchemas: readonly string[]): Promise<AppCatalog>;
|
|
17
|
+
/**
|
|
18
|
+
* Replays the catalog on the scratch database, each statement on its own: a statement that fails
|
|
19
|
+
* (a type the scratch lacks, a key on a skipped column) is skipped, so a module that never
|
|
20
|
+
* references that table still builds its reference. Returns the skipped objects with the reason.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createAppStubs(session: MigrationSession, catalog: AppCatalog): Promise<string[]>;
|
|
23
|
+
//# sourceMappingURL=stubs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stubs.d.ts","sourceRoot":"","sources":["../../src/migrations/stubs.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,0FAA0F;AAC1F,MAAM,WAAW,aAAa;IAC5B,mGAAmG;IACnG,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,qEAAqE;AACrE,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,UAAU,EAAE,SAAS,aAAa,EAAE,CAAC;CAC/C;AA2ED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAuBtH;AAED;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAUtG"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Names come back quoted by Postgres (`quote_ident`, `format('%I')`), so no DDL below concatenates
|
|
2
|
+
// a raw identifier. Each query takes the excluded schemas as $1.
|
|
3
|
+
const IN_APP_SCHEMA = (alias) => `${alias}.nspname <> ALL($1::text[]) AND ${alias}.nspname <> 'information_schema' AND ${alias}.nspname NOT LIKE 'pg\\_%'`;
|
|
4
|
+
const NOT_IN_EXTENSION = (catalog, alias) => `NOT EXISTS (SELECT 1 FROM pg_depend dep WHERE dep.classid = '${catalog}'::regclass AND dep.objid = ${alias}.oid AND dep.deptype = 'e')`;
|
|
5
|
+
const QUERIES = {
|
|
6
|
+
schemas: `
|
|
7
|
+
SELECT DISTINCT n.nspname AS name, quote_ident(n.nspname) AS quoted
|
|
8
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
9
|
+
WHERE c.relkind IN ('r', 'p') AND ${IN_APP_SCHEMA("n")} AND ${NOT_IN_EXTENSION("pg_class", "c")}
|
|
10
|
+
ORDER BY 1`,
|
|
11
|
+
enums: `
|
|
12
|
+
SELECT n.nspname || '.' || t.typname AS object, format('%I.%I', n.nspname, t.typname) AS quoted,
|
|
13
|
+
(SELECT string_agg(quote_literal(e.enumlabel), ', ' ORDER BY e.enumsortorder) FROM pg_enum e WHERE e.enumtypid = t.oid) AS labels
|
|
14
|
+
FROM pg_type t JOIN pg_namespace n ON n.oid = t.typnamespace
|
|
15
|
+
WHERE t.typtype = 'e' AND ${IN_APP_SCHEMA("n")} AND ${NOT_IN_EXTENSION("pg_type", "t")}
|
|
16
|
+
ORDER BY 1`,
|
|
17
|
+
tables: `
|
|
18
|
+
SELECT n.nspname || '.' || c.relname AS object, format('%I.%I', n.nspname, c.relname) AS quoted
|
|
19
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
20
|
+
WHERE c.relkind IN ('r', 'p') AND ${IN_APP_SCHEMA("n")} AND ${NOT_IN_EXTENSION("pg_class", "c")}
|
|
21
|
+
ORDER BY 1`,
|
|
22
|
+
columns: `
|
|
23
|
+
SELECT n.nspname || '.' || c.relname AS table_object, format('%I.%I', n.nspname, c.relname) AS table_quoted,
|
|
24
|
+
a.attname AS name, quote_ident(a.attname) AS quoted, format_type(a.atttypid, a.atttypmod) AS type
|
|
25
|
+
FROM pg_attribute a
|
|
26
|
+
JOIN pg_class c ON c.oid = a.attrelid
|
|
27
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
28
|
+
WHERE c.relkind IN ('r', 'p') AND a.attnum > 0 AND NOT a.attisdropped
|
|
29
|
+
AND ${IN_APP_SCHEMA("n")} AND ${NOT_IN_EXTENSION("pg_class", "c")}
|
|
30
|
+
ORDER BY 1, a.attnum`,
|
|
31
|
+
keys: `
|
|
32
|
+
SELECT n.nspname || '.' || c.relname AS table_object, format('%I.%I', n.nspname, c.relname) AS table_quoted,
|
|
33
|
+
con.conname AS name, quote_ident(con.conname) AS quoted, pg_get_constraintdef(con.oid) AS definition
|
|
34
|
+
FROM pg_constraint con
|
|
35
|
+
JOIN pg_class c ON c.oid = con.conrelid
|
|
36
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
37
|
+
WHERE con.contype IN ('p', 'u') AND c.relkind IN ('r', 'p')
|
|
38
|
+
AND ${IN_APP_SCHEMA("n")} AND ${NOT_IN_EXTENSION("pg_class", "c")}
|
|
39
|
+
ORDER BY 1, con.contype, 3`,
|
|
40
|
+
// A unique index that backs no key constraint. A foreign key records the index it references in
|
|
41
|
+
// `conindid` too, so only key constraints count.
|
|
42
|
+
uniqueIndexes: `
|
|
43
|
+
SELECT n.nspname || '.' || c.relname AS table_object, i.relname AS name, pg_get_indexdef(i.oid) AS definition
|
|
44
|
+
FROM pg_index x
|
|
45
|
+
JOIN pg_class i ON i.oid = x.indexrelid
|
|
46
|
+
JOIN pg_class c ON c.oid = x.indrelid
|
|
47
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
48
|
+
WHERE x.indisunique AND x.indpred IS NULL AND x.indexprs IS NULL AND c.relkind IN ('r', 'p')
|
|
49
|
+
AND NOT EXISTS (SELECT 1 FROM pg_constraint con WHERE con.conindid = x.indexrelid AND con.contype IN ('p', 'u', 'x'))
|
|
50
|
+
AND ${IN_APP_SCHEMA("n")} AND ${NOT_IN_EXTENSION("pg_class", "c")}
|
|
51
|
+
ORDER BY 1, 2`,
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The app's tables in every schema except `pg_*`, `information_schema` and `excludedSchemas` (the
|
|
55
|
+
* ledger, the enabled modules' schemas, the app migrator's own). Runs in its own read-only transaction.
|
|
56
|
+
*/
|
|
57
|
+
export async function readAppTables(session, excludedSchemas) {
|
|
58
|
+
await session.exec("BEGIN READ ONLY; SET LOCAL search_path = pg_catalog;");
|
|
59
|
+
try {
|
|
60
|
+
const params = [[...excludedSchemas]];
|
|
61
|
+
const schemas = await session.query(QUERIES.schemas, params);
|
|
62
|
+
const enums = await session.query(QUERIES.enums, params);
|
|
63
|
+
const tables = await session.query(QUERIES.tables, params);
|
|
64
|
+
const columns = await session.query(QUERIES.columns, params);
|
|
65
|
+
const keys = await session.query(QUERIES.keys, params);
|
|
66
|
+
const indexes = await session.query(QUERIES.uniqueIndexes, params);
|
|
67
|
+
return {
|
|
68
|
+
statements: [
|
|
69
|
+
...schemas.map((row) => ({ object: row.name, sql: `CREATE SCHEMA IF NOT EXISTS ${row.quoted}` })),
|
|
70
|
+
...enums.map((row) => ({ object: row.object, sql: `CREATE TYPE ${row.quoted} AS ENUM (${row.labels})` })),
|
|
71
|
+
...tables.map((row) => ({ object: row.object, sql: `CREATE TABLE ${row.quoted} ()` })),
|
|
72
|
+
...columns.map((row) => ({ object: `${row.table_object}.${row.name}`, sql: `ALTER TABLE ${row.table_quoted} ADD COLUMN ${row.quoted} ${row.type}` })),
|
|
73
|
+
...keys.map((row) => ({ object: `${row.table_object}.${row.name}`, sql: `ALTER TABLE ${row.table_quoted} ADD CONSTRAINT ${row.quoted} ${row.definition}` })),
|
|
74
|
+
...indexes.map((row) => ({ object: `${row.table_object}.${row.name}`, sql: row.definition })),
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
await session.exec("ROLLBACK");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Replays the catalog on the scratch database, each statement on its own: a statement that fails
|
|
84
|
+
* (a type the scratch lacks, a key on a skipped column) is skipped, so a module that never
|
|
85
|
+
* references that table still builds its reference. Returns the skipped objects with the reason.
|
|
86
|
+
*/
|
|
87
|
+
export async function createAppStubs(session, catalog) {
|
|
88
|
+
const skipped = [];
|
|
89
|
+
for (const statement of catalog.statements) {
|
|
90
|
+
try {
|
|
91
|
+
await session.exec(statement.sql);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
skipped.push(`${statement.object}: ${error instanceof Error ? error.message : String(error)}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return skipped;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=stubs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stubs.js","sourceRoot":"","sources":["../../src/migrations/stubs.ts"],"names":[],"mappings":"AAqBA,mGAAmG;AACnG,iEAAiE;AACjE,MAAM,aAAa,GAAG,CAAC,KAAa,EAAU,EAAE,CAC9C,GAAG,KAAK,mCAAmC,KAAK,wCAAwC,KAAK,4BAA4B,CAAC;AAE5H,MAAM,gBAAgB,GAAG,CAAC,OAAe,EAAE,KAAa,EAAU,EAAE,CAClE,gEAAgE,OAAO,+BAA+B,KAAK,6BAA6B,CAAC;AAE3I,MAAM,OAAO,GAAG;IACd,OAAO,EAAE;;;wCAG6B,aAAa,CAAC,GAAG,CAAC,QAAQ,gBAAgB,CAAC,UAAU,EAAE,GAAG,CAAC;eACpF;IACb,KAAK,EAAE;;;;gCAIuB,aAAa,CAAC,GAAG,CAAC,QAAQ,gBAAgB,CAAC,SAAS,EAAE,GAAG,CAAC;eAC3E;IACb,MAAM,EAAE;;;wCAG8B,aAAa,CAAC,GAAG,CAAC,QAAQ,gBAAgB,CAAC,UAAU,EAAE,GAAG,CAAC;eACpF;IACb,OAAO,EAAE;;;;;;;YAOC,aAAa,CAAC,GAAG,CAAC,QAAQ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;QACT,MAAM,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACjC,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,OAAyB,EAAE,OAAmB;IACjF,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,KAAK,MAAM,SAAS,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;QAC3C,IAAI,CAAC;YACH,MAAM,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACpC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,MAAM,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACjG,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type CreateDatabaseOptions, type DatabaseHandle } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* The process-wide handle for `url`, opened on first use. `options` apply only to that first call.
|
|
4
|
+
* A failed open is not kept, so the next call tries again.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getSharedDatabase(url: string, options?: CreateDatabaseOptions): Promise<DatabaseHandle>;
|
|
7
|
+
/** Closes and forgets every shared handle. For tests and graceful shutdown. */
|
|
8
|
+
export declare function closeSharedDatabases(): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,KAAK,qBAAqB,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAQ9F;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,OAAO,CAAC,cAAc,CAAC,CAS3G;AAED,+EAA+E;AAC/E,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAO1D"}
|
package/dist/shared.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// One database handle per URL for the whole server process, shared by every module's Next.js
|
|
2
|
+
// adapter and the app. Package code cannot import the app's own handle, and `next dev` re-evaluates
|
|
3
|
+
// modules on every change, so the handles live on `globalThis` under a `Symbol.for` key: each
|
|
4
|
+
// reload, and each copy of this file in separate server bundles, finds the same pool.
|
|
5
|
+
import { createDatabase } from "./client.js";
|
|
6
|
+
const HANDLES_KEY = Symbol.for("@softure-ai/db/shared-handles");
|
|
7
|
+
const host = globalThis;
|
|
8
|
+
/**
|
|
9
|
+
* The process-wide handle for `url`, opened on first use. `options` apply only to that first call.
|
|
10
|
+
* A failed open is not kept, so the next call tries again.
|
|
11
|
+
*/
|
|
12
|
+
export function getSharedDatabase(url, options = {}) {
|
|
13
|
+
const handles = (host[HANDLES_KEY] ??= new Map());
|
|
14
|
+
let handle = handles.get(url);
|
|
15
|
+
if (handle === undefined) {
|
|
16
|
+
handle = createDatabase(url, options);
|
|
17
|
+
handles.set(url, handle);
|
|
18
|
+
handle.catch(() => handles.delete(url));
|
|
19
|
+
}
|
|
20
|
+
return handle;
|
|
21
|
+
}
|
|
22
|
+
/** Closes and forgets every shared handle. For tests and graceful shutdown. */
|
|
23
|
+
export async function closeSharedDatabases() {
|
|
24
|
+
const handles = host[HANDLES_KEY];
|
|
25
|
+
if (handles === undefined)
|
|
26
|
+
return;
|
|
27
|
+
const opened = [...handles.values()];
|
|
28
|
+
handles.clear();
|
|
29
|
+
const results = await Promise.allSettled(opened);
|
|
30
|
+
await Promise.all(results.flatMap((result) => (result.status === "fulfilled" ? [result.value.close()] : [])));
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=shared.js.map
|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,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAChH,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
2
|
+
import type { AnySoftureModule } from "@softure-ai/core";
|
|
3
|
+
import { type PgliteClientDatabase } from "./client.js";
|
|
4
|
+
import { type AppMigrations } from "./migrations/migrator.js";
|
|
5
|
+
export interface TestDatabase {
|
|
6
|
+
readonly db: PgliteClientDatabase;
|
|
7
|
+
readonly client: PGlite;
|
|
8
|
+
readonly close: () => Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export interface CreateTestDatabaseOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The app's own migrations, run around the module files as `migrate` runs them. The template is
|
|
13
|
+
* cached per hook object: define the hooks once, at module level, not inline per call.
|
|
14
|
+
*/
|
|
15
|
+
readonly app?: AppMigrations;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A fresh, migrated, in-memory database per call. List the module under test and the modules it
|
|
19
|
+
* depends on. Throws when a migration (a module file or an app hook) fails: that is a bug in the
|
|
20
|
+
* module package or the app's migrations.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createTestDatabase(modules?: readonly AnySoftureModule[], options?: CreateTestDatabaseOptions): Promise<TestDatabase>;
|
|
23
|
+
//# sourceMappingURL=testing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAsB,KAAK,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAkC,KAAK,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAG9F,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,EAAE,oBAAoB,CAAC;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACrC;AAED,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,aAAa,CAAC;CAC9B;AAMD;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,GAAE,SAAS,gBAAgB,EAAO,EAAE,OAAO,GAAE,yBAA8B,GAAG,OAAO,CAAC,YAAY,CAAC,CAIlJ"}
|