@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/dist/ir.js
CHANGED
|
@@ -12,6 +12,14 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { createHash } from 'node:crypto';
|
|
14
14
|
export const IR_VERSION = 1;
|
|
15
|
+
/** §5.11: the app-side type of a column. An encrypted column rides the wire
|
|
16
|
+
* as `bytes`; generated rows expose its declared type (the local mirror is
|
|
17
|
+
* plaintext). */
|
|
18
|
+
export function appColumnType(column) {
|
|
19
|
+
return column.encrypted === true && column.declaredType !== undefined
|
|
20
|
+
? column.declaredType
|
|
21
|
+
: column.type;
|
|
22
|
+
}
|
|
15
23
|
/** Recursively sort object keys so extension payloads serialize stably. */
|
|
16
24
|
export function canonicalizeExtensions(value) {
|
|
17
25
|
if (Array.isArray(value))
|
|
@@ -31,6 +39,47 @@ function canonicalScopeValue(value) {
|
|
|
31
39
|
? { kind: 'literal', value: value.value }
|
|
32
40
|
: { kind: 'parameter', name: value.name };
|
|
33
41
|
}
|
|
42
|
+
/** Column, reference, and index entries shared by the IR document and the
|
|
43
|
+
* schema object, in their fixed key order. Optional members (crdtType,
|
|
44
|
+
* encrypted/declaredType, references, indexes, ftsIndexes) are emitted only
|
|
45
|
+
* when present so documents without them stay byte-identical. */
|
|
46
|
+
function serializeTableShape(table, scopes) {
|
|
47
|
+
return {
|
|
48
|
+
name: table.name,
|
|
49
|
+
primaryKey: table.primaryKey,
|
|
50
|
+
columns: table.columns.map((column) => ({
|
|
51
|
+
name: column.name,
|
|
52
|
+
type: column.type,
|
|
53
|
+
nullable: column.nullable,
|
|
54
|
+
...(column.crdtType !== undefined ? { crdtType: column.crdtType } : {}),
|
|
55
|
+
...(column.encrypted === true
|
|
56
|
+
? { encrypted: true, declaredType: column.declaredType }
|
|
57
|
+
: {}),
|
|
58
|
+
})),
|
|
59
|
+
scopes,
|
|
60
|
+
...(table.references.length > 0
|
|
61
|
+
? { references: table.references.map((reference) => ({ ...reference })) }
|
|
62
|
+
: {}),
|
|
63
|
+
...(table.indexes.length > 0
|
|
64
|
+
? {
|
|
65
|
+
indexes: table.indexes.map((index) => ({
|
|
66
|
+
name: index.name,
|
|
67
|
+
columns: index.columns,
|
|
68
|
+
unique: index.unique,
|
|
69
|
+
})),
|
|
70
|
+
}
|
|
71
|
+
: {}),
|
|
72
|
+
...(table.ftsIndexes.length > 0
|
|
73
|
+
? {
|
|
74
|
+
ftsIndexes: table.ftsIndexes.map((index) => ({
|
|
75
|
+
name: index.name,
|
|
76
|
+
columns: index.columns,
|
|
77
|
+
tokenize: index.tokenize,
|
|
78
|
+
})),
|
|
79
|
+
}
|
|
80
|
+
: {}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
34
83
|
/** Serialize with a fixed key order — byte-deterministic for equal IRs. */
|
|
35
84
|
export function serializeIr(ir) {
|
|
36
85
|
const doc = {
|
|
@@ -41,56 +90,11 @@ export function serializeIr(ir) {
|
|
|
41
90
|
migrations: v.migrations,
|
|
42
91
|
})),
|
|
43
92
|
tables: ir.tables.map((table) => ({
|
|
44
|
-
|
|
45
|
-
primaryKey: table.primaryKey,
|
|
46
|
-
columns: table.columns.map((column) => ({
|
|
47
|
-
name: column.name,
|
|
48
|
-
type: column.type,
|
|
49
|
-
nullable: column.nullable,
|
|
50
|
-
// §5.10.1: crdtType is IR metadata (never on the wire); emitted only
|
|
51
|
-
// for crdt columns so non-crdt column diffs stay byte-identical.
|
|
52
|
-
...(column.crdtType !== undefined ? { crdtType: column.crdtType } : {}),
|
|
53
|
-
// §5.11: encrypted/declaredType are IR metadata (never on the wire);
|
|
54
|
-
// emitted only for encrypted columns so non-encrypted diffs stay
|
|
55
|
-
// byte-identical.
|
|
56
|
-
...(column.encrypted === true
|
|
57
|
-
? { encrypted: true, declaredType: column.declaredType }
|
|
58
|
-
: {}),
|
|
59
|
-
})),
|
|
60
|
-
scopes: table.scopes.map((scope) => ({
|
|
93
|
+
...serializeTableShape(table, table.scopes.map((scope) => ({
|
|
61
94
|
pattern: scope.pattern,
|
|
62
95
|
variable: scope.variable,
|
|
63
96
|
column: scope.column,
|
|
64
|
-
})),
|
|
65
|
-
// Declared references (§6.11) are additive under irVersion 1: emitted
|
|
66
|
-
// only when the table declares at least one, so reference-free tables
|
|
67
|
-
// stay byte-identical.
|
|
68
|
-
...(table.references.length > 0
|
|
69
|
-
? {
|
|
70
|
-
references: table.references.map((reference) => ({ ...reference })),
|
|
71
|
-
}
|
|
72
|
-
: {}),
|
|
73
|
-
// Indexes are additive under irVersion 1: emitted only when the table
|
|
74
|
-
// declares at least one, so index-free tables (every pre-index manifest)
|
|
75
|
-
// stay byte-identical and --check fresh. Declaration order preserved.
|
|
76
|
-
...(table.indexes.length > 0
|
|
77
|
-
? {
|
|
78
|
-
indexes: table.indexes.map((index) => ({
|
|
79
|
-
name: index.name,
|
|
80
|
-
columns: index.columns,
|
|
81
|
-
unique: index.unique,
|
|
82
|
-
})),
|
|
83
|
-
}
|
|
84
|
-
: {}),
|
|
85
|
-
...(table.ftsIndexes.length > 0
|
|
86
|
-
? {
|
|
87
|
-
ftsIndexes: table.ftsIndexes.map((index) => ({
|
|
88
|
-
name: index.name,
|
|
89
|
-
columns: index.columns,
|
|
90
|
-
tokenize: index.tokenize,
|
|
91
|
-
})),
|
|
92
|
-
}
|
|
93
|
-
: {}),
|
|
97
|
+
}))),
|
|
94
98
|
extensions: canonicalizeExtensions(table.extensions),
|
|
95
99
|
})),
|
|
96
100
|
subscriptions: ir.subscriptions.map((sub) => ({
|
|
@@ -105,6 +109,21 @@ export function serializeIr(ir) {
|
|
|
105
109
|
};
|
|
106
110
|
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
107
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* The ServerSchema-compatible schema object (§2.4, §3.1) as 2-space JSON.
|
|
114
|
+
* Every emitter embeds these exact bytes: TS as an object literal, Swift,
|
|
115
|
+
* Kotlin, and Dart as a string literal they parse, so every platform hands
|
|
116
|
+
* its client the same tables, scopes, references, and indexes.
|
|
117
|
+
*/
|
|
118
|
+
export function serializeSchema(ir) {
|
|
119
|
+
return JSON.stringify({
|
|
120
|
+
version: ir.schemaVersion,
|
|
121
|
+
tables: ir.tables.map((table) => serializeTableShape(table, table.scopes.map((scope) => ({
|
|
122
|
+
pattern: scope.pattern,
|
|
123
|
+
column: scope.column,
|
|
124
|
+
})))),
|
|
125
|
+
}, null, 2);
|
|
126
|
+
}
|
|
108
127
|
/** Hash of the exact IR file bytes; stamped into generated modules. */
|
|
109
128
|
export function irHash(irJson) {
|
|
110
129
|
return `sha256:${createHash('sha256').update(irJson, 'utf8').digest('hex')}`;
|
package/dist/lower.d.ts
CHANGED
|
@@ -19,13 +19,6 @@ interface ProjectionSpan {
|
|
|
19
19
|
* when there is no depth-0 SELECT…FROM shape (e.g. `SELECT 1`).
|
|
20
20
|
*/
|
|
21
21
|
export declare function findProjection(sql: string): ProjectionSpan | null;
|
|
22
|
-
/**
|
|
23
|
-
* The main verb of a `WITH …` statement (uppercased): SQLite allows a
|
|
24
|
-
* with-clause before SELECT and before INSERT/UPDATE/DELETE. CTE bodies live
|
|
25
|
-
* inside parentheses and a bare keyword cannot be a CTE name, so the first
|
|
26
|
-
* paren-depth-0 keyword after `WITH` is the main verb.
|
|
27
|
-
*/
|
|
28
|
-
export declare function mainVerbAfterWith(sql: string): string | undefined;
|
|
29
22
|
/** A `FROM`/`JOIN` base-table reference the caller already resolved. */
|
|
30
23
|
export interface LowerTableRef {
|
|
31
24
|
readonly table: string;
|
package/dist/lower.js
CHANGED
|
@@ -115,68 +115,6 @@ export function findProjection(sql) {
|
|
|
115
115
|
}
|
|
116
116
|
return null;
|
|
117
117
|
}
|
|
118
|
-
/**
|
|
119
|
-
* The main verb of a `WITH …` statement (uppercased): SQLite allows a
|
|
120
|
-
* with-clause before SELECT and before INSERT/UPDATE/DELETE. CTE bodies live
|
|
121
|
-
* inside parentheses and a bare keyword cannot be a CTE name, so the first
|
|
122
|
-
* paren-depth-0 keyword after `WITH` is the main verb.
|
|
123
|
-
*/
|
|
124
|
-
export function mainVerbAfterWith(sql) {
|
|
125
|
-
const MAIN_VERBS = new Set([
|
|
126
|
-
'SELECT',
|
|
127
|
-
'VALUES',
|
|
128
|
-
'INSERT',
|
|
129
|
-
'UPDATE',
|
|
130
|
-
'DELETE',
|
|
131
|
-
'REPLACE',
|
|
132
|
-
]);
|
|
133
|
-
let depth = 0;
|
|
134
|
-
let i = 0;
|
|
135
|
-
let sawWith = false;
|
|
136
|
-
while (i < sql.length) {
|
|
137
|
-
const ch = sql[i];
|
|
138
|
-
const next = sql[i + 1];
|
|
139
|
-
if (ch === '-' && next === '-') {
|
|
140
|
-
const end = sql.indexOf('\n', i);
|
|
141
|
-
i = end === -1 ? sql.length : end + 1;
|
|
142
|
-
}
|
|
143
|
-
else if (ch === '/' && next === '*') {
|
|
144
|
-
const end = sql.indexOf('*/', i + 2);
|
|
145
|
-
i = end === -1 ? sql.length : end + 2;
|
|
146
|
-
}
|
|
147
|
-
else if (ch === "'" || ch === '"' || ch === '`') {
|
|
148
|
-
let j = i + 1;
|
|
149
|
-
while (j < sql.length && sql[j] !== ch)
|
|
150
|
-
j += 1;
|
|
151
|
-
i = j + 1;
|
|
152
|
-
}
|
|
153
|
-
else if (ch === '(') {
|
|
154
|
-
depth += 1;
|
|
155
|
-
i += 1;
|
|
156
|
-
}
|
|
157
|
-
else if (ch === ')') {
|
|
158
|
-
depth -= 1;
|
|
159
|
-
i += 1;
|
|
160
|
-
}
|
|
161
|
-
else if (WORD_RE.test(ch)) {
|
|
162
|
-
let j = i + 1;
|
|
163
|
-
while (j < sql.length && /[A-Za-z0-9_]/.test(sql[j]))
|
|
164
|
-
j += 1;
|
|
165
|
-
const word = sql.slice(i, j).toUpperCase();
|
|
166
|
-
if (depth === 0) {
|
|
167
|
-
if (!sawWith && word === 'WITH')
|
|
168
|
-
sawWith = true;
|
|
169
|
-
else if (sawWith && MAIN_VERBS.has(word))
|
|
170
|
-
return word;
|
|
171
|
-
}
|
|
172
|
-
i = j;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
i += 1;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return undefined;
|
|
179
|
-
}
|
|
180
118
|
const IDENT = '[A-Za-z_][A-Za-z0-9_]*';
|
|
181
119
|
const EXPLICIT_ALIAS_RE = new RegExp(`^([\\s\\S]*?)\\s+AS\\s+(${IDENT})$`, 'i');
|
|
182
120
|
const TRAILING_IDENT_RE = new RegExp(`^([\\s\\S]*?)\\s+(${IDENT})$`);
|
package/dist/migration-lock.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { MigrationInput } from './generate.js';
|
|
2
|
-
import type { IrColumnType } from './ir.js';
|
|
2
|
+
import type { IrColumnType, IrSchemaVersion } from './ir.js';
|
|
3
|
+
import { type ManifestSchemaVersion } from './manifest.js';
|
|
3
4
|
export declare const MIGRATION_LOCK_FILENAME = "syncular.migrations.lock.json";
|
|
4
|
-
export declare const MIGRATION_LOCK_FORMAT_VERSION =
|
|
5
|
-
export declare const LEGACY_MIGRATION_LOCK_FORMAT_VERSION = 1;
|
|
5
|
+
export declare const MIGRATION_LOCK_FORMAT_VERSION = 3;
|
|
6
6
|
export interface MigrationLockColumn {
|
|
7
7
|
readonly name: string;
|
|
8
8
|
readonly type: IrColumnType;
|
|
@@ -17,37 +17,46 @@ export interface MigrationLockTable {
|
|
|
17
17
|
export interface MigrationLockEntry {
|
|
18
18
|
readonly name: string;
|
|
19
19
|
readonly sha256: string;
|
|
20
|
+
/** The schema version this migration belongs to (§1.5, §7.4). */
|
|
21
|
+
readonly schemaVersion: number;
|
|
20
22
|
}
|
|
21
|
-
export interface
|
|
22
|
-
readonly tables: readonly MigrationLockTable[];
|
|
23
|
-
}
|
|
24
|
-
export interface LegacyMigrationLock {
|
|
25
|
-
readonly formatVersion: typeof LEGACY_MIGRATION_LOCK_FORMAT_VERSION;
|
|
26
|
-
readonly migrations: readonly LegacyMigrationLockEntry[];
|
|
27
|
-
}
|
|
28
|
-
export interface MigrationLockV2 {
|
|
23
|
+
export interface MigrationLock {
|
|
29
24
|
readonly formatVersion: typeof MIGRATION_LOCK_FORMAT_VERSION;
|
|
30
25
|
readonly migrations: readonly MigrationLockEntry[];
|
|
31
26
|
readonly head: {
|
|
32
27
|
readonly tables: readonly MigrationLockTable[];
|
|
33
28
|
};
|
|
34
29
|
}
|
|
35
|
-
|
|
30
|
+
/** A format-1 or format-2 lock: names and checksums without schema versions,
|
|
31
|
+
* read only to upgrade it to the current format. */
|
|
32
|
+
export interface LegacyMigrationLock {
|
|
33
|
+
readonly formatVersion: 1 | 2;
|
|
34
|
+
readonly migrations: readonly Omit<MigrationLockEntry, 'schemaVersion'>[];
|
|
35
|
+
}
|
|
36
36
|
/** The names of every migration a committed lock has made immutable. */
|
|
37
|
-
export declare function lockedMigrationNames(lock: MigrationLock): ReadonlySet<string>;
|
|
38
|
-
/**
|
|
39
|
-
|
|
37
|
+
export declare function lockedMigrationNames(lock: MigrationLock | LegacyMigrationLock): ReadonlySet<string>;
|
|
38
|
+
/** Resolve the manifest's `schemaVersions` (each naming the last migration
|
|
39
|
+
* it includes) into the migrations every version adds (§1.5). */
|
|
40
|
+
export declare function buildSchemaVersions(declared: readonly ManifestSchemaVersion[], migrations: readonly MigrationInput[]): IrSchemaVersion[];
|
|
41
|
+
/** Build a deterministic lock document for a complete migration sequence;
|
|
42
|
+
* `declared` is the manifest's `schemaVersions`. */
|
|
43
|
+
export declare function buildMigrationLock(migrations: readonly MigrationInput[], declared: readonly ManifestSchemaVersion[]): MigrationLock;
|
|
40
44
|
/** Fixed-order, byte-deterministic serialization for clean code review. */
|
|
41
45
|
export declare function serializeMigrationLock(lock: MigrationLock): string;
|
|
42
46
|
/** Parse and structurally validate a committed lock document. */
|
|
43
|
-
export declare function parseMigrationLock(source: string): MigrationLock;
|
|
47
|
+
export declare function parseMigrationLock(source: string): MigrationLock | LegacyMigrationLock;
|
|
44
48
|
/** Read the version-controlled baseline without exposing its absolute path. */
|
|
45
|
-
export declare function readMigrationLock(manifestDir: string): MigrationLock;
|
|
49
|
+
export declare function readMigrationLock(manifestDir: string): MigrationLock | LegacyMigrationLock;
|
|
50
|
+
/**
|
|
51
|
+
* Validate a committed lock against the migration inputs and their schema
|
|
52
|
+
* versions, and build its exact next document. A locked migration keeps its
|
|
53
|
+
* schema version; an appended migration belongs to a version above the
|
|
54
|
+
* highest locked one, because a deployed version never changes (§7.4).
|
|
55
|
+
*/
|
|
56
|
+
export declare function updateMigrationLock(locked: MigrationLock | LegacyMigrationLock, migrations: readonly MigrationInput[], declared: readonly ManifestSchemaVersion[]): MigrationLock;
|
|
46
57
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
58
|
+
* Convert a format-1 or format-2 lock to the current format: validate its
|
|
59
|
+
* locked prefix and record the schema version the manifest's `declared`
|
|
60
|
+
* versions assign each locked migration. Appended migrations stay unlocked for generation.
|
|
50
61
|
*/
|
|
51
|
-
export declare function
|
|
52
|
-
/** Validate without changing the committed lock format. */
|
|
53
|
-
export declare function validateMigrationLock(locked: MigrationLock, migrations: readonly MigrationInput[]): void;
|
|
62
|
+
export declare function upgradeMigrationLock(locked: LegacyMigrationLock, migrations: readonly MigrationInput[], declared: readonly ManifestSchemaVersion[]): MigrationLock;
|
package/dist/migration-lock.js
CHANGED
|
@@ -2,20 +2,21 @@
|
|
|
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.js';
|
|
16
|
+
import { MANIFEST_FILENAME } from './manifest.js';
|
|
15
17
|
import { applyMigrationSql } from './sql.js';
|
|
16
18
|
export const MIGRATION_LOCK_FILENAME = 'syncular.migrations.lock.json';
|
|
17
|
-
export const MIGRATION_LOCK_FORMAT_VERSION =
|
|
18
|
-
export const LEGACY_MIGRATION_LOCK_FORMAT_VERSION = 1;
|
|
19
|
+
export const MIGRATION_LOCK_FORMAT_VERSION = 3;
|
|
19
20
|
function normalizedSql(sql) {
|
|
20
21
|
return sql.replace(/\r\n?/g, '\n');
|
|
21
22
|
}
|
|
@@ -44,46 +45,60 @@ function snapshotTables(tables) {
|
|
|
44
45
|
export function lockedMigrationNames(lock) {
|
|
45
46
|
return new Set(lock.migrations.map((migration) => migration.name));
|
|
46
47
|
}
|
|
48
|
+
/** Resolve the manifest's `schemaVersions` (each naming the last migration
|
|
49
|
+
* it includes) into the migrations every version adds (§1.5). */
|
|
50
|
+
export function buildSchemaVersions(declared, migrations) {
|
|
51
|
+
const names = migrations.map((m) => m.name);
|
|
52
|
+
let cursor = 0;
|
|
53
|
+
const out = declared.map((entry) => {
|
|
54
|
+
const index = names.indexOf(entry.through, cursor);
|
|
55
|
+
if (index === -1) {
|
|
56
|
+
throw new TypegenError(MANIFEST_FILENAME, `schemaVersions: version ${entry.version} names migration ${JSON.stringify(entry.through)}, which does not exist after ${JSON.stringify(names[cursor - 1] ?? '(start)')} (migrations: ${names.join(', ')})`);
|
|
57
|
+
}
|
|
58
|
+
const covered = names.slice(cursor, index + 1);
|
|
59
|
+
cursor = index + 1;
|
|
60
|
+
return { version: entry.version, migrations: covered };
|
|
61
|
+
});
|
|
62
|
+
if (cursor !== names.length) {
|
|
63
|
+
throw new TypegenError(MANIFEST_FILENAME, `schemaVersions: migrations ${names.slice(cursor).join(', ')} are not covered by any schema version — the last entry's "through" must be the last migration`);
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
47
67
|
/** Replay a complete migration sequence with ephemeral diagnostic snapshots.
|
|
48
68
|
* Migrations named in `lockedNames` replay as deployed history; rules that
|
|
49
69
|
* only appended migrations must satisfy are skipped for them. */
|
|
50
70
|
function buildMigrationHistory(migrations, lockedNames) {
|
|
51
71
|
const tables = new Map();
|
|
52
72
|
const droppedTables = new Set();
|
|
53
|
-
|
|
54
|
-
for (const migration of migrations) {
|
|
73
|
+
return migrations.map((migration) => {
|
|
55
74
|
applyMigrationSql(tables, migration.sql, `${migration.name}/up.sql`, droppedTables, { lockedHistory: lockedNames?.has(migration.name) === true });
|
|
56
|
-
|
|
75
|
+
return {
|
|
57
76
|
name: migration.name,
|
|
58
77
|
sha256: checksum(migration.sql),
|
|
59
78
|
tables: snapshotTables(tables),
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return { migrations: entries };
|
|
79
|
+
};
|
|
80
|
+
});
|
|
63
81
|
}
|
|
64
|
-
function lockFromHistory(history,
|
|
65
|
-
|
|
82
|
+
function lockFromHistory(history, schemaVersions) {
|
|
83
|
+
const head = history.at(-1);
|
|
84
|
+
if (head === undefined)
|
|
66
85
|
failLock('cannot lock an empty migration history');
|
|
67
|
-
}
|
|
68
|
-
if (formatVersion === LEGACY_MIGRATION_LOCK_FORMAT_VERSION) {
|
|
69
|
-
return {
|
|
70
|
-
formatVersion: LEGACY_MIGRATION_LOCK_FORMAT_VERSION,
|
|
71
|
-
migrations: history.migrations,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
const head = history.migrations.at(-1);
|
|
75
86
|
return {
|
|
76
87
|
formatVersion: MIGRATION_LOCK_FORMAT_VERSION,
|
|
77
|
-
migrations: history.
|
|
78
|
-
name
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
migrations: history.map(({ name, sha256 }) => {
|
|
89
|
+
const version = schemaVersions.find((v) => v.migrations.includes(name));
|
|
90
|
+
if (version === undefined) {
|
|
91
|
+
throw new Error(`unreachable: migration ${name} has no schema version`);
|
|
92
|
+
}
|
|
93
|
+
return { name, sha256, schemaVersion: version.version };
|
|
94
|
+
}),
|
|
81
95
|
head: { tables: head.tables },
|
|
82
96
|
};
|
|
83
97
|
}
|
|
84
|
-
/** Build a deterministic lock document for a complete migration sequence
|
|
85
|
-
|
|
86
|
-
|
|
98
|
+
/** Build a deterministic lock document for a complete migration sequence;
|
|
99
|
+
* `declared` is the manifest's `schemaVersions`. */
|
|
100
|
+
export function buildMigrationLock(migrations, declared) {
|
|
101
|
+
return lockFromHistory(buildMigrationHistory(migrations), buildSchemaVersions(declared, migrations));
|
|
87
102
|
}
|
|
88
103
|
/** Fixed-order, byte-deterministic serialization for clean code review. */
|
|
89
104
|
export function serializeMigrationLock(lock) {
|
|
@@ -151,27 +166,14 @@ function parseEntry(value, index) {
|
|
|
151
166
|
const context = `migrations[${index}]`;
|
|
152
167
|
if (!isRecord(value))
|
|
153
168
|
failLock(`${context} must be an object`);
|
|
154
|
-
const { name, sha256 } = value;
|
|
169
|
+
const { name, sha256, schemaVersion } = value;
|
|
155
170
|
if (typeof name !== 'string' || name.length === 0) {
|
|
156
171
|
failLock(`${context}.name must be a non-empty string`);
|
|
157
172
|
}
|
|
158
173
|
if (typeof sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(sha256)) {
|
|
159
174
|
failLock(`${context}.sha256 must be a lowercase SHA-256 digest`);
|
|
160
175
|
}
|
|
161
|
-
return { name, sha256 };
|
|
162
|
-
}
|
|
163
|
-
function parseLegacyEntry(value, index) {
|
|
164
|
-
const context = `migrations[${index}]`;
|
|
165
|
-
const entry = parseEntry(value, index);
|
|
166
|
-
if (!isRecord(value))
|
|
167
|
-
failLock(`${context} must be an object`);
|
|
168
|
-
const { tables } = value;
|
|
169
|
-
if (!Array.isArray(tables))
|
|
170
|
-
failLock(`${context}.tables must be an array`);
|
|
171
|
-
return {
|
|
172
|
-
...entry,
|
|
173
|
-
tables: tables.map((table, tableIndex) => parseTable(table, `${context}.tables[${tableIndex}]`)),
|
|
174
|
-
};
|
|
176
|
+
return { name, sha256, schemaVersion };
|
|
175
177
|
}
|
|
176
178
|
function parseHead(value) {
|
|
177
179
|
if (!isRecord(value))
|
|
@@ -182,15 +184,6 @@ function parseHead(value) {
|
|
|
182
184
|
tables: value.tables.map((table, index) => parseTable(table, `head.tables[${index}]`)),
|
|
183
185
|
};
|
|
184
186
|
}
|
|
185
|
-
function assertUniqueMigrationNames(migrations) {
|
|
186
|
-
const names = new Set();
|
|
187
|
-
for (const migration of migrations) {
|
|
188
|
-
if (names.has(migration.name)) {
|
|
189
|
-
failLock(`migration ${JSON.stringify(migration.name)} appears twice`);
|
|
190
|
-
}
|
|
191
|
-
names.add(migration.name);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
187
|
/** Parse and structurally validate a committed lock document. */
|
|
195
188
|
export function parseMigrationLock(source) {
|
|
196
189
|
let raw;
|
|
@@ -202,23 +195,39 @@ export function parseMigrationLock(source) {
|
|
|
202
195
|
}
|
|
203
196
|
if (!isRecord(raw))
|
|
204
197
|
failLock('root must be an object');
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
198
|
+
const { formatVersion } = raw;
|
|
199
|
+
if (formatVersion !== 1 &&
|
|
200
|
+
formatVersion !== 2 &&
|
|
201
|
+
formatVersion !== MIGRATION_LOCK_FORMAT_VERSION) {
|
|
202
|
+
failLock(`formatVersion must be 1, 2, or ${MIGRATION_LOCK_FORMAT_VERSION}, got ${JSON.stringify(formatVersion)}`);
|
|
208
203
|
}
|
|
209
204
|
if (!Array.isArray(raw.migrations) || raw.migrations.length === 0) {
|
|
210
205
|
failLock('migrations must be a non-empty array');
|
|
211
206
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
207
|
+
const entries = raw.migrations.map(parseEntry);
|
|
208
|
+
const names = new Set();
|
|
209
|
+
for (const { name } of entries) {
|
|
210
|
+
if (names.has(name)) {
|
|
211
|
+
failLock(`migration ${JSON.stringify(name)} appears twice`);
|
|
212
|
+
}
|
|
213
|
+
names.add(name);
|
|
214
|
+
}
|
|
215
|
+
if (formatVersion !== MIGRATION_LOCK_FORMAT_VERSION) {
|
|
215
216
|
return {
|
|
216
|
-
formatVersion
|
|
217
|
-
migrations,
|
|
217
|
+
formatVersion,
|
|
218
|
+
migrations: entries.map(({ name, sha256 }) => ({ name, sha256 })),
|
|
218
219
|
};
|
|
219
220
|
}
|
|
220
|
-
|
|
221
|
-
|
|
221
|
+
let previous = 1;
|
|
222
|
+
const migrations = entries.map(({ name, sha256, schemaVersion }, index) => {
|
|
223
|
+
if (typeof schemaVersion !== 'number' ||
|
|
224
|
+
!Number.isInteger(schemaVersion) ||
|
|
225
|
+
schemaVersion < previous) {
|
|
226
|
+
failLock(`migrations[${index}].schemaVersion must be an integer, at least 1 and at least the previous entry's`);
|
|
227
|
+
}
|
|
228
|
+
previous = schemaVersion;
|
|
229
|
+
return { name, sha256, schemaVersion };
|
|
230
|
+
});
|
|
222
231
|
return {
|
|
223
232
|
formatVersion: MIGRATION_LOCK_FORMAT_VERSION,
|
|
224
233
|
migrations,
|
|
@@ -274,10 +283,10 @@ function describeColumnDifference(tableName, locked, current) {
|
|
|
274
283
|
return undefined;
|
|
275
284
|
}
|
|
276
285
|
function firstSchemaDifference(locked, current) {
|
|
277
|
-
const length = Math.max(locked.
|
|
286
|
+
const length = Math.max(locked.length, current.length);
|
|
278
287
|
for (let index = 0; index < length; index++) {
|
|
279
|
-
const before = locked
|
|
280
|
-
const after = current
|
|
288
|
+
const before = locked[index];
|
|
289
|
+
const after = current[index];
|
|
281
290
|
if (before === undefined && after !== undefined) {
|
|
282
291
|
return `table ${JSON.stringify(after.name)} was added`;
|
|
283
292
|
}
|
|
@@ -304,9 +313,8 @@ const REPAIR_HINT = 'deployed migrations are immutable; restore the locked migra
|
|
|
304
313
|
* New migrations may be appended; existing names and bytes may never change.
|
|
305
314
|
*/
|
|
306
315
|
function validateMigrationHistory(locked, current) {
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
const after = current.migrations[index];
|
|
316
|
+
locked.migrations.forEach((before, index) => {
|
|
317
|
+
const after = current[index];
|
|
310
318
|
if (after === undefined) {
|
|
311
319
|
failLock(`history drift at migration ${JSON.stringify(before.name)}: the locked migration is missing; ${REPAIR_HINT}`);
|
|
312
320
|
}
|
|
@@ -314,33 +322,48 @@ function validateMigrationHistory(locked, current) {
|
|
|
314
322
|
failLock(`history drift at position ${index + 1}: locked ${JSON.stringify(before.name)} but found ${JSON.stringify(after.name)}; migrations cannot be removed, renamed, or reordered; ${REPAIR_HINT}`);
|
|
315
323
|
}
|
|
316
324
|
if (before.sha256 !== after.sha256) {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
// Compare it with the current schema at that same boundary, excluding
|
|
325
|
-
// valid newly appended migrations from the diagnostic.
|
|
326
|
-
const diagnosticAfter = locked.formatVersion === LEGACY_MIGRATION_LOCK_FORMAT_VERSION
|
|
327
|
-
? after
|
|
328
|
-
: (current.migrations[locked.migrations.length - 1] ?? after);
|
|
329
|
-
failLock(`history drift in migration ${JSON.stringify(before.name)}: checksum changed; first schema difference: ${firstSchemaDifference(diagnosticBefore, diagnosticAfter)}; ${REPAIR_HINT}`);
|
|
325
|
+
// The head snapshot is the schema after the complete locked prefix:
|
|
326
|
+
// compare it with the current schema at that same boundary, excluding
|
|
327
|
+
// valid newly appended migrations. A legacy lock is read without one.
|
|
328
|
+
const difference = locked.formatVersion === MIGRATION_LOCK_FORMAT_VERSION
|
|
329
|
+
? `; first schema difference: ${firstSchemaDifference(locked.head.tables, (current[locked.migrations.length - 1] ?? after).tables)}`
|
|
330
|
+
: '';
|
|
331
|
+
failLock(`history drift in migration ${JSON.stringify(before.name)}: checksum changed${difference}; ${REPAIR_HINT}`);
|
|
330
332
|
}
|
|
331
|
-
}
|
|
333
|
+
});
|
|
332
334
|
}
|
|
333
335
|
/**
|
|
334
|
-
* Validate a committed lock against migration inputs and
|
|
335
|
-
*
|
|
336
|
-
* version
|
|
336
|
+
* Validate a committed lock against the migration inputs and their schema
|
|
337
|
+
* versions, and build its exact next document. A locked migration keeps its
|
|
338
|
+
* schema version; an appended migration belongs to a version above the
|
|
339
|
+
* highest locked one, because a deployed version never changes (§7.4).
|
|
337
340
|
*/
|
|
338
|
-
export function updateMigrationLock(locked, migrations) {
|
|
341
|
+
export function updateMigrationLock(locked, migrations, declared) {
|
|
342
|
+
if (locked.formatVersion !== MIGRATION_LOCK_FORMAT_VERSION) {
|
|
343
|
+
throw new TypegenError(MIGRATION_LOCK_FILENAME, 'the lock predates per-migration schema versions; run `syncular migrations upgrade-lock` once and commit the lock', 'typegen.migration_lock_outdated', { formatVersion: locked.formatVersion });
|
|
344
|
+
}
|
|
339
345
|
const current = buildMigrationHistory(migrations, lockedMigrationNames(locked));
|
|
340
346
|
validateMigrationHistory(locked, current);
|
|
341
|
-
|
|
347
|
+
const next = lockFromHistory(current, buildSchemaVersions(declared, migrations));
|
|
348
|
+
const highestLockedVersion = locked.migrations.at(-1)?.schemaVersion ?? 0;
|
|
349
|
+
next.migrations.forEach(({ name, schemaVersion }, index) => {
|
|
350
|
+
const lockedVersion = locked.migrations[index]?.schemaVersion;
|
|
351
|
+
if (lockedVersion !== undefined && lockedVersion !== schemaVersion) {
|
|
352
|
+
throw new TypegenError(MIGRATION_LOCK_FILENAME, 'a locked migration moved to another schema version; a locked version keeps its migration set (§7.4)', 'typegen.migration_version_changed', { migration: name, lockedVersion, schemaVersion });
|
|
353
|
+
}
|
|
354
|
+
if (lockedVersion === undefined && schemaVersion <= highestLockedVersion) {
|
|
355
|
+
throw new TypegenError(MIGRATION_LOCK_FILENAME, 'an appended migration joins a locked schema version; declare a schema version above the highest locked one (§7.4)', 'typegen.schema_version_locked', { migration: name, schemaVersion, highestLockedVersion });
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
return next;
|
|
342
359
|
}
|
|
343
|
-
/**
|
|
344
|
-
|
|
345
|
-
|
|
360
|
+
/**
|
|
361
|
+
* Convert a format-1 or format-2 lock to the current format: validate its
|
|
362
|
+
* locked prefix and record the schema version the manifest's `declared`
|
|
363
|
+
* versions assign each locked migration. Appended migrations stay unlocked for generation.
|
|
364
|
+
*/
|
|
365
|
+
export function upgradeMigrationLock(locked, migrations, declared) {
|
|
366
|
+
const current = buildMigrationHistory(migrations, lockedMigrationNames(locked));
|
|
367
|
+
validateMigrationHistory(locked, current);
|
|
368
|
+
return lockFromHistory(current.slice(0, locked.migrations.length), buildSchemaVersions(declared, migrations));
|
|
346
369
|
}
|
package/dist/naming.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
import type { IrTable } from './ir.js';
|
|
1
2
|
/** Manifest `"naming"`: camelCase emission (default) or SQL-truth names. */
|
|
2
3
|
export type NamingMode = 'camel' | 'preserve';
|
|
3
4
|
/** The pinned §12 snake→camel conversion. Non-identifier names (computed
|
|
4
5
|
* result columns like `count(*)`) are returned unchanged. */
|
|
5
6
|
export declare function snakeToCamel(name: string): string;
|
|
7
|
+
/** Generated type name of a table or subscription: `_`/`-` separated parts,
|
|
8
|
+
* each with its first character upper-cased (`project_tasks` →
|
|
9
|
+
* `ProjectTasks`). */
|
|
10
|
+
export declare function pascalCase(name: string): string;
|
|
11
|
+
/** `value` as a string literal of a schema emitter's language. Swift and
|
|
12
|
+
* Kotlin delimit with `"`, TS and Dart with `'`; Kotlin and Dart also escape
|
|
13
|
+
* the `$` that would start an interpolation. */
|
|
14
|
+
export declare function stringLiteral(value: string, target: Exclude<NamingTarget, 'rust'>): string;
|
|
6
15
|
/** The emitter targets whose keyword sets we police. `ts` is included for
|
|
7
16
|
* completeness (its emitters can quote any property, so its list is the
|
|
8
17
|
* small set that breaks generated FUNCTION/const identifiers). */
|
|
@@ -30,3 +39,19 @@ export interface NameMapping {
|
|
|
30
39
|
* run generates — keyword hazards are only real on targets that exist.
|
|
31
40
|
*/
|
|
32
41
|
export declare function buildNamingMap(sqlNames: readonly string[], mode: NamingMode, context: string, scope: string, targets: readonly NamingTarget[]): NameMapping[];
|
|
42
|
+
/**
|
|
43
|
+
* The schema identifier rules for every configured target in both naming
|
|
44
|
+
* modes. Table names, and column names within a table, stay distinct under
|
|
45
|
+
* SQLite's case-insensitive comparison; generated type names
|
|
46
|
+
* (`pascalCase(table)`) stay distinct; each target's emitted column names
|
|
47
|
+
* stay distinct and clear of its reserved words. Swift, Kotlin, and Dart
|
|
48
|
+
* always emit camelCase fields; TS and Rust follow the naming mode.
|
|
49
|
+
*/
|
|
50
|
+
export declare function validateSchemaIdentifiers(tables: readonly IrTable[], mode: NamingMode, targets: readonly NamingTarget[], context: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* Refuse a query name, result column, or query input that some configured
|
|
53
|
+
* target cannot emit: TS uses the name as is, Rust its snake_case form, and
|
|
54
|
+
* Swift, Kotlin, and Dart its camelCase form. Each must be an identifier
|
|
55
|
+
* clear of the target's reserved words; Dart also refuses a leading `_`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function validateQueryIdentifier(name: string, targets: readonly NamingTarget[], context: string, scope: string): void;
|