@zukhruf/nx 0.8.108 → 0.8.110
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 +6 -4
- package/dist/prisma/migrate-diff.js +58 -10
- package/dist/prisma/migrate-diff.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -750,7 +750,7 @@ You need Nx 24.0.6 or later, and `prisma` and `@prisma/client` in the lockfile.
|
|
|
750
750
|
}
|
|
751
751
|
```
|
|
752
752
|
|
|
753
|
-
Keep the entries that are already in the lists. Nx skips a `dependsOn` on a target that the project does not have, so a project with no Prisma is not changed. `migrate:check` needs Docker and `@zukhruf/testing` (see [Check the migrations](#check-the-migrations)). The plugin does not change `typecheck` or `build`. Those targets belong to other plugins, as for `format:check` ([ADR 0001](./docs/adr/0001-inferred-targets-not-copied-defaults.md)). The `typecheck:generate` tag of text2sql is then not necessary. See [ADR 0008](./docs/adr/0008-prisma-targets-from-prisma-config-with-outputs-from-the-schema.md).
|
|
753
|
+
Keep the entries that are already in the lists. Nx skips a `dependsOn` on a target that the project does not have, so a project with no Prisma is not changed. `migrate:check` of a Postgres schema needs Docker and `@zukhruf/testing` (see [Check the migrations](#check-the-migrations)). The plugin does not change `typecheck` or `build`. Those targets belong to other plugins, as for `format:check` ([ADR 0001](./docs/adr/0001-inferred-targets-not-copied-defaults.md)). The `typecheck:generate` tag of text2sql is then not necessary. See [ADR 0008](./docs/adr/0008-prisma-targets-from-prisma-config-with-outputs-from-the-schema.md).
|
|
754
754
|
|
|
755
755
|
### Signature of the prisma area
|
|
756
756
|
|
|
@@ -796,8 +796,10 @@ npx nx run @acme/db:migrate:create --name add_patient_age
|
|
|
796
796
|
npx nx run @acme/db:migrate:check
|
|
797
797
|
```
|
|
798
798
|
|
|
799
|
-
- Both run `src/prisma/migrate-diff.ts` of this package.
|
|
800
|
-
- `prisma.config.ts` must read `datasource.shadowDatabaseUrl` from `SHADOW_CONNECTION_STRING`, as the [prisma generator](#prisma-generator) writes it. Else Prisma stops with "You must set `datasource.shadowDatabaseUrl`".
|
|
799
|
+
- Both run `src/prisma/migrate-diff.ts` of this package. The script reads the `provider` of the `datasource` block of the schema, and the shadow database follows it:
|
|
800
|
+
- `postgresql`: the script takes a new, empty database from `@zukhruf/testing/postgres`, gives it to Prisma as `SHADOW_CONNECTION_STRING`, and drops it after the diff. Prisma replays the migrations there. So the targets need Docker and `@zukhruf/testing`, as the tests of a db package do, and no `.env` entry, dev database or CI step. A `SHADOW_CONNECTION_STRING` in `.env` or in the shell is not used, so its database is never emptied. `prisma.config.ts` must read `datasource.shadowDatabaseUrl` from `SHADOW_CONNECTION_STRING`, as the [prisma generator](#prisma-generator) writes it. Else Prisma stops with "You must set `datasource.shadowDatabaseUrl`".
|
|
801
|
+
- `sqlite`: the script sets no `SHADOW_CONNECTION_STRING` and starts no database. With no shadow URL, Prisma replays the migrations on an in-memory SQLite database. The targets need no Docker and no `@zukhruf/testing` (#5625).
|
|
802
|
+
- Another provider: the script stops before Prisma with an error that names the provider.
|
|
801
803
|
- The schema is the project folder (`--to-schema .`), with each `*.prisma` file under it, as `schema: '.'` of the generated config. A diff to `schema.prisma` alone removes every table that `models/*.prisma` holds.
|
|
802
804
|
- The folder name of `migrate:create` is the UTC time to the second and the name, as `prisma migrate dev` names it. With no change, the file is `-- This is an empty migration.`, for SQL that you write. `migrate:create` needs no TTY and no dev database, where `migrate dev --create-only` refuses a data-loss warning with no TTY. Its SQL can still fail on a table with rows, for example a required column with no default: edit it before `deploy`.
|
|
803
805
|
- `migrate:check` is cached on the `*.prisma` files, `prisma.config.ts`, the migrations and the package `prisma`. The [prisma generator](#prisma-generator) adds it to `targetDefaults.typecheck.dependsOn`. See [ADR 0018](./docs/adr/0018-migrate-check-diffs-the-migrations-on-a-shadow-database-that-it-makes.md).
|
|
@@ -805,7 +807,7 @@ npx nx run @acme/db:migrate:check
|
|
|
805
807
|
### Migrate a workspace to the prisma area
|
|
806
808
|
|
|
807
809
|
1. Add `@zukhruf/nx/prisma` to `plugins` in `nx.json`.
|
|
808
|
-
2. Add `generate` to `targetDefaults.typecheck.dependsOn` and `targetDefaults.build.dependsOn`. Add `migrate:check` to `targetDefaults.typecheck.dependsOn`, as the [prisma generator](#prisma-generator) does (commit 19a4bd37, `src/generators/prisma/generator.test.ts`). `migrate:check` needs Docker and `@zukhruf/testing` (see [Check the migrations](#check-the-migrations)).
|
|
810
|
+
2. Add `generate` to `targetDefaults.typecheck.dependsOn` and `targetDefaults.build.dependsOn`. Add `migrate:check` to `targetDefaults.typecheck.dependsOn`, as the [prisma generator](#prisma-generator) does (commit 19a4bd37, `src/generators/prisma/generator.test.ts`). `migrate:check` of a Postgres schema needs Docker and `@zukhruf/testing` (see [Check the migrations](#check-the-migrations)).
|
|
809
811
|
3. Delete `generate`, `migrate`, `deploy`, `reset` and `pull` from each Prisma project, and the `typecheck` entry that only adds `generate`.
|
|
810
812
|
4. Delete the `typecheck:generate` tag and its `targetDefaults` filter.
|
|
811
813
|
5. Compare `nx show project <project> --json` before and after.
|
|
@@ -2,14 +2,17 @@
|
|
|
2
2
|
// `node migrate-diff.js check` for `migrate:check`, and
|
|
3
3
|
// `node migrate-diff.js create --name <name>` for `migrate:create`. Each runs
|
|
4
4
|
// `prisma migrate diff` from the migrations folder to the schema folder.
|
|
5
|
-
// Prisma replays the migrations on
|
|
6
|
-
// `datasource.shadowDatabaseUrl` of prisma.config.ts
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
5
|
+
// Prisma replays the migrations on a shadow database, which it reads from
|
|
6
|
+
// `datasource.shadowDatabaseUrl` of prisma.config.ts. The shadow database
|
|
7
|
+
// follows the `provider` of the schema's datasource block (ADR 0018):
|
|
8
|
+
// - postgresql: Prisma never creates the shadow database, so this script sets
|
|
9
|
+
// SHADOW_CONNECTION_STRING, which the generated config reads, to a new,
|
|
10
|
+
// empty database of @zukhruf/testing/postgres, and drops it after the diff.
|
|
11
|
+
// - sqlite: with no shadow URL, Prisma replays the migrations on an in-memory
|
|
12
|
+
// SQLite database, so this script sets nothing and starts no database.
|
|
10
13
|
import { spawn } from 'node:child_process';
|
|
14
|
+
import { glob, readFile } from 'node:fs/promises';
|
|
11
15
|
import { parseArgs } from 'node:util';
|
|
12
|
-
import { Postgres } from '@zukhruf/testing/postgres';
|
|
13
16
|
function parse() {
|
|
14
17
|
const { positionals, values } = parseArgs({
|
|
15
18
|
allowPositionals: true,
|
|
@@ -33,10 +36,55 @@ function parse() {
|
|
|
33
36
|
}
|
|
34
37
|
throw new Error('Run `migrate-diff.js check`, or `migrate-diff.js create --name <name>`.');
|
|
35
38
|
}
|
|
39
|
+
class PostgresShadow {
|
|
40
|
+
static async create() {
|
|
41
|
+
// Imported here: @zukhruf/testing is an optional peer, and a repo whose
|
|
42
|
+
// db packages all use SQLite need not install it.
|
|
43
|
+
const { Postgres } = await import('@zukhruf/testing/postgres');
|
|
44
|
+
return new PostgresShadow(await new Postgres({ environment: process.env }).database());
|
|
45
|
+
}
|
|
46
|
+
#database;
|
|
47
|
+
constructor(database) {
|
|
48
|
+
this.#database = database;
|
|
49
|
+
}
|
|
50
|
+
get environment() {
|
|
51
|
+
return { SHADOW_CONNECTION_STRING: this.#database.connectionString };
|
|
52
|
+
}
|
|
53
|
+
[Symbol.asyncDispose]() {
|
|
54
|
+
return this.#database[Symbol.asyncDispose]();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
class SqliteShadow {
|
|
58
|
+
environment = {};
|
|
59
|
+
async [Symbol.asyncDispose]() { }
|
|
60
|
+
}
|
|
61
|
+
const shadows = {
|
|
62
|
+
postgresql: () => PostgresShadow.create(),
|
|
63
|
+
sqlite: async () => new SqliteShadow(),
|
|
64
|
+
};
|
|
65
|
+
const datasourceBlock = /^\s*datasource\s+\w+\s*\{([^}]*)\}/m;
|
|
66
|
+
const providerLine = /^\s*provider\s*=\s*"([^"]+)"/m;
|
|
67
|
+
// The `provider` of the datasource block, from the `*.prisma` files of the
|
|
68
|
+
// schema folder, as `--to-schema .` reads them.
|
|
69
|
+
async function provider() {
|
|
70
|
+
for await (const file of glob('**/*.prisma', {
|
|
71
|
+
exclude: ['**/node_modules/**'],
|
|
72
|
+
})) {
|
|
73
|
+
const block = datasourceBlock.exec(await readFile(file, 'utf8'))?.[1];
|
|
74
|
+
const name = block === undefined ? undefined : providerLine.exec(block)?.[1];
|
|
75
|
+
if (name !== undefined) {
|
|
76
|
+
return name;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
throw new Error('No *.prisma file of this project has a datasource block with a provider.');
|
|
80
|
+
}
|
|
36
81
|
const run = parse();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
82
|
+
const declared = await provider();
|
|
83
|
+
const shadowOf = shadows[declared];
|
|
84
|
+
if (shadowOf === undefined) {
|
|
85
|
+
throw new Error(`migrate-diff has no shadow database for the datasource provider "${declared}" of this schema. It has one for ${Object.keys(shadows).join(' and ')}.`);
|
|
86
|
+
}
|
|
87
|
+
await using shadow = await shadowOf();
|
|
40
88
|
const prisma = spawn('prisma', [
|
|
41
89
|
'migrate',
|
|
42
90
|
'diff',
|
|
@@ -51,7 +99,7 @@ const prisma = spawn('prisma', [
|
|
|
51
99
|
: ['--script', '--output', run.output]),
|
|
52
100
|
], {
|
|
53
101
|
stdio: 'inherit',
|
|
54
|
-
env: { ...process.env,
|
|
102
|
+
env: { ...process.env, ...shadow.environment },
|
|
55
103
|
});
|
|
56
104
|
const { promise: exit, resolve, reject, } = Promise.withResolvers();
|
|
57
105
|
prisma.once('error', reject);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate-diff.js","sourceRoot":"","sources":["../../src/prisma/migrate-diff.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,wDAAwD;AACxD,8EAA8E;AAC9E,yEAAyE;AACzE,
|
|
1
|
+
{"version":3,"file":"migrate-diff.js","sourceRoot":"","sources":["../../src/prisma/migrate-diff.ts"],"names":[],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,EACJ,OAAO,EAAE,IAAI,EACb,OAAO,EACP,MAAM,GACP,GAAG,OAAO,CAAC,aAAa,EAGrB,CAAC;AACL,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC7B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;AAClE,0DAA0D;AAC1D,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC;AACpC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;IAClB,MAAM,IAAI,KAAK,CAAC,kCAAkC,MAAM,GAAG,CAAC,CAAC;AAC/D,CAAC;AACD,+DAA+D;AAC/D,IAAI,GAAG,CAAC,OAAO,KAAK,OAAO,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;IAC1C,OAAO,CAAC,KAAK,CACX,0OAA0O,CAC3O,CAAC;AACJ,CAAC;AACD,IAAI,GAAG,CAAC,OAAO,KAAK,QAAQ,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;IAC3C,OAAO,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;AACxC,CAAC;AACD,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/nx",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.110",
|
|
4
4
|
"description": "The Nx part of the monorepo standard: inference plugins for format, test, lint, opentofu and prisma targets, and generators for the root files (preset, init), apps (node-app, react-app, pair, ui-package), a db package (prisma) and delivery (dokploy).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -132,7 +132,7 @@
|
|
|
132
132
|
"@nx/react": ">=24.0.6 <25.0.0",
|
|
133
133
|
"@nx/vite": ">=24.0.6 <25.0.0",
|
|
134
134
|
"@nx/vitest": ">=24.0.6 <25.0.0",
|
|
135
|
-
"@zukhruf/testing": "^0.8.
|
|
135
|
+
"@zukhruf/testing": "^0.8.110",
|
|
136
136
|
"nx": ">=24.0.6 <25.0.0",
|
|
137
137
|
"prettier": "^3.0.0",
|
|
138
138
|
"typescript": ">=5.8.0 <6.1.0"
|
|
@@ -155,9 +155,9 @@
|
|
|
155
155
|
}
|
|
156
156
|
},
|
|
157
157
|
"devDependencies": {
|
|
158
|
-
"@zukhruf/eslint": "^0.8.
|
|
159
|
-
"@zukhruf/fs": "^0.8.
|
|
160
|
-
"@zukhruf/testing": "^0.8.
|
|
158
|
+
"@zukhruf/eslint": "^0.8.110",
|
|
159
|
+
"@zukhruf/fs": "^0.8.110",
|
|
160
|
+
"@zukhruf/testing": "^0.8.110",
|
|
161
161
|
"semver": "^7.8.5"
|
|
162
162
|
}
|
|
163
163
|
}
|