@syncular/typegen 0.58.0 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -25
- package/dist/cli.js +8 -3
- package/dist/emit-dart.d.ts +4 -3
- package/dist/emit-dart.js +39 -88
- package/dist/emit-kotlin.d.ts +3 -3
- package/dist/emit-kotlin.js +39 -86
- package/dist/emit-queries-dart.d.ts +1 -1
- package/dist/emit-queries-dart.js +17 -26
- package/dist/emit-queries-kotlin.d.ts +1 -1
- package/dist/emit-queries-kotlin.js +15 -24
- package/dist/emit-queries-rust.d.ts +1 -1
- package/dist/emit-queries-rust.js +1 -6
- package/dist/emit-queries-swift.d.ts +1 -1
- package/dist/emit-queries-swift.js +22 -26
- package/dist/emit-queries.d.ts +1 -1
- package/dist/emit-queries.js +1 -6
- package/dist/emit-swift.d.ts +3 -3
- package/dist/emit-swift.js +41 -89
- package/dist/emit.d.ts +1 -1
- package/dist/emit.js +33 -98
- package/dist/errors.d.ts +6 -2
- package/dist/errors.js +10 -2
- package/dist/generate.d.ts +4 -3
- package/dist/generate.js +32 -49
- package/dist/init.js +1 -1
- package/dist/ir.d.ts +11 -0
- package/dist/ir.js +66 -47
- package/dist/lower.d.ts +0 -7
- package/dist/lower.js +0 -62
- package/dist/migration-lock.d.ts +32 -23
- package/dist/migration-lock.js +115 -92
- package/dist/naming.d.ts +25 -0
- package/dist/naming.js +92 -0
- package/dist/query.d.ts +54 -14
- package/dist/query.js +484 -462
- package/dist/sql.js +18 -3
- package/dist/syql-lowering.js +10 -1
- package/dist/syql-validator.js +13 -220
- package/package.json +3 -3
- package/src/cli.ts +9 -3
- package/src/emit-dart.ts +41 -102
- package/src/emit-kotlin.ts +38 -102
- package/src/emit-queries-dart.ts +23 -32
- package/src/emit-queries-kotlin.ts +20 -30
- package/src/emit-queries-rust.ts +7 -12
- package/src/emit-queries-swift.ts +26 -32
- package/src/emit-queries.ts +5 -11
- package/src/emit-swift.ts +42 -104
- package/src/emit.ts +33 -116
- package/src/errors.ts +13 -2
- package/src/generate.ts +74 -76
- package/src/init.ts +4 -1
- package/src/ir.ts +87 -50
- package/src/lower.ts +0 -53
- package/src/migration-lock.ts +192 -137
- package/src/naming.ts +154 -0
- package/src/query.ts +615 -504
- package/src/sql.ts +30 -3
- package/src/syql-lowering.ts +15 -1
- package/src/syql-validator.ts +21 -279
package/src/migration-lock.ts
CHANGED
|
@@ -2,23 +2,24 @@
|
|
|
2
2
|
* Immutable application-migration history.
|
|
3
3
|
*
|
|
4
4
|
* `syncular.migrations.lock.json` is a version-controlled baseline of every
|
|
5
|
-
* migration that has been accepted so far
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* migration that has been accepted so far, with the schema version each one
|
|
6
|
+
* belongs to. Existing entries are immutable; generation may only append newly
|
|
7
|
+
* named migrations, in a schema version above the highest locked one (§7.4).
|
|
8
|
+
* Format 3 stores one canonical head-schema snapshot for actionable
|
|
9
|
+
* diagnostics. Formats 1 and 2 predate per-migration versions: only the
|
|
10
|
+
* explicit `migrations upgrade-lock` command reads them.
|
|
10
11
|
*/
|
|
11
12
|
import { createHash } from 'node:crypto';
|
|
12
13
|
import { existsSync, readFileSync } from 'node:fs';
|
|
13
14
|
import { resolve } from 'node:path';
|
|
14
15
|
import { TypegenError } from './errors';
|
|
15
16
|
import type { MigrationInput } from './generate';
|
|
16
|
-
import type { IrColumn, IrColumnType } from './ir';
|
|
17
|
+
import type { IrColumn, IrColumnType, IrSchemaVersion } from './ir';
|
|
18
|
+
import { MANIFEST_FILENAME, type ManifestSchemaVersion } from './manifest';
|
|
17
19
|
import { applyMigrationSql, type ParsedTable } from './sql';
|
|
18
20
|
|
|
19
21
|
export const MIGRATION_LOCK_FILENAME = 'syncular.migrations.lock.json';
|
|
20
|
-
export const MIGRATION_LOCK_FORMAT_VERSION =
|
|
21
|
-
export const LEGACY_MIGRATION_LOCK_FORMAT_VERSION = 1;
|
|
22
|
+
export const MIGRATION_LOCK_FORMAT_VERSION = 3;
|
|
22
23
|
|
|
23
24
|
export interface MigrationLockColumn {
|
|
24
25
|
readonly name: string;
|
|
@@ -36,18 +37,11 @@ export interface MigrationLockTable {
|
|
|
36
37
|
export interface MigrationLockEntry {
|
|
37
38
|
readonly name: string;
|
|
38
39
|
readonly sha256: string;
|
|
40
|
+
/** The schema version this migration belongs to (§1.5, §7.4). */
|
|
41
|
+
readonly schemaVersion: number;
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
export interface
|
|
42
|
-
readonly tables: readonly MigrationLockTable[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface LegacyMigrationLock {
|
|
46
|
-
readonly formatVersion: typeof LEGACY_MIGRATION_LOCK_FORMAT_VERSION;
|
|
47
|
-
readonly migrations: readonly LegacyMigrationLockEntry[];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface MigrationLockV2 {
|
|
44
|
+
export interface MigrationLock {
|
|
51
45
|
readonly formatVersion: typeof MIGRATION_LOCK_FORMAT_VERSION;
|
|
52
46
|
readonly migrations: readonly MigrationLockEntry[];
|
|
53
47
|
readonly head: {
|
|
@@ -55,10 +49,17 @@ export interface MigrationLockV2 {
|
|
|
55
49
|
};
|
|
56
50
|
}
|
|
57
51
|
|
|
58
|
-
|
|
52
|
+
/** A format-1 or format-2 lock: names and checksums without schema versions,
|
|
53
|
+
* read only to upgrade it to the current format. */
|
|
54
|
+
export interface LegacyMigrationLock {
|
|
55
|
+
readonly formatVersion: 1 | 2;
|
|
56
|
+
readonly migrations: readonly Omit<MigrationLockEntry, 'schemaVersion'>[];
|
|
57
|
+
}
|
|
59
58
|
|
|
60
|
-
interface
|
|
61
|
-
readonly
|
|
59
|
+
interface HistoryEntry {
|
|
60
|
+
readonly name: string;
|
|
61
|
+
readonly sha256: string;
|
|
62
|
+
readonly tables: readonly MigrationLockTable[];
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
function normalizedSql(sql: string): string {
|
|
@@ -94,21 +95,51 @@ function snapshotTables(
|
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
/** The names of every migration a committed lock has made immutable. */
|
|
97
|
-
export function lockedMigrationNames(
|
|
98
|
+
export function lockedMigrationNames(
|
|
99
|
+
lock: MigrationLock | LegacyMigrationLock,
|
|
100
|
+
): ReadonlySet<string> {
|
|
98
101
|
return new Set(lock.migrations.map((migration) => migration.name));
|
|
99
102
|
}
|
|
100
103
|
|
|
104
|
+
/** Resolve the manifest's `schemaVersions` (each naming the last migration
|
|
105
|
+
* it includes) into the migrations every version adds (§1.5). */
|
|
106
|
+
export function buildSchemaVersions(
|
|
107
|
+
declared: readonly ManifestSchemaVersion[],
|
|
108
|
+
migrations: readonly MigrationInput[],
|
|
109
|
+
): IrSchemaVersion[] {
|
|
110
|
+
const names = migrations.map((m) => m.name);
|
|
111
|
+
let cursor = 0;
|
|
112
|
+
const out = declared.map((entry) => {
|
|
113
|
+
const index = names.indexOf(entry.through, cursor);
|
|
114
|
+
if (index === -1) {
|
|
115
|
+
throw new TypegenError(
|
|
116
|
+
MANIFEST_FILENAME,
|
|
117
|
+
`schemaVersions: version ${entry.version} names migration ${JSON.stringify(entry.through)}, which does not exist after ${JSON.stringify(names[cursor - 1] ?? '(start)')} (migrations: ${names.join(', ')})`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const covered = names.slice(cursor, index + 1);
|
|
121
|
+
cursor = index + 1;
|
|
122
|
+
return { version: entry.version, migrations: covered };
|
|
123
|
+
});
|
|
124
|
+
if (cursor !== names.length) {
|
|
125
|
+
throw new TypegenError(
|
|
126
|
+
MANIFEST_FILENAME,
|
|
127
|
+
`schemaVersions: migrations ${names.slice(cursor).join(', ')} are not covered by any schema version — the last entry's "through" must be the last migration`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
101
133
|
/** Replay a complete migration sequence with ephemeral diagnostic snapshots.
|
|
102
134
|
* Migrations named in `lockedNames` replay as deployed history; rules that
|
|
103
135
|
* only appended migrations must satisfy are skipped for them. */
|
|
104
136
|
function buildMigrationHistory(
|
|
105
137
|
migrations: readonly MigrationInput[],
|
|
106
138
|
lockedNames?: ReadonlySet<string>,
|
|
107
|
-
):
|
|
139
|
+
): HistoryEntry[] {
|
|
108
140
|
const tables = new Map<string, ParsedTable>();
|
|
109
141
|
const droppedTables = new Set<string>();
|
|
110
|
-
|
|
111
|
-
for (const migration of migrations) {
|
|
142
|
+
return migrations.map((migration) => {
|
|
112
143
|
applyMigrationSql(
|
|
113
144
|
tables,
|
|
114
145
|
migration.sql,
|
|
@@ -116,52 +147,42 @@ function buildMigrationHistory(
|
|
|
116
147
|
droppedTables,
|
|
117
148
|
{ lockedHistory: lockedNames?.has(migration.name) === true },
|
|
118
149
|
);
|
|
119
|
-
|
|
150
|
+
return {
|
|
120
151
|
name: migration.name,
|
|
121
152
|
sha256: checksum(migration.sql),
|
|
122
153
|
tables: snapshotTables(tables),
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return { migrations: entries };
|
|
154
|
+
};
|
|
155
|
+
});
|
|
126
156
|
}
|
|
127
157
|
|
|
128
158
|
function lockFromHistory(
|
|
129
|
-
history:
|
|
130
|
-
|
|
131
|
-
| typeof LEGACY_MIGRATION_LOCK_FORMAT_VERSION
|
|
132
|
-
| typeof MIGRATION_LOCK_FORMAT_VERSION,
|
|
159
|
+
history: readonly HistoryEntry[],
|
|
160
|
+
schemaVersions: readonly IrSchemaVersion[],
|
|
133
161
|
): MigrationLock {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
if (formatVersion === LEGACY_MIGRATION_LOCK_FORMAT_VERSION) {
|
|
138
|
-
return {
|
|
139
|
-
formatVersion: LEGACY_MIGRATION_LOCK_FORMAT_VERSION,
|
|
140
|
-
migrations: history.migrations,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
const head = history.migrations.at(-1) as LegacyMigrationLockEntry;
|
|
162
|
+
const head = history.at(-1);
|
|
163
|
+
if (head === undefined) failLock('cannot lock an empty migration history');
|
|
144
164
|
return {
|
|
145
165
|
formatVersion: MIGRATION_LOCK_FORMAT_VERSION,
|
|
146
|
-
migrations: history.
|
|
147
|
-
name
|
|
148
|
-
|
|
149
|
-
|
|
166
|
+
migrations: history.map(({ name, sha256 }) => {
|
|
167
|
+
const version = schemaVersions.find((v) => v.migrations.includes(name));
|
|
168
|
+
if (version === undefined) {
|
|
169
|
+
throw new Error(`unreachable: migration ${name} has no schema version`);
|
|
170
|
+
}
|
|
171
|
+
return { name, sha256, schemaVersion: version.version };
|
|
172
|
+
}),
|
|
150
173
|
head: { tables: head.tables },
|
|
151
174
|
};
|
|
152
175
|
}
|
|
153
176
|
|
|
154
|
-
/** Build a deterministic lock document for a complete migration sequence
|
|
177
|
+
/** Build a deterministic lock document for a complete migration sequence;
|
|
178
|
+
* `declared` is the manifest's `schemaVersions`. */
|
|
155
179
|
export function buildMigrationLock(
|
|
156
180
|
migrations: readonly MigrationInput[],
|
|
157
|
-
|
|
158
|
-
| typeof LEGACY_MIGRATION_LOCK_FORMAT_VERSION
|
|
159
|
-
| typeof MIGRATION_LOCK_FORMAT_VERSION = MIGRATION_LOCK_FORMAT_VERSION,
|
|
160
|
-
lockedNames?: ReadonlySet<string>,
|
|
181
|
+
declared: readonly ManifestSchemaVersion[],
|
|
161
182
|
): MigrationLock {
|
|
162
183
|
return lockFromHistory(
|
|
163
|
-
buildMigrationHistory(migrations
|
|
164
|
-
|
|
184
|
+
buildMigrationHistory(migrations),
|
|
185
|
+
buildSchemaVersions(declared, migrations),
|
|
165
186
|
);
|
|
166
187
|
}
|
|
167
188
|
|
|
@@ -233,37 +254,23 @@ function parseTable(value: unknown, context: string): MigrationLockTable {
|
|
|
233
254
|
};
|
|
234
255
|
}
|
|
235
256
|
|
|
236
|
-
function parseEntry(
|
|
257
|
+
function parseEntry(
|
|
258
|
+
value: unknown,
|
|
259
|
+
index: number,
|
|
260
|
+
): Omit<MigrationLockEntry, 'schemaVersion'> & { schemaVersion?: unknown } {
|
|
237
261
|
const context = `migrations[${index}]`;
|
|
238
262
|
if (!isRecord(value)) failLock(`${context} must be an object`);
|
|
239
|
-
const { name, sha256 } = value;
|
|
263
|
+
const { name, sha256, schemaVersion } = value;
|
|
240
264
|
if (typeof name !== 'string' || name.length === 0) {
|
|
241
265
|
failLock(`${context}.name must be a non-empty string`);
|
|
242
266
|
}
|
|
243
267
|
if (typeof sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(sha256)) {
|
|
244
268
|
failLock(`${context}.sha256 must be a lowercase SHA-256 digest`);
|
|
245
269
|
}
|
|
246
|
-
return { name, sha256 };
|
|
270
|
+
return { name, sha256, schemaVersion };
|
|
247
271
|
}
|
|
248
272
|
|
|
249
|
-
function
|
|
250
|
-
value: unknown,
|
|
251
|
-
index: number,
|
|
252
|
-
): LegacyMigrationLockEntry {
|
|
253
|
-
const context = `migrations[${index}]`;
|
|
254
|
-
const entry = parseEntry(value, index);
|
|
255
|
-
if (!isRecord(value)) failLock(`${context} must be an object`);
|
|
256
|
-
const { tables } = value;
|
|
257
|
-
if (!Array.isArray(tables)) failLock(`${context}.tables must be an array`);
|
|
258
|
-
return {
|
|
259
|
-
...entry,
|
|
260
|
-
tables: tables.map((table, tableIndex) =>
|
|
261
|
-
parseTable(table, `${context}.tables[${tableIndex}]`),
|
|
262
|
-
),
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function parseHead(value: unknown): MigrationLockV2['head'] {
|
|
273
|
+
function parseHead(value: unknown): MigrationLock['head'] {
|
|
267
274
|
if (!isRecord(value)) failLock('head must be an object');
|
|
268
275
|
if (!Array.isArray(value.tables)) failLock('head.tables must be an array');
|
|
269
276
|
return {
|
|
@@ -273,20 +280,10 @@ function parseHead(value: unknown): MigrationLockV2['head'] {
|
|
|
273
280
|
};
|
|
274
281
|
}
|
|
275
282
|
|
|
276
|
-
function assertUniqueMigrationNames(
|
|
277
|
-
migrations: readonly MigrationLockEntry[],
|
|
278
|
-
): void {
|
|
279
|
-
const names = new Set<string>();
|
|
280
|
-
for (const migration of migrations) {
|
|
281
|
-
if (names.has(migration.name)) {
|
|
282
|
-
failLock(`migration ${JSON.stringify(migration.name)} appears twice`);
|
|
283
|
-
}
|
|
284
|
-
names.add(migration.name);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
283
|
/** Parse and structurally validate a committed lock document. */
|
|
289
|
-
export function parseMigrationLock(
|
|
284
|
+
export function parseMigrationLock(
|
|
285
|
+
source: string,
|
|
286
|
+
): MigrationLock | LegacyMigrationLock {
|
|
290
287
|
let raw: unknown;
|
|
291
288
|
try {
|
|
292
289
|
raw = JSON.parse(source);
|
|
@@ -294,27 +291,47 @@ export function parseMigrationLock(source: string): MigrationLock {
|
|
|
294
291
|
failLock('invalid JSON');
|
|
295
292
|
}
|
|
296
293
|
if (!isRecord(raw)) failLock('root must be an object');
|
|
294
|
+
const { formatVersion } = raw;
|
|
297
295
|
if (
|
|
298
|
-
|
|
299
|
-
|
|
296
|
+
formatVersion !== 1 &&
|
|
297
|
+
formatVersion !== 2 &&
|
|
298
|
+
formatVersion !== MIGRATION_LOCK_FORMAT_VERSION
|
|
300
299
|
) {
|
|
301
300
|
failLock(
|
|
302
|
-
`formatVersion must be
|
|
301
|
+
`formatVersion must be 1, 2, or ${MIGRATION_LOCK_FORMAT_VERSION}, got ${JSON.stringify(formatVersion)}`,
|
|
303
302
|
);
|
|
304
303
|
}
|
|
305
304
|
if (!Array.isArray(raw.migrations) || raw.migrations.length === 0) {
|
|
306
305
|
failLock('migrations must be a non-empty array');
|
|
307
306
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
307
|
+
const entries = raw.migrations.map(parseEntry);
|
|
308
|
+
const names = new Set<string>();
|
|
309
|
+
for (const { name } of entries) {
|
|
310
|
+
if (names.has(name)) {
|
|
311
|
+
failLock(`migration ${JSON.stringify(name)} appears twice`);
|
|
312
|
+
}
|
|
313
|
+
names.add(name);
|
|
314
|
+
}
|
|
315
|
+
if (formatVersion !== MIGRATION_LOCK_FORMAT_VERSION) {
|
|
311
316
|
return {
|
|
312
|
-
formatVersion
|
|
313
|
-
migrations,
|
|
317
|
+
formatVersion,
|
|
318
|
+
migrations: entries.map(({ name, sha256 }) => ({ name, sha256 })),
|
|
314
319
|
};
|
|
315
320
|
}
|
|
316
|
-
|
|
317
|
-
|
|
321
|
+
let previous = 1;
|
|
322
|
+
const migrations = entries.map(({ name, sha256, schemaVersion }, index) => {
|
|
323
|
+
if (
|
|
324
|
+
typeof schemaVersion !== 'number' ||
|
|
325
|
+
!Number.isInteger(schemaVersion) ||
|
|
326
|
+
schemaVersion < previous
|
|
327
|
+
) {
|
|
328
|
+
failLock(
|
|
329
|
+
`migrations[${index}].schemaVersion must be an integer, at least 1 and at least the previous entry's`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
previous = schemaVersion;
|
|
333
|
+
return { name, sha256, schemaVersion };
|
|
334
|
+
});
|
|
318
335
|
return {
|
|
319
336
|
formatVersion: MIGRATION_LOCK_FORMAT_VERSION,
|
|
320
337
|
migrations,
|
|
@@ -323,7 +340,9 @@ export function parseMigrationLock(source: string): MigrationLock {
|
|
|
323
340
|
}
|
|
324
341
|
|
|
325
342
|
/** Read the version-controlled baseline without exposing its absolute path. */
|
|
326
|
-
export function readMigrationLock(
|
|
343
|
+
export function readMigrationLock(
|
|
344
|
+
manifestDir: string,
|
|
345
|
+
): MigrationLock | LegacyMigrationLock {
|
|
327
346
|
const path = resolve(manifestDir, MIGRATION_LOCK_FILENAME);
|
|
328
347
|
if (!existsSync(path)) {
|
|
329
348
|
failLock(
|
|
@@ -378,13 +397,13 @@ function describeColumnDifference(
|
|
|
378
397
|
}
|
|
379
398
|
|
|
380
399
|
function firstSchemaDifference(
|
|
381
|
-
locked:
|
|
382
|
-
current:
|
|
400
|
+
locked: readonly MigrationLockTable[],
|
|
401
|
+
current: readonly MigrationLockTable[],
|
|
383
402
|
): string {
|
|
384
|
-
const length = Math.max(locked.
|
|
403
|
+
const length = Math.max(locked.length, current.length);
|
|
385
404
|
for (let index = 0; index < length; index++) {
|
|
386
|
-
const before = locked
|
|
387
|
-
const after = current
|
|
405
|
+
const before = locked[index];
|
|
406
|
+
const after = current[index];
|
|
388
407
|
if (before === undefined && after !== undefined) {
|
|
389
408
|
return `table ${JSON.stringify(after.name)} was added`;
|
|
390
409
|
}
|
|
@@ -416,12 +435,11 @@ const REPAIR_HINT =
|
|
|
416
435
|
* New migrations may be appended; existing names and bytes may never change.
|
|
417
436
|
*/
|
|
418
437
|
function validateMigrationHistory(
|
|
419
|
-
locked: MigrationLock,
|
|
420
|
-
current:
|
|
438
|
+
locked: MigrationLock | LegacyMigrationLock,
|
|
439
|
+
current: readonly HistoryEntry[],
|
|
421
440
|
): void {
|
|
422
|
-
|
|
423
|
-
const
|
|
424
|
-
const after = current.migrations[index];
|
|
441
|
+
locked.migrations.forEach((before, index) => {
|
|
442
|
+
const after = current[index];
|
|
425
443
|
if (after === undefined) {
|
|
426
444
|
failLock(
|
|
427
445
|
`history drift at migration ${JSON.stringify(before.name)}: the locked migration is missing; ${REPAIR_HINT}`,
|
|
@@ -433,51 +451,88 @@ function validateMigrationHistory(
|
|
|
433
451
|
);
|
|
434
452
|
}
|
|
435
453
|
if (before.sha256 !== after.sha256) {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
// Format 2 keeps one schema snapshot after the complete locked prefix.
|
|
444
|
-
// Compare it with the current schema at that same boundary, excluding
|
|
445
|
-
// valid newly appended migrations from the diagnostic.
|
|
446
|
-
const diagnosticAfter =
|
|
447
|
-
locked.formatVersion === LEGACY_MIGRATION_LOCK_FORMAT_VERSION
|
|
448
|
-
? after
|
|
449
|
-
: (current.migrations[locked.migrations.length - 1] ?? after);
|
|
454
|
+
// The head snapshot is the schema after the complete locked prefix:
|
|
455
|
+
// compare it with the current schema at that same boundary, excluding
|
|
456
|
+
// valid newly appended migrations. A legacy lock is read without one.
|
|
457
|
+
const difference =
|
|
458
|
+
locked.formatVersion === MIGRATION_LOCK_FORMAT_VERSION
|
|
459
|
+
? `; first schema difference: ${firstSchemaDifference(locked.head.tables, (current[locked.migrations.length - 1] ?? after).tables)}`
|
|
460
|
+
: '';
|
|
450
461
|
failLock(
|
|
451
|
-
`history drift in migration ${JSON.stringify(before.name)}: checksum changed
|
|
462
|
+
`history drift in migration ${JSON.stringify(before.name)}: checksum changed${difference}; ${REPAIR_HINT}`,
|
|
452
463
|
);
|
|
453
464
|
}
|
|
454
|
-
}
|
|
465
|
+
});
|
|
455
466
|
}
|
|
456
467
|
|
|
457
468
|
/**
|
|
458
|
-
* Validate a committed lock against migration inputs and
|
|
459
|
-
*
|
|
460
|
-
* version
|
|
469
|
+
* Validate a committed lock against the migration inputs and their schema
|
|
470
|
+
* versions, and build its exact next document. A locked migration keeps its
|
|
471
|
+
* schema version; an appended migration belongs to a version above the
|
|
472
|
+
* highest locked one, because a deployed version never changes (§7.4).
|
|
461
473
|
*/
|
|
462
474
|
export function updateMigrationLock(
|
|
463
|
-
locked: MigrationLock,
|
|
475
|
+
locked: MigrationLock | LegacyMigrationLock,
|
|
464
476
|
migrations: readonly MigrationInput[],
|
|
477
|
+
declared: readonly ManifestSchemaVersion[],
|
|
465
478
|
): MigrationLock {
|
|
479
|
+
if (locked.formatVersion !== MIGRATION_LOCK_FORMAT_VERSION) {
|
|
480
|
+
throw new TypegenError(
|
|
481
|
+
MIGRATION_LOCK_FILENAME,
|
|
482
|
+
'the lock predates per-migration schema versions; run `syncular migrations upgrade-lock` once and commit the lock',
|
|
483
|
+
'typegen.migration_lock_outdated',
|
|
484
|
+
{ formatVersion: locked.formatVersion },
|
|
485
|
+
);
|
|
486
|
+
}
|
|
466
487
|
const current = buildMigrationHistory(
|
|
467
488
|
migrations,
|
|
468
489
|
lockedMigrationNames(locked),
|
|
469
490
|
);
|
|
470
491
|
validateMigrationHistory(locked, current);
|
|
471
|
-
|
|
492
|
+
const next = lockFromHistory(
|
|
493
|
+
current,
|
|
494
|
+
buildSchemaVersions(declared, migrations),
|
|
495
|
+
);
|
|
496
|
+
const highestLockedVersion = locked.migrations.at(-1)?.schemaVersion ?? 0;
|
|
497
|
+
next.migrations.forEach(({ name, schemaVersion }, index) => {
|
|
498
|
+
const lockedVersion = locked.migrations[index]?.schemaVersion;
|
|
499
|
+
if (lockedVersion !== undefined && lockedVersion !== schemaVersion) {
|
|
500
|
+
throw new TypegenError(
|
|
501
|
+
MIGRATION_LOCK_FILENAME,
|
|
502
|
+
'a locked migration moved to another schema version; a locked version keeps its migration set (§7.4)',
|
|
503
|
+
'typegen.migration_version_changed',
|
|
504
|
+
{ migration: name, lockedVersion, schemaVersion },
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
if (lockedVersion === undefined && schemaVersion <= highestLockedVersion) {
|
|
508
|
+
throw new TypegenError(
|
|
509
|
+
MIGRATION_LOCK_FILENAME,
|
|
510
|
+
'an appended migration joins a locked schema version; declare a schema version above the highest locked one (§7.4)',
|
|
511
|
+
'typegen.schema_version_locked',
|
|
512
|
+
{ migration: name, schemaVersion, highestLockedVersion },
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
return next;
|
|
472
517
|
}
|
|
473
518
|
|
|
474
|
-
/**
|
|
475
|
-
|
|
476
|
-
|
|
519
|
+
/**
|
|
520
|
+
* Convert a format-1 or format-2 lock to the current format: validate its
|
|
521
|
+
* locked prefix and record the schema version the manifest's `declared`
|
|
522
|
+
* versions assign each locked migration. Appended migrations stay unlocked for generation.
|
|
523
|
+
*/
|
|
524
|
+
export function upgradeMigrationLock(
|
|
525
|
+
locked: LegacyMigrationLock,
|
|
477
526
|
migrations: readonly MigrationInput[],
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
527
|
+
declared: readonly ManifestSchemaVersion[],
|
|
528
|
+
): MigrationLock {
|
|
529
|
+
const current = buildMigrationHistory(
|
|
530
|
+
migrations,
|
|
531
|
+
lockedMigrationNames(locked),
|
|
532
|
+
);
|
|
533
|
+
validateMigrationHistory(locked, current);
|
|
534
|
+
return lockFromHistory(
|
|
535
|
+
current.slice(0, locked.migrations.length),
|
|
536
|
+
buildSchemaVersions(declared, migrations),
|
|
482
537
|
);
|
|
483
538
|
}
|
package/src/naming.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* keyword, or a leading underscore on the Dart target (library-private).
|
|
19
19
|
*/
|
|
20
20
|
import { TypegenError } from './errors';
|
|
21
|
+
import type { IrTable } from './ir';
|
|
21
22
|
|
|
22
23
|
/** Manifest `"naming"`: camelCase emission (default) or SQL-truth names. */
|
|
23
24
|
export type NamingMode = 'camel' | 'preserve';
|
|
@@ -44,6 +45,36 @@ export function snakeToCamel(name: string): string {
|
|
|
44
45
|
return lead + first + rest.join('') + trail;
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
/** Generated type name of a table or subscription: `_`/`-` separated parts,
|
|
49
|
+
* each with its first character upper-cased (`project_tasks` →
|
|
50
|
+
* `ProjectTasks`). */
|
|
51
|
+
export function pascalCase(name: string): string {
|
|
52
|
+
return name
|
|
53
|
+
.split(/[_-]+/)
|
|
54
|
+
.filter((part) => part.length > 0)
|
|
55
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
56
|
+
.join('');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `value` as a string literal of a schema emitter's language. Swift and
|
|
60
|
+
* Kotlin delimit with `"`, TS and Dart with `'`; Kotlin and Dart also escape
|
|
61
|
+
* the `$` that would start an interpolation. */
|
|
62
|
+
export function stringLiteral(
|
|
63
|
+
value: string,
|
|
64
|
+
target: Exclude<NamingTarget, 'rust'>,
|
|
65
|
+
): string {
|
|
66
|
+
const delimiter = target === 'swift' || target === 'kotlin' ? '"' : "'";
|
|
67
|
+
const interpolates = target === 'kotlin' || target === 'dart';
|
|
68
|
+
let body = '';
|
|
69
|
+
for (const char of value) {
|
|
70
|
+
body +=
|
|
71
|
+
char === '\\' || char === delimiter || (interpolates && char === '$')
|
|
72
|
+
? `\\${char}`
|
|
73
|
+
: char;
|
|
74
|
+
}
|
|
75
|
+
return `${delimiter}${body}${delimiter}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
47
78
|
/** The emitter targets whose keyword sets we police. `ts` is included for
|
|
48
79
|
* completeness (its emitters can quote any property, so its list is the
|
|
49
80
|
* small set that breaks generated FUNCTION/const identifiers). */
|
|
@@ -419,3 +450,126 @@ export function buildNamingMap(
|
|
|
419
450
|
langName: bySqlName.get(sqlName) as string,
|
|
420
451
|
}));
|
|
421
452
|
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* The schema identifier rules for every configured target in both naming
|
|
456
|
+
* modes. Table names, and column names within a table, stay distinct under
|
|
457
|
+
* SQLite's case-insensitive comparison; generated type names
|
|
458
|
+
* (`pascalCase(table)`) stay distinct; each target's emitted column names
|
|
459
|
+
* stay distinct and clear of its reserved words. Swift, Kotlin, and Dart
|
|
460
|
+
* always emit camelCase fields; TS and Rust follow the naming mode.
|
|
461
|
+
*/
|
|
462
|
+
export function validateSchemaIdentifiers(
|
|
463
|
+
tables: readonly IrTable[],
|
|
464
|
+
mode: NamingMode,
|
|
465
|
+
targets: readonly NamingTarget[],
|
|
466
|
+
context: string,
|
|
467
|
+
): void {
|
|
468
|
+
const distinct = (
|
|
469
|
+
names: readonly string[],
|
|
470
|
+
key: (name: string) => string,
|
|
471
|
+
code: string,
|
|
472
|
+
message: string,
|
|
473
|
+
details: Readonly<Record<string, unknown>> = {},
|
|
474
|
+
): void => {
|
|
475
|
+
const seen = new Map<string, string>();
|
|
476
|
+
for (const name of names) {
|
|
477
|
+
const other = seen.get(key(name));
|
|
478
|
+
if (other !== undefined) {
|
|
479
|
+
throw new TypegenError(context, message, code, {
|
|
480
|
+
...details,
|
|
481
|
+
names: [other, name],
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
seen.set(key(name), name);
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
const caseless = (name: string): string => name.toLowerCase();
|
|
488
|
+
const tableNames = tables.map((table) => table.name);
|
|
489
|
+
distinct(
|
|
490
|
+
tableNames,
|
|
491
|
+
caseless,
|
|
492
|
+
'typegen.duplicate_identifier',
|
|
493
|
+
'two table names differ only in letter case; SQLite compares identifiers case-insensitively',
|
|
494
|
+
);
|
|
495
|
+
distinct(
|
|
496
|
+
tableNames,
|
|
497
|
+
pascalCase,
|
|
498
|
+
'typegen.identifier_collision',
|
|
499
|
+
'two tables map to one generated type name',
|
|
500
|
+
);
|
|
501
|
+
for (const table of tables) {
|
|
502
|
+
const columnNames = table.columns.map((column) => column.name);
|
|
503
|
+
distinct(
|
|
504
|
+
columnNames,
|
|
505
|
+
caseless,
|
|
506
|
+
'typegen.duplicate_identifier',
|
|
507
|
+
'two column names differ only in letter case; SQLite compares identifiers case-insensitively',
|
|
508
|
+
{ table: table.name },
|
|
509
|
+
);
|
|
510
|
+
for (const target of targets) {
|
|
511
|
+
const emit = (name: string): string => {
|
|
512
|
+
const langName =
|
|
513
|
+
mode === 'camel' || (target !== 'ts' && target !== 'rust')
|
|
514
|
+
? snakeToCamel(name)
|
|
515
|
+
: name;
|
|
516
|
+
return target === 'rust' ? rustSnakeCase(langName) : langName;
|
|
517
|
+
};
|
|
518
|
+
distinct(
|
|
519
|
+
columnNames,
|
|
520
|
+
emit,
|
|
521
|
+
'typegen.identifier_collision',
|
|
522
|
+
'two columns map to one generated field name',
|
|
523
|
+
{ target, table: table.name },
|
|
524
|
+
);
|
|
525
|
+
for (const column of columnNames) {
|
|
526
|
+
const emitted = emit(column);
|
|
527
|
+
if (
|
|
528
|
+
TARGET_KEYWORDS[target].has(emitted) ||
|
|
529
|
+
(target === 'dart' && emitted.startsWith('_'))
|
|
530
|
+
) {
|
|
531
|
+
throw new TypegenError(
|
|
532
|
+
context,
|
|
533
|
+
'a column maps to a generated field name that is a reserved word or library-private on an emitter target; rename the column',
|
|
534
|
+
'typegen.reserved_identifier',
|
|
535
|
+
{ target, table: table.name, column, emitted },
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Refuse a query name, result column, or query input that some configured
|
|
545
|
+
* target cannot emit: TS uses the name as is, Rust its snake_case form, and
|
|
546
|
+
* Swift, Kotlin, and Dart its camelCase form. Each must be an identifier
|
|
547
|
+
* clear of the target's reserved words; Dart also refuses a leading `_`.
|
|
548
|
+
*/
|
|
549
|
+
export function validateQueryIdentifier(
|
|
550
|
+
name: string,
|
|
551
|
+
targets: readonly NamingTarget[],
|
|
552
|
+
context: string,
|
|
553
|
+
scope: string,
|
|
554
|
+
): void {
|
|
555
|
+
for (const target of targets) {
|
|
556
|
+
const emitted =
|
|
557
|
+
target === 'rust'
|
|
558
|
+
? rustSnakeCase(name)
|
|
559
|
+
: target === 'ts'
|
|
560
|
+
? name
|
|
561
|
+
: snakeToCamel(name);
|
|
562
|
+
if (
|
|
563
|
+
!/^[A-Za-z_][A-Za-z0-9_]*$/.test(emitted) ||
|
|
564
|
+
TARGET_KEYWORDS[target].has(emitted) ||
|
|
565
|
+
(target === 'dart' && emitted.startsWith('_'))
|
|
566
|
+
) {
|
|
567
|
+
throw new TypegenError(
|
|
568
|
+
context,
|
|
569
|
+
'a query name, result column, or query input is not a valid identifier on an emitter target; rename it or alias it in SQL (AS)',
|
|
570
|
+
'typegen.invalid_query_identifier',
|
|
571
|
+
{ target, scope, name, emitted },
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|