hekireki 0.52.0 → 0.52.2
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 +8 -0
- package/dist/bin/activerecord.js +1 -1
- package/dist/bin/ajv.js +1 -1
- package/dist/bin/arktype.js +1 -1
- package/dist/bin/atlas.js +1 -1
- package/dist/bin/django.js +1 -1
- package/dist/bin/docs.js +1 -1
- package/dist/bin/drizzle.js +1 -1
- package/dist/bin/ecto.js +1 -1
- package/dist/bin/efcore.js +1 -1
- package/dist/bin/effect.js +1 -1
- package/dist/bin/eloquent.js +1 -1
- package/dist/bin/er.js +1 -1
- package/dist/bin/exposed.js +1 -1
- package/dist/bin/gorm.js +1 -1
- package/dist/bin/hekireki.js +5 -5
- package/dist/bin/kysely.js +1 -1
- package/dist/bin/openapi.js +1 -1
- package/dist/bin/pydantic.js +1 -1
- package/dist/bin/sea-orm.js +1 -1
- package/dist/bin/seed.js +1 -1
- package/dist/bin/sqlalchemy.js +1 -1
- package/dist/bin/typebox.js +1 -1
- package/dist/bin/valibot.js +1 -1
- package/dist/bin/zod-openapi-hono.js +1 -1
- package/dist/bin/zod.js +1 -1
- package/dist/{bin-BcqPhgiX.js → bin-i7iQQ-V-.js} +3295 -2428
- package/dist/case-D3JCqTLI.js +54 -0
- package/dist/{discover-BKlX-LLS.js → discover-CLLycWcR.js} +3 -3
- package/dist/{emit-BSQAIFiF.js → emit-DsCT5gLu.js} +5 -2
- package/dist/{errors-fAN1Og-7.js → errors-CRIxtr-P.js} +88 -26
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{migrate-BS6d_8aA.js → migrate-1xVrRUlb.js} +77 -21
- package/dist/{native-engine-DcZUEqO8.js → native-engine-DeQEnJxi.js} +145 -30
- package/dist/{plan-BMypogKp.js → plan-8rsBBJcs.js} +1294 -768
- package/dist/{run-BYq7BEVd.js → run-DbZYkHVI.js} +235 -128
- package/dist/{schema-B7beUJic.js → schema-CyJTUCF1.js} +54 -182
- package/dist/{sql-BwL2BncN.js → sql-LMMCiAlf.js} +48 -19
- package/dist/{start-s1j4PqUM.js → start-Ba8wQAD1.js} +1304 -929
- package/dist/studio/assets/client-8R97eV42.js +2 -0
- package/dist/studio/assets/{confirm-dialog-CKCkchiq.js → confirm-dialog-BfMzwGyk.js} +1 -1
- package/dist/studio/assets/docs-C7FtC4KG.js +1 -0
- package/dist/studio/assets/enums._name-DLAiEdMc.js +1 -0
- package/dist/studio/assets/{field-icon-C1wSRFKO.js → field-icon-LVK7vIIn.js} +1 -1
- package/dist/studio/assets/index-T46k2NyA.js +15 -0
- package/dist/studio/assets/{labels-B5gZ2suK.js → labels-Bn-KWaMa.js} +2 -2
- package/dist/studio/assets/{marked-text-B27DfhKS.js → marked-text-BdU5_qSK.js} +1 -1
- package/dist/studio/assets/models._name-BKzAVcW3.js +1 -0
- package/dist/studio/assets/{monaco-DRYJTcJl.js → monaco-33yj-p0X.js} +1 -1
- package/dist/studio/assets/prisma-DqpMbUwJ.js +2 -0
- package/dist/studio/assets/result-table-PN64JbsN.js +1 -0
- package/dist/studio/assets/{routes-Bl7HRpBJ.js → routes-C1Aze5QM.js} +1 -1
- package/dist/studio/assets/{schema-view-BEdx0cC2.js → schema-view-Bq2_9B5a.js} +4 -4
- package/dist/studio/assets/sql-FZV6GAdV.js +6 -0
- package/dist/studio/assets/{tabs-BpZ0hh1V.js → tabs-GBri63Xo.js} +1 -1
- package/dist/studio/index.html +2 -2
- package/package.json +12 -5
- package/dist/studio/assets/client-C-v3Paav.js +0 -2
- package/dist/studio/assets/docs-CXORFY0Y.js +0 -1
- package/dist/studio/assets/enums._name-BUBU_WMt.js +0 -1
- package/dist/studio/assets/index-DzCcMC-d.js +0 -15
- package/dist/studio/assets/models._name-CQFgPD0T.js +0 -1
- package/dist/studio/assets/prisma-1nDx08Wb.js +0 -2
- package/dist/studio/assets/result-table-0P18Nywh.js +0 -1
- package/dist/studio/assets/sql-GBZZtEKI.js +0 -6
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/naming/case.ts
|
|
2
|
+
function makeSnakeCase(name) {
|
|
3
|
+
return name.replaceAll(/([a-z0-9])([A-Z])/gu, "$1_$2").toLowerCase();
|
|
4
|
+
}
|
|
5
|
+
function makePascalCase(name) {
|
|
6
|
+
return name.split("_").filter((part) => part !== "").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
7
|
+
}
|
|
8
|
+
const IDENTIFIER_LIMITS = {
|
|
9
|
+
sqlite: Infinity,
|
|
10
|
+
mysql: 64
|
|
11
|
+
};
|
|
12
|
+
/** The text with its first character in lower case: a model's Prisma Client delegate, a label. */
|
|
13
|
+
function lowerFirst(text) {
|
|
14
|
+
return text.charAt(0).toLowerCase() + text.slice(1);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The name Prisma Migrate gives a key, index or constraint the schema does not name: the table,
|
|
18
|
+
* the columns and a suffix, the first two cut to the database's longest identifier (63 on
|
|
19
|
+
* PostgreSQL, 64 on MySQL).
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```sql
|
|
23
|
+
* -- @@unique([s, i]) on Scalar; a foreign key on refs.comp_a, comp_b
|
|
24
|
+
* CREATE UNIQUE INDEX "Scalar_s_i_key" ON "Scalar"("s", "i");
|
|
25
|
+
* CONSTRAINT "refs_comp_a_comp_b_fkey" FOREIGN KEY ("comp_a", "comp_b") ...
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function constraintName(table, columns, suffix, provider) {
|
|
29
|
+
const limit = IDENTIFIER_LIMITS[provider] ?? 63;
|
|
30
|
+
return `${[table, ...columns].join("_").slice(0, limit - suffix.length - 1)}_${suffix}`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The part of a column an index takes: MySQL's `@@index([identifier(length: 191)])` on a TEXT. A
|
|
34
|
+
* length the column does not reach is the whole of it, and MySQL keeps none: a String is
|
|
35
|
+
* `varchar(191)` unless its native type says otherwise.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```sql
|
|
39
|
+
* -- identifier String @db.Text, @@index([identifier(length: 191)])
|
|
40
|
+
* CREATE INDEX `verification_identifier_idx` ON `verification`(`identifier`(191));
|
|
41
|
+
* -- userId String, @@index([userId(length: 191)])
|
|
42
|
+
* CREATE INDEX `account_userId_idx` ON `account`(`userId`);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function indexPrefix(model, field) {
|
|
46
|
+
const native = model.fields.find((f) => f.name === field.name)?.nativeType;
|
|
47
|
+
const size = indexableLength(native);
|
|
48
|
+
return field.length === void 0 || field.length >= size ? void 0 : field.length;
|
|
49
|
+
}
|
|
50
|
+
function indexableLength(native) {
|
|
51
|
+
if (native === null || native === void 0) return 191;
|
|
52
|
+
return native[0] === "VarChar" || native[0] === "Char" ? Number(native[1][0] ?? 1) : Infinity;
|
|
53
|
+
}
|
|
54
|
+
export { makeSnakeCase as a, makePascalCase as i, indexPrefix as n, lowerFirst as r, constraintName as t };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { d as importFromProject, f as ADAPTERS, h as LIBSQL_ADAPTER } from "./file-DOGnu_Q2.js";
|
|
2
|
-
import { d as makePostgresSchema, l as isLibsqlUrl, p as makeSqliteFilePath } from "./schema-
|
|
3
|
-
import { l as SeedDatabaseError } from "./sql-
|
|
2
|
+
import { d as makePostgresSchema, l as isLibsqlUrl, p as makeSqliteFilePath } from "./schema-CyJTUCF1.js";
|
|
3
|
+
import { l as SeedDatabaseError } from "./sql-LMMCiAlf.js";
|
|
4
4
|
import { Effect } from "effect";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
@@ -154,7 +154,7 @@ function discoverClient(input) {
|
|
|
154
154
|
source,
|
|
155
155
|
reason: `the Prisma Client at ${source} could not be loaded (${loaded.error}); run \`prisma generate\``
|
|
156
156
|
};
|
|
157
|
-
const clientClass = isRecord(loaded.value) ? loaded.value
|
|
157
|
+
const clientClass = isRecord(loaded.value) ? loaded.value["PrismaClient"] : void 0;
|
|
158
158
|
if (typeof clientClass !== "function") return {
|
|
159
159
|
client: null,
|
|
160
160
|
source,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as makeDirectory, u as writeFile } from "./file-DOGnu_Q2.js";
|
|
2
|
-
import {
|
|
2
|
+
import { R as fmt } from "./schema-CyJTUCF1.js";
|
|
3
3
|
import { Effect } from "effect";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
// src/emit/index.ts
|
|
@@ -22,7 +22,10 @@ function emitRaw(data, dir, output) {
|
|
|
22
22
|
function emitMany(files, dir) {
|
|
23
23
|
return Effect.gen(function* () {
|
|
24
24
|
yield* makeDirectory(dir);
|
|
25
|
-
yield* Effect.forEach(files, (f) => writeFile(path.join(dir, f.fileName), f.code), {
|
|
25
|
+
yield* Effect.forEach(files, (f) => writeFile(path.join(dir, f.fileName), f.code), {
|
|
26
|
+
discard: true,
|
|
27
|
+
concurrency: 1
|
|
28
|
+
});
|
|
26
29
|
});
|
|
27
30
|
}
|
|
28
31
|
export { emitMany as n, emitRaw as r, emit as t };
|
|
@@ -139,11 +139,26 @@ function toTsType(dialect, dataType) {
|
|
|
139
139
|
default: return "unknown";
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
|
+
const LITERAL_TYPES = {
|
|
143
|
+
string: {
|
|
144
|
+
sqlite: "TEXT",
|
|
145
|
+
mysql: "varchar",
|
|
146
|
+
postgresql: "text"
|
|
147
|
+
},
|
|
148
|
+
number: {
|
|
149
|
+
sqlite: "INTEGER",
|
|
150
|
+
mysql: "int",
|
|
151
|
+
postgresql: "int4"
|
|
152
|
+
},
|
|
153
|
+
boolean: {
|
|
154
|
+
sqlite: "INTEGER",
|
|
155
|
+
mysql: "tinyint",
|
|
156
|
+
postgresql: "bool"
|
|
157
|
+
}
|
|
158
|
+
};
|
|
142
159
|
/** The declared type a literal of the expression kind would have in the dialect, for the type column of the picture. */
|
|
143
160
|
function literalDataType(dialect, kind) {
|
|
144
|
-
|
|
145
|
-
if (kind === "number") return dialect === "sqlite" ? "INTEGER" : dialect === "mysql" ? "int" : "int4";
|
|
146
|
-
return dialect === "postgresql" ? "bool" : dialect === "mysql" ? "tinyint" : "INTEGER";
|
|
161
|
+
return LITERAL_TYPES[kind][dialect];
|
|
147
162
|
}
|
|
148
163
|
// src/sql/split.ts
|
|
149
164
|
/**
|
|
@@ -155,29 +170,11 @@ function splitStatements(text) {
|
|
|
155
170
|
let mode = "code";
|
|
156
171
|
let quote = "";
|
|
157
172
|
for (let index = 0; index < text.length; index += 1) {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (char === "*" && next === "/") {
|
|
164
|
-
mode = "code";
|
|
165
|
-
index += 1;
|
|
166
|
-
}
|
|
167
|
-
} else if (mode === "quoted") {
|
|
168
|
-
if (char === quote) {
|
|
169
|
-
if (next === quote) index += 1;
|
|
170
|
-
else {
|
|
171
|
-
mode = "code";
|
|
172
|
-
quote = "";
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
} else if (char === "-" && next === "-") mode = "line-comment";
|
|
176
|
-
else if (char === "/" && next === "*") mode = "block-comment";
|
|
177
|
-
else if (char === "'" || char === "\"" || char === "`") {
|
|
178
|
-
mode = "quoted";
|
|
179
|
-
quote = char;
|
|
180
|
-
} else if (char === ";") ends.push(index);
|
|
173
|
+
const scanned = scan(mode, quote, text[index] ?? "", text[index + 1] ?? "");
|
|
174
|
+
mode = scanned.mode;
|
|
175
|
+
quote = scanned.quote;
|
|
176
|
+
if (scanned.end) ends.push(index);
|
|
177
|
+
if (scanned.skip) index += 1;
|
|
181
178
|
}
|
|
182
179
|
const bounds = [...ends, text.length];
|
|
183
180
|
return bounds.map((end, index) => text.slice(index === 0 ? 0 : (bounds[index - 1] ?? 0) + 1, end).trim()).filter((statement) => statement !== "");
|
|
@@ -205,6 +202,71 @@ function splitTopLevel(text) {
|
|
|
205
202
|
}
|
|
206
203
|
return [...parts, current.trim()].filter((part) => part !== "");
|
|
207
204
|
}
|
|
205
|
+
function scan(mode, quote, char, next) {
|
|
206
|
+
if (mode === "line-comment") return {
|
|
207
|
+
mode: char === "\n" ? "code" : mode,
|
|
208
|
+
quote,
|
|
209
|
+
skip: false,
|
|
210
|
+
end: false
|
|
211
|
+
};
|
|
212
|
+
if (mode === "block-comment") {
|
|
213
|
+
const closes = char === "*" && next === "/";
|
|
214
|
+
return {
|
|
215
|
+
mode: closes ? "code" : mode,
|
|
216
|
+
quote,
|
|
217
|
+
skip: closes,
|
|
218
|
+
end: false
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (mode === "quoted") return scanQuoted(quote, char, next);
|
|
222
|
+
return scanCode(char, next);
|
|
223
|
+
}
|
|
224
|
+
function scanQuoted(quote, char, next) {
|
|
225
|
+
if (char === quote && next === quote) return {
|
|
226
|
+
mode: "quoted",
|
|
227
|
+
quote,
|
|
228
|
+
skip: true,
|
|
229
|
+
end: false
|
|
230
|
+
};
|
|
231
|
+
if (char === quote) return {
|
|
232
|
+
mode: "code",
|
|
233
|
+
quote: "",
|
|
234
|
+
skip: false,
|
|
235
|
+
end: false
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
mode: "quoted",
|
|
239
|
+
quote,
|
|
240
|
+
skip: false,
|
|
241
|
+
end: false
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function scanCode(char, next) {
|
|
245
|
+
if (char === "-" && next === "-") return {
|
|
246
|
+
mode: "line-comment",
|
|
247
|
+
quote: "",
|
|
248
|
+
skip: false,
|
|
249
|
+
end: false
|
|
250
|
+
};
|
|
251
|
+
if (char === "/" && next === "*") return {
|
|
252
|
+
mode: "block-comment",
|
|
253
|
+
quote: "",
|
|
254
|
+
skip: false,
|
|
255
|
+
end: false
|
|
256
|
+
};
|
|
257
|
+
if (char === "'" || char === "\"" || char === "`") return {
|
|
258
|
+
mode: "quoted",
|
|
259
|
+
quote: char,
|
|
260
|
+
skip: false,
|
|
261
|
+
end: false
|
|
262
|
+
};
|
|
263
|
+
return {
|
|
264
|
+
mode: "code",
|
|
265
|
+
quote: "",
|
|
266
|
+
skip: false,
|
|
267
|
+
end: char === ";"
|
|
268
|
+
};
|
|
269
|
+
}
|
|
208
270
|
// src/migrate/errors.ts
|
|
209
271
|
/** The schema cannot be read, or the command line names something the check cannot use. */
|
|
210
272
|
var MigrateConfigError = class extends Data.TaggedError("MigrateConfigError") {};
|
package/dist/index.d.ts
CHANGED
|
@@ -142,7 +142,7 @@ type Data<S extends SeedSchema, M extends ModelName<S>, Filled extends string =
|
|
|
142
142
|
* file: `export const users = defineData(schema, 'User', [{ email: '...', name: '...' }])`. The
|
|
143
143
|
* rows are returned as they are; the check is the same one `defineConfig` makes.
|
|
144
144
|
*/
|
|
145
|
-
export declare function defineData<const S extends SeedSchema, M extends ModelName<S>>(
|
|
145
|
+
export declare function defineData<const S extends SeedSchema, M extends ModelName<S>>(_schema: S, _model: M, rows: NoInfer<Data<S, M>>): Data<S, M, never>;
|
|
146
146
|
/**
|
|
147
147
|
* How one field is filled: a function of the seeded faker and the row so far, or bounds that fit
|
|
148
148
|
* the field's type. `value` fixes it, `values` draws from a list (enum members by Prisma name).
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ function defineSchema(schema) {
|
|
|
8
8
|
* file: `export const users = defineData(schema, 'User', [{ email: '...', name: '...' }])`. The
|
|
9
9
|
* rows are returned as they are; the check is the same one `defineConfig` makes.
|
|
10
10
|
*/
|
|
11
|
-
function defineData(
|
|
11
|
+
function defineData(_schema, _model, rows) {
|
|
12
12
|
return rows;
|
|
13
13
|
}
|
|
14
14
|
function defineConfig(...args) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { s as readFile } from "./file-DOGnu_Q2.js";
|
|
2
|
-
import "./schema-
|
|
3
|
-
import { r as emitRaw } from "./emit-
|
|
4
|
-
import { a as splitStatements, t as MigrateConfigError } from "./errors-
|
|
5
|
-
import { i as lostInRebuild, l as runMigrateCheck, n as isDestructiveStep, o as BLOCK_QUOTE, r as isLossyFix, s as rewriteMigration, t as batchedStatements } from "./plan-
|
|
2
|
+
import "./schema-CyJTUCF1.js";
|
|
3
|
+
import { r as emitRaw } from "./emit-DsCT5gLu.js";
|
|
4
|
+
import { a as splitStatements, t as MigrateConfigError } from "./errors-CRIxtr-P.js";
|
|
5
|
+
import { i as lostInRebuild, l as runMigrateCheck, n as isDestructiveStep, o as BLOCK_QUOTE, r as isLossyFix, s as rewriteMigration, t as batchedStatements } from "./plan-8rsBBJcs.js";
|
|
6
6
|
import { Effect } from "effect";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
// src/migrate/adapter/plan-file.ts
|
|
@@ -70,22 +70,17 @@ function planSql(report, migration = null, batch = null) {
|
|
|
70
70
|
after: report.rewrite.after,
|
|
71
71
|
nullable: report.rewrite.nullable
|
|
72
72
|
});
|
|
73
|
-
const errors =
|
|
73
|
+
const errors = planErrors(migration, rewritten, inMigration);
|
|
74
74
|
const transaction = rewritten?.transaction === true;
|
|
75
|
-
const postgres = report.database.dialect === "postgresql" && !report.database.cockroach;
|
|
76
75
|
const before = report.fixes.filter((fix) => !fix.inMigration);
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
...batch !== null && transaction ? ["--batch changes nothing here: the DO block is one transaction, which holds every lock until it ends however its statements are cut."] : [],
|
|
86
|
-
...rewritten === null ? [] : lostInRebuild(report, splitStatements(rewritten.sql)),
|
|
87
|
-
...lossy ? [transaction ? "It deletes rows, drops what holds them or writes over values. The DO block undoes itself only if it fails: once it has run, a backup taken before it is the only way back." : "It deletes rows, drops what holds them or writes over values, and nothing here undoes a statement that has run: take a backup before it."] : []
|
|
88
|
-
];
|
|
76
|
+
const notes = planNotes({
|
|
77
|
+
report,
|
|
78
|
+
migration,
|
|
79
|
+
batch,
|
|
80
|
+
rewritten,
|
|
81
|
+
transaction,
|
|
82
|
+
before
|
|
83
|
+
});
|
|
89
84
|
const blocks = [...before.map((fix) => (transaction ? fix.statements : batchedStatements(report.database, fix, batch)).map((statement) => `${statement};`).join("\n")), ...rewritten === null ? [] : [rewritten.sql.trimEnd()]];
|
|
90
85
|
const statements = transaction ? [`DO ${BLOCK_QUOTE}\n#variable_conflict use_column\nBEGIN\n${blocks.join("\n\n")}\nEND ${BLOCK_QUOTE};`] : blocks;
|
|
91
86
|
return {
|
|
@@ -94,6 +89,53 @@ function planSql(report, migration = null, batch = null) {
|
|
|
94
89
|
notes
|
|
95
90
|
};
|
|
96
91
|
}
|
|
92
|
+
/** What the plan cannot do: a migration it wrote itself, a change the migration has no statement for, or no migration given. */
|
|
93
|
+
function planErrors(migration, rewritten, inMigration) {
|
|
94
|
+
const path = migration?.path ?? "migration.sql";
|
|
95
|
+
if (migration?.sql.split("\n").some((line) => line.trim() === GENERATED) === true) return [`${path}: already written by hekireki migrate plan. Pass the migration.sql Prisma wrote (\`prisma migrate diff --script\` writes it again).`];
|
|
96
|
+
if (rewritten !== null) return rewritten.errors.map((error) => `${path}: ${error}.`);
|
|
97
|
+
if (inMigration > 0) return [`The decisions ask the migration itself for ${plural(inMigration, "change")} (a rename, a conversion, an added column filled, an enum value mapped in its cast or values moved to another table): pass --migration with the migration.sql Prisma wrote for this schema.`];
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
/** Where the plan goes. */
|
|
101
|
+
function placementNote(migration, before) {
|
|
102
|
+
if (migration !== null) return "Put it in place of the migration.sql Prisma wrote.";
|
|
103
|
+
return before.length === 0 ? "No fixes decided: nothing to run before the migration." : "Put it at the top of the migration Prisma writes for the schema (`prisma migrate dev --create-only` writes one to edit), or pass that migration with --migration.";
|
|
104
|
+
}
|
|
105
|
+
/** On PostgreSQL: whether the plan could run, or why it does not run, as one statement. */
|
|
106
|
+
function oneStatementNotes(migration, before, transaction) {
|
|
107
|
+
if (migration === null && before.length > 0) return ["With --migration, the plan runs the fixes and the migration as one statement, whole or not at all."];
|
|
108
|
+
if (migration !== null && !transaction) return ["Not as one statement here: PostgreSQL cannot use an enum value in the transaction that adds it or make an index CONCURRENTLY in one, and a DO block does not run a SELECT as it is."];
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
/** A fix over many rows: how long it holds its locks. */
|
|
112
|
+
function heavyNote(fix, batch, transaction) {
|
|
113
|
+
const parted = fix.parts !== null && fix.parts !== void 0;
|
|
114
|
+
const rows = plural(fix.rows ?? 0, "row");
|
|
115
|
+
if (batch !== null && !transaction && parted) return `${fix.subject}: ${rows}, ${batch} at a time. Each statement holds its locks only for its own rows; the last one takes whatever was written since the check.`;
|
|
116
|
+
return `${fix.subject}: ${rows} in one statement, which holds its locks until it ends. On a table in use, run it while the table is quiet${transaction || !parted ? "" : ", or pass --batch to run it some rows at a time"}.`;
|
|
117
|
+
}
|
|
118
|
+
/** What a person needs to know about running the plan, for the command to print. */
|
|
119
|
+
function planNotes(plan) {
|
|
120
|
+
const { report, migration, batch, rewritten, transaction, before } = plan;
|
|
121
|
+
const postgres = report.database.dialect === "postgresql" && !report.database.cockroach;
|
|
122
|
+
const heavy = before.filter((fix) => (fix.rows ?? 0) >= HEAVY_ROWS);
|
|
123
|
+
const migrationStatements = rewritten === null ? null : splitStatements(rewritten.sql);
|
|
124
|
+
const lossy = before.some(isLossyFix) || migrationStatements !== null && isDestructiveStep(migrationStatements);
|
|
125
|
+
return [
|
|
126
|
+
placementNote(migration, before),
|
|
127
|
+
transaction ? "One statement, a DO block PostgreSQL runs whole or not at all: if a statement in it fails, none of it is done, the fixes included." : "Prisma runs a migration a statement at a time, with no transaction around it: if a statement fails, the ones before it, the fixes included, stay done.",
|
|
128
|
+
...postgres ? oneStatementNotes(migration, before, transaction) : [],
|
|
129
|
+
...before.length === 0 && rewritten === null ? [] : ["The check counted the rows as they were when it ran. Rows written since are not in it: stop what writes to these tables while the plan runs, or run `hekireki migrate check` again right before it."],
|
|
130
|
+
...heavy.map((fix) => heavyNote(fix, batch, transaction)),
|
|
131
|
+
...batch !== null && transaction ? ["--batch changes nothing here: the DO block is one transaction, which holds every lock until it ends however its statements are cut."] : [],
|
|
132
|
+
...migrationStatements === null ? [] : lostInRebuild(report, migrationStatements),
|
|
133
|
+
...lossy ? [lossyNote(transaction)] : []
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
function lossyNote(transaction) {
|
|
137
|
+
return transaction ? "It deletes rows, drops what holds them or writes over values. The DO block undoes itself only if it fails: once it has run, a backup taken before it is the only way back." : "It deletes rows, drops what holds them or writes over values, and nothing here undoes a statement that has run: take a backup before it.";
|
|
138
|
+
}
|
|
97
139
|
// src/migrate/report.ts
|
|
98
140
|
/** How many checks came out each way; the migration is clear when nothing blocks or failed. */
|
|
99
141
|
function summarize(report) {
|
|
@@ -137,14 +179,14 @@ function checkBanner(report) {
|
|
|
137
179
|
"",
|
|
138
180
|
` Fixes from ${path.relative(process.cwd(), report.decisionsPath) || report.decisionsPath}, counted in: the checks read the rows as they leave them`,
|
|
139
181
|
...report.fixes.map((fix) => {
|
|
140
|
-
const found =
|
|
182
|
+
const found = rowsFound(fix);
|
|
141
183
|
return ` ${fix.rows === null ? "?" : "✓"} ${fix.subject.padEnd(fixWidth)} ${fix.action.padEnd(actionWidth)} ${found}`;
|
|
142
184
|
}),
|
|
143
185
|
...renamed.map((rename) => ` ✓ ${rename.subject.padEnd(fixWidth)} ${rename.action.padEnd(actionWidth)} in the migration`),
|
|
144
|
-
|
|
186
|
+
planHint(inMigration)
|
|
145
187
|
];
|
|
146
188
|
return [
|
|
147
|
-
|
|
189
|
+
headlineOf(summary),
|
|
148
190
|
` Schema: ${report.schemaPath}`,
|
|
149
191
|
` Database: ${report.database.dialect} ${report.database.url}`.trimEnd(),
|
|
150
192
|
` Checks: ${summary.passed} passed, ${summary.guaranteed} already guaranteed by the database's constraints`,
|
|
@@ -179,4 +221,18 @@ function checkJson(report) {
|
|
|
179
221
|
}))
|
|
180
222
|
}, null, 2);
|
|
181
223
|
}
|
|
224
|
+
/** What the check found of a fix's rows. */
|
|
225
|
+
function rowsFound(fix) {
|
|
226
|
+
if (fix.rows === null) return `not counted: ${fix.error ?? ""}`;
|
|
227
|
+
return fix.rows === 0 ? "nothing to fix: drop it once the migration has shipped" : plural(fix.rows, "row");
|
|
228
|
+
}
|
|
229
|
+
/** Which command writes the fixes, by whether they go into the migration, before it, or both. */
|
|
230
|
+
function planHint(inMigration) {
|
|
231
|
+
if (inMigration.every(Boolean)) return " `hekireki migrate plan --migration` writes them into the migration Prisma wrote.";
|
|
232
|
+
return inMigration.some(Boolean) ? " `hekireki migrate plan --migration` writes them as SQL: renames, conversions, fills and new enum members into the migration Prisma wrote, the rest before it." : " `hekireki migrate plan` writes them as SQL to run before the migration.";
|
|
233
|
+
}
|
|
234
|
+
function headlineOf(summary) {
|
|
235
|
+
if (!summary.ok) return `⚡️ Migration check: ${plural(summary.blocking, "blocking problem")}${summary.failed === 0 ? "" : `, ${summary.failed} not checked`}${summary.warning === 0 ? "" : `, ${plural(summary.warning, "warning")}`}`;
|
|
236
|
+
return summary.warning === 0 ? `⚡️ Migration check: the data is ready for this schema (${plural(summary.passed + summary.guaranteed, "check")})` : `⚡️ Migration check: nothing blocks, ${plural(summary.warning, "warning")}`;
|
|
237
|
+
}
|
|
182
238
|
export { checkBanner, checkJson, planSql, readMigration, runMigrateCheck, summarize, writePlan };
|