@opensaas/stack-cli 0.44.0-alpha.4 → 0.44.0-alpha.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/README.md +1 -1
- package/bin/node-guard.d.ts +2 -0
- package/bin/node-guard.js +14 -0
- package/bin/opensaas.js +10 -1
- package/dist/.build-hash +1 -1
- package/dist/commands/db-plan.d.ts +28 -0
- package/dist/commands/db-plan.d.ts.map +1 -0
- package/dist/commands/db-plan.js +123 -0
- package/dist/commands/db-plan.js.map +1 -0
- package/dist/commands/db.d.ts.map +1 -1
- package/dist/commands/db.js +9 -0
- package/dist/commands/db.js.map +1 -1
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +172 -80
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/generate.d.ts +2 -0
- package/dist/commands/generate.d.ts.map +1 -1
- package/dist/commands/generate.js +6 -0
- package/dist/commands/generate.js.map +1 -1
- package/dist/commands/mcp.d.ts.map +1 -1
- package/dist/commands/mcp.js +11 -2
- package/dist/commands/mcp.js.map +1 -1
- package/dist/commands/migrate.d.ts.map +1 -1
- package/dist/commands/migrate.js +6 -0
- package/dist/commands/migrate.js.map +1 -1
- package/dist/dev/control.d.ts +2 -0
- package/dist/dev/control.d.ts.map +1 -1
- package/dist/dev/control.js +10 -0
- package/dist/dev/control.js.map +1 -1
- package/dist/dev/staged-reconcile.d.ts +28 -2
- package/dist/dev/staged-reconcile.d.ts.map +1 -1
- package/dist/dev/staged-reconcile.js +108 -12
- package/dist/dev/staged-reconcile.js.map +1 -1
- package/dist/generator/config-load.d.ts +7 -0
- package/dist/generator/config-load.d.ts.map +1 -1
- package/dist/generator/config-load.js +30 -20
- package/dist/generator/config-load.js.map +1 -1
- package/dist/generator/contract-module.d.ts.map +1 -1
- package/dist/generator/contract-module.js +27 -11
- package/dist/generator/contract-module.js.map +1 -1
- package/dist/generator/prisma-config.d.ts.map +1 -1
- package/dist/generator/prisma-config.js +1 -0
- package/dist/generator/prisma-config.js.map +1 -1
- package/dist/mcp/lib/generators/feature-generator.d.ts.map +1 -1
- package/dist/mcp/lib/generators/feature-generator.js +25 -31
- package/dist/mcp/lib/generators/feature-generator.js.map +1 -1
- package/dist/mcp/server/index.d.ts.map +1 -1
- package/dist/mcp/server/index.js +3 -0
- package/dist/mcp/server/index.js.map +1 -1
- package/dist/migration/junction-ids.d.ts.map +1 -1
- package/dist/migration/junction-ids.js +5 -3
- package/dist/migration/junction-ids.js.map +1 -1
- package/package.json +16 -16
package/README.md
CHANGED
|
@@ -425,7 +425,7 @@ Planning a migration needs a database to plan against, and a direct
|
|
|
425
425
|
(non-pooled) connection, which `DIRECT_DATABASE_URL` supplies:
|
|
426
426
|
|
|
427
427
|
```bash
|
|
428
|
-
DIRECT_DATABASE_URL=...
|
|
428
|
+
DIRECT_DATABASE_URL=... pnpm migrate
|
|
429
429
|
```
|
|
430
430
|
|
|
431
431
|
Commit the resulting `migrations/` directory. The release step applies it:
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const REQUIRED_NODE = '22.18.0'
|
|
2
|
+
|
|
3
|
+
export function nodeVersionError(version, required = REQUIRED_NODE) {
|
|
4
|
+
const parse = (v) => v.split('.').map((part) => Number.parseInt(part, 10) || 0)
|
|
5
|
+
const have = parse(version)
|
|
6
|
+
const need = parse(required)
|
|
7
|
+
for (let i = 0; i < 3; i++) {
|
|
8
|
+
if ((have[i] ?? 0) > need[i]) return null
|
|
9
|
+
if ((have[i] ?? 0) < need[i]) {
|
|
10
|
+
return `opensaas requires Node.js ${required} or newer; this is Node.js ${version}.`
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return null
|
|
14
|
+
}
|
package/bin/opensaas.js
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import { nodeVersionError } from './node-guard.js'
|
|
5
|
+
|
|
6
|
+
const problem = nodeVersionError(process.versions.node)
|
|
7
|
+
if (problem) {
|
|
8
|
+
process.stderr.write(`${problem}\n`)
|
|
9
|
+
process.exit(1)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
await import('../dist/index.js')
|
package/dist/.build-hash
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
2bb0df4f632a356a841015fe0607f505088a41453bd089ed297db2cee301cdc5
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
interface Edge {
|
|
2
|
+
dir: string;
|
|
3
|
+
from: string;
|
|
4
|
+
to: string;
|
|
5
|
+
createdAt: number | null;
|
|
6
|
+
}
|
|
7
|
+
/** Thrown when two migration packages leave the same state for different ones. */
|
|
8
|
+
export declare class MultipleMigrationHeadsError extends Error {
|
|
9
|
+
readonly candidates: readonly Edge[];
|
|
10
|
+
constructor(candidates: readonly Edge[]);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The state the most recently planned migration package leaves, or `null` when
|
|
14
|
+
* the app space has no package yet. Packages are ordered by their manifest's
|
|
15
|
+
* `createdAt`; without one on every package the chain is walked from the baseline.
|
|
16
|
+
*
|
|
17
|
+
* @throws {MultipleMigrationHeadsError} when two packages fork the graph.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveMigrationHead(cwd: string): string | null;
|
|
20
|
+
/** The `prisma migration plan` arguments for the given forwarded arguments. */
|
|
21
|
+
export declare function buildPlanArgs(cwd: string, forwarded: readonly string[]): string[];
|
|
22
|
+
/**
|
|
23
|
+
* `opensaas db plan` — plans a migration package from the state the latest
|
|
24
|
+
* migration package leaves, so the committed `db` ref never decides the origin.
|
|
25
|
+
*/
|
|
26
|
+
export declare function dbPlanCommand(forwarded?: readonly string[]): Promise<void>;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=db-plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-plan.d.ts","sourceRoot":"","sources":["../../src/commands/db-plan.ts"],"names":[],"mappings":"AAKA,UAAU,IAAI;IACZ,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;IACZ,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB;AAED,kFAAkF;AAClF,qBAAa,2BAA4B,SAAQ,KAAK;IACxC,QAAQ,CAAC,UAAU,EAAE,SAAS,IAAI,EAAE;IAAhD,YAAqB,UAAU,EAAE,SAAS,IAAI,EAAE,EAQ/C;CACF;AAsED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM/D;AAED,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAMjF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,SAAS,GAAE,SAAS,MAAM,EAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CASpF"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { runPrismaCli } from '../generator/prisma-cli.js';
|
|
5
|
+
/** Thrown when two migration packages leave the same state for different ones. */
|
|
6
|
+
export class MultipleMigrationHeadsError extends Error {
|
|
7
|
+
candidates;
|
|
8
|
+
constructor(candidates) {
|
|
9
|
+
super('The app migration graph forks: ' +
|
|
10
|
+
candidates.map((edge) => `${edge.dir} (${edge.from} -> ${edge.to})`).join(', ') +
|
|
11
|
+
'. Merge or remove the divergent migration packages, or pass --from <hash> ' +
|
|
12
|
+
`to choose an origin, e.g. --from ${candidates[0]?.to ?? '<hash>'}.`);
|
|
13
|
+
this.candidates = candidates;
|
|
14
|
+
this.name = 'MultipleMigrationHeadsError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function readEdges(appDir) {
|
|
18
|
+
if (!fs.existsSync(appDir))
|
|
19
|
+
return [];
|
|
20
|
+
const edges = [];
|
|
21
|
+
for (const entry of fs.readdirSync(appDir, { withFileTypes: true })) {
|
|
22
|
+
if (!entry.isDirectory())
|
|
23
|
+
continue;
|
|
24
|
+
const file = path.join(appDir, entry.name, 'migration.json');
|
|
25
|
+
if (!fs.existsSync(file))
|
|
26
|
+
continue;
|
|
27
|
+
let manifest;
|
|
28
|
+
try {
|
|
29
|
+
manifest = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
throw new Error(`Could not read ${file}: ${error instanceof Error ? error.message : error}`);
|
|
33
|
+
}
|
|
34
|
+
const field = (key) => typeof manifest === 'object' && manifest !== null && key in manifest
|
|
35
|
+
? Object.getOwnPropertyDescriptor(manifest, key)?.value
|
|
36
|
+
: undefined;
|
|
37
|
+
const from = field('from');
|
|
38
|
+
const to = field('to');
|
|
39
|
+
const created = field('createdAt');
|
|
40
|
+
if (typeof to !== 'string')
|
|
41
|
+
throw new Error(`${file} has no string "to" hash.`);
|
|
42
|
+
const parsed = typeof created === 'string' ? Date.parse(created) : NaN;
|
|
43
|
+
edges.push({
|
|
44
|
+
dir: entry.name,
|
|
45
|
+
from: typeof from === 'string' ? from : '',
|
|
46
|
+
to,
|
|
47
|
+
createdAt: Number.isNaN(parsed) ? null : parsed,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return edges.sort((a, b) => (a.dir < b.dir ? -1 : a.dir > b.dir ? 1 : 0));
|
|
51
|
+
}
|
|
52
|
+
function headByTimestamp(edges) {
|
|
53
|
+
const ordered = [...edges].sort((a, b) => (a.createdAt ?? 0) - (b.createdAt ?? 0));
|
|
54
|
+
const last = ordered[ordered.length - 1];
|
|
55
|
+
const forks = ordered.filter((edge, index) => edge !== last &&
|
|
56
|
+
edge.to !== last.to &&
|
|
57
|
+
!ordered.slice(index + 1).some((later) => later.from === edge.to));
|
|
58
|
+
if (forks.length > 0)
|
|
59
|
+
throw new MultipleMigrationHeadsError([...forks, last]);
|
|
60
|
+
return last.to;
|
|
61
|
+
}
|
|
62
|
+
function headByChain(edges) {
|
|
63
|
+
const targets = new Set(edges.map((edge) => edge.to));
|
|
64
|
+
const roots = [...new Set(edges.map((edge) => edge.from))].filter((from) => !targets.has(from));
|
|
65
|
+
const root = roots[0];
|
|
66
|
+
if (root === undefined || roots.length > 1) {
|
|
67
|
+
const latest = edges[edges.length - 1];
|
|
68
|
+
throw new Error(`The app migration graph has no single origin (${edges.length} packages, no usable createdAt). ` +
|
|
69
|
+
`Pass --from <hash> to choose an origin, e.g. --from ${latest.to}.`);
|
|
70
|
+
}
|
|
71
|
+
const used = new Set();
|
|
72
|
+
let current = root;
|
|
73
|
+
for (;;) {
|
|
74
|
+
const next = edges.filter((edge) => edge.from === current && !used.has(edge));
|
|
75
|
+
if (next.length === 0)
|
|
76
|
+
return current;
|
|
77
|
+
const distinct = new Set(next.map((edge) => edge.to));
|
|
78
|
+
if (distinct.size > 1)
|
|
79
|
+
throw new MultipleMigrationHeadsError(next);
|
|
80
|
+
used.add(next[0]);
|
|
81
|
+
current = next[0].to;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The state the most recently planned migration package leaves, or `null` when
|
|
86
|
+
* the app space has no package yet. Packages are ordered by their manifest's
|
|
87
|
+
* `createdAt`; without one on every package the chain is walked from the baseline.
|
|
88
|
+
*
|
|
89
|
+
* @throws {MultipleMigrationHeadsError} when two packages fork the graph.
|
|
90
|
+
*/
|
|
91
|
+
export function resolveMigrationHead(cwd) {
|
|
92
|
+
const edges = readEdges(path.join(cwd, 'migrations', 'app'));
|
|
93
|
+
if (edges.length === 0)
|
|
94
|
+
return null;
|
|
95
|
+
return edges.every((edge) => edge.createdAt !== null)
|
|
96
|
+
? headByTimestamp(edges)
|
|
97
|
+
: headByChain(edges);
|
|
98
|
+
}
|
|
99
|
+
/** The `prisma migration plan` arguments for the given forwarded arguments. */
|
|
100
|
+
export function buildPlanArgs(cwd, forwarded) {
|
|
101
|
+
const separator = forwarded.indexOf('--');
|
|
102
|
+
const flags = separator === -1 ? forwarded : forwarded.slice(0, separator);
|
|
103
|
+
const named = flags.some((arg) => arg === '--from' || arg.startsWith('--from='));
|
|
104
|
+
const head = named ? null : resolveMigrationHead(cwd);
|
|
105
|
+
return ['migration', 'plan', ...(head === null ? [] : ['--from', head]), ...forwarded];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* `opensaas db plan` — plans a migration package from the state the latest
|
|
109
|
+
* migration package leaves, so the committed `db` ref never decides the origin.
|
|
110
|
+
*/
|
|
111
|
+
export async function dbPlanCommand(forwarded = []) {
|
|
112
|
+
const cwd = process.cwd();
|
|
113
|
+
try {
|
|
114
|
+
const run = await runPrismaCli(cwd, buildPlanArgs(cwd, forwarded), 'interactive');
|
|
115
|
+
if (run.exitCode !== 0)
|
|
116
|
+
process.exitCode = run.exitCode ?? 1;
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
console.error(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
120
|
+
process.exitCode = 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=db-plan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-plan.js","sourceRoot":"","sources":["../../src/commands/db-plan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAA;AACxB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAA;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA;AASzD,kFAAkF;AAClF,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IAC/B,UAAU;IAA/B,YAAqB,UAA2B;QAC9C,KAAK,CACH,iCAAiC;YAC/B,UAAU,CAAC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AAQ,GAAG,GAAG,CAAC,QAAQ,IAAI,CAAC,CAAA;IAC9D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAChF,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;IACtB,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/commands/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/commands/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAKnC,2CAA2C;AAC3C,MAAM,WAAW,sBAAsB;IACrC,sFAAsF;IACtF,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CAAC,OAAO,GAAE,sBAA2B,GAAG,OAAO,CAAC,IAAI,CAAC,CAYzF;AAED,8BAA8B;AAC9B,wBAAgB,eAAe,IAAI,OAAO,CAoBzC"}
|
package/dist/commands/db.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
|
+
import { dbPlanCommand } from './db-plan.js';
|
|
3
4
|
import { DevLoopUnreachableError, NoDevLoopError, requestDatabaseUpdate } from '../dev/control.js';
|
|
4
5
|
/**
|
|
5
6
|
* `opensaas db update` — what `pnpm db:update` points at.
|
|
@@ -38,6 +39,14 @@ export function createDbCommand() {
|
|
|
38
39
|
.action(async (options) => {
|
|
39
40
|
await dbUpdateCommand({ plan: options.plan });
|
|
40
41
|
});
|
|
42
|
+
db.command('plan')
|
|
43
|
+
.description('Plan a migration package from the head of the migration graph')
|
|
44
|
+
.allowUnknownOption()
|
|
45
|
+
.allowExcessArguments()
|
|
46
|
+
.helpOption(false)
|
|
47
|
+
.action(async (_options, command) => {
|
|
48
|
+
await dbPlanCommand(command.args);
|
|
49
|
+
});
|
|
41
50
|
return db;
|
|
42
51
|
}
|
|
43
52
|
//# sourceMappingURL=db.js.map
|
package/dist/commands/db.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/commands/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,uBAAuB,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAQlG;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,OAAO,GAA2B,EAAE;IACxE,IAAI,CAAC;QACH,MAAM,EAAE,GAAG,MAAM,qBAAqB,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;YAC9E,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACtB,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,EAAE;YAAE,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,CAAC,KAAK,YAAY,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,YAAY,uBAAuB,CAAC;YACnF,MAAM,KAAK,CAAA;QACb,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAA;QACvC,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;IACtB,CAAC;A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AuB,CAAC;YACnF,MAAM,KAAK,CAAA;QACb,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAA;QACvC,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;IACtB,CAAC;AACH,CAAC;AAED,8BAA8B;AAC9B,MAAM,UAAU,eAAe;IAC7B,MAAM,EAAE,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,WAAW,CAAC,4CAA4C,CAAC,CAAA;IAEtF,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC;SACjB,WAAW,CAAC,wEAAwE,CAAC;SACrF,MAAM,CAAC,aAAa,EAAE,8DAA8D,CAAC;SACrF,MAAM,CAAC,KAAK,EAAE,OAA0B,EAAE,EAAE;QAC3C,MAAM,eAAe,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAA;IAC/C,CAAC,CAAC,CAAA;IAEJ,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;SACf,WAAW,CAAC,+DAA+D,CAAC;SAC5E,kBAAkB,EAAE;SACpB,oBAAoB,EAAE;SACtB,UAAU,CAAC,KAAK,CAAC;SACjB,MAAM,CAAC,KAAK,EAAE,QAAiB,EAAE,OAAgB,EAAE,EAAE;QACpD,MAAM,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;IAEJ,OAAO,EAAE,CAAA;AACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/commands/dev.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/commands/dev.ts"],"names":[],"mappings":"AAmEA,sCAAsC;AACtC,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC9B,wGAAwG;IACxG,GAAG,CAAC,EAAE,OAAO,CAAA;CACd;AA8ED,2FAA2F;AAC3F,wBAAsB,UAAU,CAAC,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8nB/E"}
|
package/dist/commands/dev.js
CHANGED
|
@@ -8,7 +8,7 @@ import { generateCommand, GenerationFailedError } from './generate.js';
|
|
|
8
8
|
import { loadOpenSaasConfig, loadProjectEnvFile, runPrismaCli } from '../generator/index.js';
|
|
9
9
|
import { createAppRunner } from '../dev/app-runner.js';
|
|
10
10
|
import { startControlChannel } from '../dev/control.js';
|
|
11
|
-
import { describePlan, planId, planDatabaseUpdate, promoteStagedGeneration, restoreMigrationRefs, restoreRefsAfterReconcile, snapshotMigrationRefs, STAGING_DIR, } from '../dev/staged-reconcile.js';
|
|
11
|
+
import { changedSince, describePlan, filesystemNow, planId, planDatabaseUpdate, promoteStagedGeneration, restoreMigrationRefs, restoreRefsAfterReconcile, snapshotMigrationRefs, STAGING_DIR, } from '../dev/staged-reconcile.js';
|
|
12
12
|
/**
|
|
13
13
|
* How long the config file must sit unwritten before a save counts as one
|
|
14
14
|
* save. `fs.writeFileSync` truncates then writes, which Linux inotify can
|
|
@@ -63,8 +63,10 @@ async function reconcile(cwd) {
|
|
|
63
63
|
const refs = snapshotMigrationRefs(cwd);
|
|
64
64
|
const run = await runPrismaCli(cwd, ['db', 'update'], 'interactive');
|
|
65
65
|
restoreRefsAfterReconcile(cwd, refs);
|
|
66
|
-
if (run.exitCode === 0)
|
|
66
|
+
if (run.exitCode === 0) {
|
|
67
|
+
console.log(chalk.gray('Any "Advanced ref" line above is Prisma\'s own: the loop restores migration refs.'));
|
|
67
68
|
return true;
|
|
69
|
+
}
|
|
68
70
|
console.error(chalk.red(`\nprisma db update did not apply (exit ${run.exitCode ?? run.signal ?? 'unknown'}).`));
|
|
69
71
|
console.error(chalk.gray('The app was not started; the database is unchanged.\n'));
|
|
70
72
|
return false;
|
|
@@ -121,10 +123,10 @@ export async function devCommand(options = {}) {
|
|
|
121
123
|
let app;
|
|
122
124
|
let interrupted = false;
|
|
123
125
|
let loadedExtensions = [];
|
|
124
|
-
/**
|
|
125
|
-
let staged;
|
|
126
|
-
/** The id of the destructive plan printed when `staged` was parked. */
|
|
126
|
+
/** The id of the plan the watcher printed and parked, for the no-change message to repeat. */
|
|
127
127
|
let parkedPlanId;
|
|
128
|
+
/** The plan a `db update` applied but could not promote: the database is ahead of the bundle. */
|
|
129
|
+
let unpromotedPlanId;
|
|
128
130
|
/** One reconcile at a time: a burst of writes must not race itself. */
|
|
129
131
|
let queue = Promise.resolve();
|
|
130
132
|
/**
|
|
@@ -153,12 +155,13 @@ export async function devCommand(options = {}) {
|
|
|
153
155
|
*/
|
|
154
156
|
let watchedModules = [];
|
|
155
157
|
/**
|
|
156
|
-
* The bytes of every module in {@link watchedModules}
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
158
|
+
* The bytes of every module in {@link watchedModules} as the watcher last read
|
|
159
|
+
* them around a generation — `null` for one that could not be read (deleted
|
|
160
|
+
* mid-session). Paired with `reconciledSource` to decide whether a watcher
|
|
161
|
+
* event has anything new to reconcile: a save that reproduces both is the race
|
|
162
|
+
* #1260 closed, still closed here because the two are compared as bytes, never
|
|
163
|
+
* as generated output. Only that skip reads it: it is not a record of what a
|
|
164
|
+
* generation read, and `db update` never consults it.
|
|
162
165
|
*/
|
|
163
166
|
let reconciledModules = new Map();
|
|
164
167
|
/** Reads each path's current bytes, `null` for one that cannot be read. */
|
|
@@ -174,6 +177,22 @@ export async function devCommand(options = {}) {
|
|
|
174
177
|
}
|
|
175
178
|
return snapshot;
|
|
176
179
|
};
|
|
180
|
+
/**
|
|
181
|
+
* The bytes the watcher's skip compares a later save with: `before`, read ahead
|
|
182
|
+
* of a generation, for every module already in the graph, so an edit landing
|
|
183
|
+
* while it generates or plans still reads as unreconciled. A module the
|
|
184
|
+
* generation newly reached had no earlier read, so its bytes are read here,
|
|
185
|
+
* which may be after generation read it.
|
|
186
|
+
*/
|
|
187
|
+
const modulesGeneratedFrom = (generation, before) => {
|
|
188
|
+
const snapshot = snapshotModules(generation.resolvedModules.filter((modulePath) => !before.has(modulePath)));
|
|
189
|
+
for (const modulePath of generation.resolvedModules) {
|
|
190
|
+
const bytes = before.get(modulePath);
|
|
191
|
+
if (bytes !== undefined)
|
|
192
|
+
snapshot.set(modulePath, bytes);
|
|
193
|
+
}
|
|
194
|
+
return snapshot;
|
|
195
|
+
};
|
|
177
196
|
const moduleSnapshotsEqual = (a, b) => {
|
|
178
197
|
if (a.size !== b.size)
|
|
179
198
|
return false;
|
|
@@ -224,7 +243,7 @@ export async function devCommand(options = {}) {
|
|
|
224
243
|
/** How the loop reports that it could not get as far as a staged generation. */
|
|
225
244
|
const sayNothingStaged = (say, error) => {
|
|
226
245
|
say(`Generation failed, so nothing was staged: ${error instanceof Error ? error.message : String(error)}`);
|
|
227
|
-
say('The
|
|
246
|
+
say('The database and generated bundle stay at the last good contract. Routes that import opensaas.config.ts will error until the file is fixed.');
|
|
228
247
|
return undefined;
|
|
229
248
|
};
|
|
230
249
|
/**
|
|
@@ -232,10 +251,6 @@ export async function devCommand(options = {}) {
|
|
|
232
251
|
* this refuses — a half-saved config must not take the loop down with it.
|
|
233
252
|
*/
|
|
234
253
|
const stage = async (say) => {
|
|
235
|
-
// `staged` only ever names files inside `stagingDir`, so it has to be
|
|
236
|
-
// dropped with them — a pointer left behind names the bytes of whatever
|
|
237
|
-
// generation runs next, including one this loop refused.
|
|
238
|
-
staged = undefined;
|
|
239
254
|
parkedPlanId = undefined;
|
|
240
255
|
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
241
256
|
try {
|
|
@@ -248,10 +263,17 @@ export async function devCommand(options = {}) {
|
|
|
248
263
|
return sayNothingStaged(say, error);
|
|
249
264
|
}
|
|
250
265
|
};
|
|
266
|
+
/** Returns whether this finished a promote that failed after its apply landed. */
|
|
251
267
|
const promote = (generation) => {
|
|
252
268
|
promoteStagedGeneration(generation.paths, generation.livePaths, stagingDir);
|
|
253
|
-
staged = undefined;
|
|
254
269
|
parkedPlanId = undefined;
|
|
270
|
+
const completedFailedPromote = unpromotedPlanId !== undefined;
|
|
271
|
+
unpromotedPlanId = undefined;
|
|
272
|
+
return completedFailedPromote;
|
|
273
|
+
};
|
|
274
|
+
const restartApp = (say) => {
|
|
275
|
+
say('Restarting the app: a client cached across a reload would query the dropped column.');
|
|
276
|
+
app?.restart();
|
|
255
277
|
};
|
|
256
278
|
const reportFailure = (say, output) => {
|
|
257
279
|
say('prisma db update did not apply. Nothing was promoted.');
|
|
@@ -276,7 +298,7 @@ export async function devCommand(options = {}) {
|
|
|
276
298
|
const currentModules = snapshotModules(watchedModules);
|
|
277
299
|
if (source === reconciledSource && moduleSnapshotsEqual(currentModules, reconciledModules)) {
|
|
278
300
|
console.log(chalk.gray('\nConfig saved with no change: nothing to reconcile.'));
|
|
279
|
-
console.log(chalk.gray(
|
|
301
|
+
console.log(chalk.gray(parkedPlanId === undefined
|
|
280
302
|
? NOTHING_TO_RECONCILE_ROUTE
|
|
281
303
|
: `The change staged earlier is still parked. ${parkedRoute(parkedPlanId)}`));
|
|
282
304
|
return;
|
|
@@ -293,6 +315,7 @@ export async function devCommand(options = {}) {
|
|
|
293
315
|
return;
|
|
294
316
|
}
|
|
295
317
|
reconciledSource = source;
|
|
318
|
+
const generatedModules = modulesGeneratedFrom(generation, currentModules);
|
|
296
319
|
const planned = await planDatabaseUpdate(cwd, generation.prismaConfig, { dryRun: true });
|
|
297
320
|
if (!planned.ok) {
|
|
298
321
|
reconciledSource = undefined;
|
|
@@ -303,7 +326,6 @@ export async function devCommand(options = {}) {
|
|
|
303
326
|
const unconsentedEnvChange = database === undefined && !sessionConsent && planned.plan.operations.length > 0;
|
|
304
327
|
if (planned.plan.destructive || unconsentedEnvChange) {
|
|
305
328
|
restoreMigrationRefs(cwd, refs);
|
|
306
|
-
staged = generation;
|
|
307
329
|
const id = planId(planned.plan);
|
|
308
330
|
parkedPlanId = id;
|
|
309
331
|
// Only now, not right after staging: a rejected generation must never
|
|
@@ -311,7 +333,7 @@ export async function devCommand(options = {}) {
|
|
|
311
333
|
// and the database are still the previous generation's until this one
|
|
312
334
|
// is at least parked.
|
|
313
335
|
syncWatchedModules(generation.resolvedModules);
|
|
314
|
-
reconciledModules =
|
|
336
|
+
reconciledModules = generatedModules;
|
|
315
337
|
console.log(chalk.yellow(planned.plan.destructive
|
|
316
338
|
? '\nThis change would destroy data, so it was not applied:\n'
|
|
317
339
|
: '\nThe database came from the environment and would change, so nothing was applied:\n'));
|
|
@@ -329,18 +351,25 @@ export async function devCommand(options = {}) {
|
|
|
329
351
|
return;
|
|
330
352
|
}
|
|
331
353
|
restoreRefsAfterReconcile(cwd, refs);
|
|
332
|
-
promote(generation);
|
|
354
|
+
const completedFailedPromote = promote(generation);
|
|
333
355
|
syncWatchedModules(generation.resolvedModules);
|
|
334
|
-
reconciledModules =
|
|
356
|
+
reconciledModules = generatedModules;
|
|
335
357
|
console.log(chalk.green('\nDatabase updated and the new contract promoted.\n'));
|
|
358
|
+
if (completedFailedPromote)
|
|
359
|
+
restartApp(say);
|
|
336
360
|
};
|
|
337
361
|
const onDatabaseUpdateRequest = async (requestedPlanId, reply) => {
|
|
338
362
|
const say = (message) => {
|
|
339
363
|
console.log(chalk.gray(message));
|
|
340
364
|
reply.log(message);
|
|
341
365
|
};
|
|
366
|
+
const startedAt = filesystemNow(cwd);
|
|
342
367
|
const refs = snapshotMigrationRefs(cwd);
|
|
343
|
-
|
|
368
|
+
// Always from disk, so a plan id is only ever compared with what the config
|
|
369
|
+
// wants now. What the watcher last saw says nothing about this generation,
|
|
370
|
+
// so its next event reconciles rather than skips.
|
|
371
|
+
reconciledSource = undefined;
|
|
372
|
+
const generation = await stage(say);
|
|
344
373
|
if (generation === undefined) {
|
|
345
374
|
restoreMigrationRefs(cwd, refs);
|
|
346
375
|
reply.finish(false, 'Nothing was staged: generation refused the current config.');
|
|
@@ -353,21 +382,45 @@ export async function devCommand(options = {}) {
|
|
|
353
382
|
reply.finish(false, 'The database is unchanged and nothing was promoted.');
|
|
354
383
|
return;
|
|
355
384
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
385
|
+
const id = planId(planned.plan);
|
|
386
|
+
const refuse = (message) => {
|
|
387
|
+
restoreMigrationRefs(cwd, refs);
|
|
388
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
389
|
+
syncWatchedModules(generation.resolvedModules);
|
|
390
|
+
reply.finish(false, message);
|
|
391
|
+
};
|
|
392
|
+
if (planned.plan.destructive && requestedPlanId !== id) {
|
|
393
|
+
say('This change would destroy data, so it was not applied:');
|
|
394
|
+
for (const line of describePlan(planned.plan))
|
|
395
|
+
say(line);
|
|
396
|
+
say(`Plan id: ${id}`);
|
|
397
|
+
refuse(requestedPlanId === undefined
|
|
398
|
+
? `Destructive changes need the plan's id. ${parkedRoute(id).trim()}`
|
|
399
|
+
: `Plan ${requestedPlanId} is not the current plan. ${parkedRoute(id).trim()}`);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
// The apply already landed, so the plan is empty and its id can no longer
|
|
403
|
+
// match; what is left to do is the promote that failed.
|
|
404
|
+
const retryingPromote = planned.plan.operations.length === 0 && requestedPlanId === unpromotedPlanId;
|
|
405
|
+
if (requestedPlanId !== undefined && requestedPlanId !== id && !retryingPromote) {
|
|
406
|
+
if (planned.plan.operations.length > 0) {
|
|
407
|
+
refuse(`Plan ${requestedPlanId} is not the current plan, so nothing was applied. To apply the config as it stands, run \`pnpm db:update\`.`);
|
|
408
|
+
}
|
|
409
|
+
else if (unpromotedPlanId === undefined) {
|
|
410
|
+
refuse(`Plan ${requestedPlanId} is no longer current: the database already matches the config, so nothing was applied.`);
|
|
370
411
|
}
|
|
412
|
+
else {
|
|
413
|
+
refuse(`Plan ${requestedPlanId} is not the plan whose promote failed, so nothing was applied. The generated files are still behind the database: run \`pnpm db:update\` to finish promoting them.`);
|
|
414
|
+
}
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
// Only ever refuses: the id check above is what makes a plan valid. This
|
|
418
|
+
// catches a save between the start of the request and here, whatever
|
|
419
|
+
// generation happened to read. `resolvedModules` comes from this request's
|
|
420
|
+
// own load, so it includes a module only this request reached.
|
|
421
|
+
if (changedSince([configPath, ...generation.resolvedModules], startedAt)) {
|
|
422
|
+
refuse('The config changed while this request was planning, so nothing was applied. Run the command again to plan from the config as it stands.');
|
|
423
|
+
return;
|
|
371
424
|
}
|
|
372
425
|
const applied = await planDatabaseUpdate(cwd, generation.prismaConfig, {
|
|
373
426
|
consent: planned.plan,
|
|
@@ -379,26 +432,22 @@ export async function devCommand(options = {}) {
|
|
|
379
432
|
return;
|
|
380
433
|
}
|
|
381
434
|
restoreRefsAfterReconcile(cwd, refs);
|
|
382
|
-
|
|
383
|
-
syncWatchedModules(generation.resolvedModules);
|
|
384
|
-
reconciledModules = snapshotModules(watchedModules);
|
|
385
|
-
// Best-effort: this request did not read the config itself, so this is
|
|
386
|
-
// the freshest bytes available for what the loop just generated from. A
|
|
387
|
-
// config deleted out from under a running loop is caught the same way
|
|
388
|
-
// the watcher's own read of it already is — by leaving nothing reconciled.
|
|
435
|
+
let completedFailedPromote;
|
|
389
436
|
try {
|
|
390
|
-
|
|
437
|
+
completedFailedPromote = promote(generation);
|
|
391
438
|
}
|
|
392
|
-
catch {
|
|
393
|
-
|
|
439
|
+
catch (error) {
|
|
440
|
+
unpromotedPlanId ??= id;
|
|
441
|
+
throw error;
|
|
394
442
|
}
|
|
443
|
+
syncWatchedModules(generation.resolvedModules);
|
|
395
444
|
for (const line of describePlan(applied.plan))
|
|
396
445
|
say(line);
|
|
397
|
-
if (applied.plan.destructive)
|
|
398
|
-
say
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
446
|
+
if (applied.plan.destructive || completedFailedPromote)
|
|
447
|
+
restartApp(say);
|
|
448
|
+
reply.finish(true, applied.plan.operations.length === 0 && !completedFailedPromote
|
|
449
|
+
? 'Nothing to apply: the database already matches the config.'
|
|
450
|
+
: 'Applied, promoted.');
|
|
402
451
|
};
|
|
403
452
|
try {
|
|
404
453
|
if (findDatabaseConnection({ cwd })?.provenance === 'env') {
|
|
@@ -442,35 +491,78 @@ export async function devCommand(options = {}) {
|
|
|
442
491
|
// Read before generating, so an edit landing during startup leaves bytes
|
|
443
492
|
// the watcher will not recognise as already reconciled.
|
|
444
493
|
reconciledSource = fs.readFileSync(configPath, 'utf-8');
|
|
445
|
-
let
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
494
|
+
let parkedAtBoot = false;
|
|
495
|
+
if (!process.stdin.isTTY) {
|
|
496
|
+
const say = (message) => console.log(chalk.gray(message));
|
|
497
|
+
const refs = snapshotMigrationRefs(cwd);
|
|
498
|
+
const bootModules = snapshotModules(watchedModules);
|
|
499
|
+
const staged = await stage(say);
|
|
500
|
+
if (interrupted) {
|
|
501
|
+
process.exitCode = 1;
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (staged !== undefined) {
|
|
505
|
+
const planned = await planDatabaseUpdate(cwd, staged.prismaConfig, { dryRun: true });
|
|
506
|
+
restoreMigrationRefs(cwd, refs);
|
|
507
|
+
if (planned.ok && planned.plan.destructive) {
|
|
508
|
+
const id = planId(planned.plan);
|
|
509
|
+
console.log(chalk.yellow('\nThis change would destroy data, so it was not applied:\n'));
|
|
510
|
+
for (const line of describePlan(planned.plan))
|
|
511
|
+
console.log(chalk.yellow(line));
|
|
512
|
+
console.log(chalk.yellow(`\nPlan id: ${id}`));
|
|
513
|
+
const hasPromotedBundle = fs.existsSync(staged.livePaths.contractJson) &&
|
|
514
|
+
fs.existsSync(path.join(staged.livePaths.opensaasDir, 'context.ts'));
|
|
515
|
+
if (!hasPromotedBundle) {
|
|
516
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
517
|
+
console.error(chalk.red('\nThere is no previously generated bundle to serve, so the app was not started. ' +
|
|
518
|
+
'Revert the config change, or delete `.opensaas/dev-db` to start from an empty database.\n'));
|
|
519
|
+
process.exitCode = 1;
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
parkedAtBoot = true;
|
|
523
|
+
parkedPlanId = id;
|
|
524
|
+
watchedModules = staged.resolvedModules;
|
|
525
|
+
reconciledModules = modulesGeneratedFrom(staged, bootModules);
|
|
526
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
527
|
+
console.log(chalk.yellow(`\nThe app starts on the previous schema. ${parkedRoute(id)}` +
|
|
528
|
+
'`--yes` does not consent to data loss.\n'));
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
532
|
+
}
|
|
533
|
+
}
|
|
470
534
|
}
|
|
471
|
-
if (!
|
|
472
|
-
|
|
473
|
-
|
|
535
|
+
if (!parkedAtBoot) {
|
|
536
|
+
let bootGeneration;
|
|
537
|
+
try {
|
|
538
|
+
bootGeneration = await generateCommand({ showNextSteps: false, ownedByLoop: true });
|
|
539
|
+
}
|
|
540
|
+
catch (error) {
|
|
541
|
+
if (!(error instanceof GenerationFailedError))
|
|
542
|
+
throw error;
|
|
543
|
+
process.exitCode = 1;
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
// Read as soon as generation names them, same reasoning as reconciledSource
|
|
547
|
+
// above: `reconcile()` below is interactive and can sit at a destructive
|
|
548
|
+
// plan's consent prompt indefinitely, and an edit to a watched module made
|
|
549
|
+
// while it waits must not be mistaken for one already reconciled.
|
|
550
|
+
watchedModules = bootGeneration.resolvedModules;
|
|
551
|
+
reconciledModules = snapshotModules(watchedModules);
|
|
552
|
+
if (database === undefined &&
|
|
553
|
+
!sessionConsent &&
|
|
554
|
+
!(await consentToStartupPlan(cwd, bootGeneration))) {
|
|
555
|
+
process.exitCode = 1;
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
if (interrupted) {
|
|
559
|
+
process.exitCode = 1;
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (!(await reconcile(cwd)) || interrupted) {
|
|
563
|
+
process.exitCode = 1;
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
474
566
|
}
|
|
475
567
|
// Armed only now: `queue` serialises reconciles against each other, not
|
|
476
568
|
// against this startup generate and reconcile, so a save landing earlier
|