viborm 0.1.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 +529 -0
- package/dist/adapters.d.mts +2 -0
- package/dist/adapters.mjs +1 -0
- package/dist/bun-sql.d.mts +3 -0
- package/dist/bun-sql.mjs +2 -0
- package/dist/bun-sql.mjs.map +1 -0
- package/dist/bun-sqlite.d.mts +3 -0
- package/dist/bun-sqlite.mjs +2 -0
- package/dist/bun-sqlite.mjs.map +1 -0
- package/dist/cache/cloudflare-kv.d.mts +3 -0
- package/dist/cache/cloudflare-kv.mjs +2 -0
- package/dist/cache/cloudflare-kv.mjs.map +1 -0
- package/dist/cache/memory.d.mts +3 -0
- package/dist/cache/memory.mjs +2 -0
- package/dist/cache/memory.mjs.map +1 -0
- package/dist/cache.d.mts +4 -0
- package/dist/cache.mjs +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +41 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/client-CgdtiDGV.mjs +2 -0
- package/dist/client-CgdtiDGV.mjs.map +1 -0
- package/dist/client.d.mts +3 -0
- package/dist/client.mjs +1 -0
- package/dist/cloudflare-kv-DauXhtnT.d.mts +1110 -0
- package/dist/d1-http.d.mts +3 -0
- package/dist/d1-http.mjs +2 -0
- package/dist/d1-http.mjs.map +1 -0
- package/dist/d1.d.mts +3 -0
- package/dist/d1.mjs +2 -0
- package/dist/d1.mjs.map +1 -0
- package/dist/driver-B_vBzQ6A.mjs +2 -0
- package/dist/driver-B_vBzQ6A.mjs.map +1 -0
- package/dist/driver-Cj1EbciY.mjs +2 -0
- package/dist/driver-Cj1EbciY.mjs.map +1 -0
- package/dist/driver.d.mts +2 -0
- package/dist/driver.mjs +1 -0
- package/dist/errors-DvcqIdEo.mjs +2 -0
- package/dist/errors-DvcqIdEo.mjs.map +1 -0
- package/dist/helper-cDCDjRAD.d.mts +3020 -0
- package/dist/helpers-WNr9B-2U.mjs +2 -0
- package/dist/helpers-WNr9B-2U.mjs.map +1 -0
- package/dist/hydration-CFnwc7j6.mjs +2 -0
- package/dist/hydration-CFnwc7j6.mjs.map +1 -0
- package/dist/index-3sdVKfeG.d.mts +1447 -0
- package/dist/index-BGmpst1h.d.mts +2158 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/instrumentation.d.mts +2 -0
- package/dist/instrumentation.mjs +1 -0
- package/dist/libsql.d.mts +3 -0
- package/dist/libsql.mjs +2 -0
- package/dist/libsql.mjs.map +1 -0
- package/dist/migrations.d.mts +1588 -0
- package/dist/migrations.mjs +11 -0
- package/dist/migrations.mjs.map +1 -0
- package/dist/mysql-adapter-0catKeCI.mjs +6 -0
- package/dist/mysql-adapter-0catKeCI.mjs.map +1 -0
- package/dist/mysql-utils-BPw9qhac.mjs +2 -0
- package/dist/mysql-utils-BPw9qhac.mjs.map +1 -0
- package/dist/mysql2.d.mts +3 -0
- package/dist/mysql2.mjs +2 -0
- package/dist/mysql2.mjs.map +1 -0
- package/dist/native-types-BwICkVjz.mjs +2 -0
- package/dist/native-types-BwICkVjz.mjs.map +1 -0
- package/dist/neon-http.d.mts +3 -0
- package/dist/neon-http.mjs +2 -0
- package/dist/neon-http.mjs.map +1 -0
- package/dist/pending-operation-CkzYTpi4.mjs +2 -0
- package/dist/pending-operation-CkzYTpi4.mjs.map +1 -0
- package/dist/pg.d.mts +3 -0
- package/dist/pg.mjs +2 -0
- package/dist/pg.mjs.map +1 -0
- package/dist/pglite.d.mts +3 -0
- package/dist/pglite.mjs +2 -0
- package/dist/pglite.mjs.map +1 -0
- package/dist/planetscale.d.mts +3 -0
- package/dist/planetscale.mjs +2 -0
- package/dist/planetscale.mjs.map +1 -0
- package/dist/postgres-adapter-Blzqj1bl.mjs +6 -0
- package/dist/postgres-adapter-Blzqj1bl.mjs.map +1 -0
- package/dist/postgres.d.mts +3 -0
- package/dist/postgres.mjs +2 -0
- package/dist/postgres.mjs.map +1 -0
- package/dist/push-qZreXYkb.mjs +252 -0
- package/dist/push-qZreXYkb.mjs.map +1 -0
- package/dist/result-parsing-FNQbuHvC.mjs +2 -0
- package/dist/result-parsing-FNQbuHvC.mjs.map +1 -0
- package/dist/schema-DeAJZocc.mjs +2 -0
- package/dist/schema-DeAJZocc.mjs.map +1 -0
- package/dist/schema.d.mts +293 -0
- package/dist/schema.mjs +2 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/spans-CJt5UgbJ.mjs +2 -0
- package/dist/spans-CJt5UgbJ.mjs.map +1 -0
- package/dist/sqlite-adapter-5Em_-oGU.mjs +6 -0
- package/dist/sqlite-adapter-5Em_-oGU.mjs.map +1 -0
- package/dist/sqlite-utils-BW7elKTz.mjs +2 -0
- package/dist/sqlite-utils-BW7elKTz.mjs.map +1 -0
- package/dist/sqlite3.d.mts +3 -0
- package/dist/sqlite3.mjs +2 -0
- package/dist/sqlite3.mjs.map +1 -0
- package/dist/tracer-CGzIAsj8.mjs +2 -0
- package/dist/tracer-CGzIAsj8.mjs.map +1 -0
- package/dist/validation-RtU-wu4N.mjs +2 -0
- package/dist/validation-RtU-wu4N.mjs.map +1 -0
- package/dist/validation.d.mts +2 -0
- package/dist/validation.mjs +1 -0
- package/dist/validator-DpJsJcd7.mjs +3 -0
- package/dist/validator-DpJsJcd7.mjs.map +1 -0
- package/package.json +278 -0
- package/readme/README.md +69 -0
|
@@ -0,0 +1,1588 @@
|
|
|
1
|
+
import { A as Dialect$1, Ct as MigrationError, Mt as isMigrationError, w as AnyDriver } from "./index-BGmpst1h.mjs";
|
|
2
|
+
import { b as Field, g as AnyModel, z as FieldState } from "./helper-cDCDjRAD.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/migrations/storage/driver.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Abstract base class for migration storage drivers.
|
|
8
|
+
* Provides high-level operations for managing migration files.
|
|
9
|
+
*
|
|
10
|
+
* Concrete drivers must implement:
|
|
11
|
+
* - `get(path)`: Read file content
|
|
12
|
+
* - `put(path, content)`: Write file content
|
|
13
|
+
* - `delete(path)`: Delete file
|
|
14
|
+
*/
|
|
15
|
+
declare abstract class MigrationStorageDriver {
|
|
16
|
+
readonly driverName: string;
|
|
17
|
+
constructor(driverName: string);
|
|
18
|
+
/**
|
|
19
|
+
* Read file content.
|
|
20
|
+
* @param path - Relative path (e.g., "0000_initial.sql", "meta/_journal.json")
|
|
21
|
+
* @returns File content as string, or null if file doesn't exist
|
|
22
|
+
*/
|
|
23
|
+
abstract get(path: string): Promise<string | null>;
|
|
24
|
+
/**
|
|
25
|
+
* Write file content. Creates directories as needed.
|
|
26
|
+
* @param path - Relative path
|
|
27
|
+
* @param content - Content to write
|
|
28
|
+
*/
|
|
29
|
+
abstract put(path: string, content: string): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Delete a file. No-op if file doesn't exist.
|
|
32
|
+
* @param path - Relative path
|
|
33
|
+
*/
|
|
34
|
+
abstract delete(path: string): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Read the migration journal.
|
|
37
|
+
* Returns null if no journal exists.
|
|
38
|
+
*/
|
|
39
|
+
readJournal(): Promise<MigrationJournal | null>;
|
|
40
|
+
/**
|
|
41
|
+
* Write the migration journal.
|
|
42
|
+
*/
|
|
43
|
+
writeJournal(journal: MigrationJournal): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Get or create a journal for the given dialect.
|
|
46
|
+
*/
|
|
47
|
+
getOrCreateJournal(dialect: Dialect): Promise<MigrationJournal>;
|
|
48
|
+
/**
|
|
49
|
+
* Read the schema snapshot.
|
|
50
|
+
* Returns null if no snapshot exists.
|
|
51
|
+
*/
|
|
52
|
+
readSnapshot(): Promise<SchemaSnapshot | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Write the schema snapshot.
|
|
55
|
+
*/
|
|
56
|
+
writeSnapshot(snapshot: SchemaSnapshot): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Get the snapshot or return an empty one.
|
|
59
|
+
*/
|
|
60
|
+
getSnapshotOrEmpty(): Promise<SchemaSnapshot>;
|
|
61
|
+
/**
|
|
62
|
+
* Read a migration file content.
|
|
63
|
+
*/
|
|
64
|
+
readMigration(entry: MigrationEntry): Promise<string | null>;
|
|
65
|
+
/**
|
|
66
|
+
* Write a migration file.
|
|
67
|
+
*/
|
|
68
|
+
writeMigration(entry: MigrationEntry, content: string): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Delete a migration file.
|
|
71
|
+
*/
|
|
72
|
+
deleteMigration(entry: MigrationEntry): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Check if a migration file exists.
|
|
75
|
+
*/
|
|
76
|
+
migrationExists(entry: MigrationEntry): Promise<boolean>;
|
|
77
|
+
/**
|
|
78
|
+
* Read a down migration file if it exists.
|
|
79
|
+
*/
|
|
80
|
+
readDownMigration(entry: MigrationEntry): Promise<string | null>;
|
|
81
|
+
/**
|
|
82
|
+
* Write a down migration file.
|
|
83
|
+
*/
|
|
84
|
+
writeDownMigration(entry: MigrationEntry, content: string): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Backup a migration file to the backup directory.
|
|
87
|
+
* Returns the backup path, or null if the file doesn't exist.
|
|
88
|
+
*/
|
|
89
|
+
backupMigration(entry: MigrationEntry): Promise<string | null>;
|
|
90
|
+
/**
|
|
91
|
+
* Archive a migration (backup and delete).
|
|
92
|
+
* Returns the backup path, or null if the file doesn't exist.
|
|
93
|
+
*/
|
|
94
|
+
archiveMigration(entry: MigrationEntry): Promise<string | null>;
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/migrations/types.d.ts
|
|
98
|
+
interface SchemaSnapshot {
|
|
99
|
+
tables: TableDef[];
|
|
100
|
+
enums?: EnumDef[] | undefined;
|
|
101
|
+
}
|
|
102
|
+
interface TableDef {
|
|
103
|
+
name: string;
|
|
104
|
+
columns: ColumnDef[];
|
|
105
|
+
primaryKey?: PrimaryKeyDef | undefined;
|
|
106
|
+
indexes: IndexDef[];
|
|
107
|
+
foreignKeys: ForeignKeyDef[];
|
|
108
|
+
uniqueConstraints: UniqueConstraintDef[];
|
|
109
|
+
}
|
|
110
|
+
interface ColumnDef {
|
|
111
|
+
name: string;
|
|
112
|
+
type: string;
|
|
113
|
+
nullable: boolean;
|
|
114
|
+
default?: string | undefined;
|
|
115
|
+
autoIncrement?: boolean | undefined;
|
|
116
|
+
}
|
|
117
|
+
interface PrimaryKeyDef {
|
|
118
|
+
columns: string[];
|
|
119
|
+
name?: string | undefined;
|
|
120
|
+
}
|
|
121
|
+
interface IndexDef {
|
|
122
|
+
name: string;
|
|
123
|
+
columns: string[];
|
|
124
|
+
unique: boolean;
|
|
125
|
+
type?: "btree" | "hash" | "gin" | "gist" | "fulltext" | "spatial" | undefined;
|
|
126
|
+
where?: string | undefined;
|
|
127
|
+
}
|
|
128
|
+
interface ForeignKeyDef {
|
|
129
|
+
name: string;
|
|
130
|
+
columns: string[];
|
|
131
|
+
referencedTable: string;
|
|
132
|
+
referencedColumns: string[];
|
|
133
|
+
onDelete?: ReferentialAction | undefined;
|
|
134
|
+
onUpdate?: ReferentialAction | undefined;
|
|
135
|
+
}
|
|
136
|
+
type ReferentialAction = "cascade" | "setNull" | "restrict" | "noAction" | "setDefault";
|
|
137
|
+
interface UniqueConstraintDef {
|
|
138
|
+
name: string;
|
|
139
|
+
columns: string[];
|
|
140
|
+
}
|
|
141
|
+
interface EnumDef {
|
|
142
|
+
name: string;
|
|
143
|
+
values: string[];
|
|
144
|
+
}
|
|
145
|
+
type DiffOperation = {
|
|
146
|
+
type: "createTable";
|
|
147
|
+
table: TableDef;
|
|
148
|
+
} | {
|
|
149
|
+
type: "dropTable";
|
|
150
|
+
tableName: string;
|
|
151
|
+
} | {
|
|
152
|
+
type: "renameTable";
|
|
153
|
+
from: string;
|
|
154
|
+
to: string;
|
|
155
|
+
} | {
|
|
156
|
+
type: "addColumn";
|
|
157
|
+
tableName: string;
|
|
158
|
+
column: ColumnDef;
|
|
159
|
+
} | {
|
|
160
|
+
type: "dropColumn";
|
|
161
|
+
tableName: string;
|
|
162
|
+
columnName: string;
|
|
163
|
+
} | {
|
|
164
|
+
type: "renameColumn";
|
|
165
|
+
tableName: string;
|
|
166
|
+
from: string;
|
|
167
|
+
to: string;
|
|
168
|
+
} | {
|
|
169
|
+
type: "alterColumn";
|
|
170
|
+
tableName: string;
|
|
171
|
+
columnName: string;
|
|
172
|
+
from: ColumnDef;
|
|
173
|
+
to: ColumnDef;
|
|
174
|
+
} | {
|
|
175
|
+
type: "createIndex";
|
|
176
|
+
tableName: string;
|
|
177
|
+
index: IndexDef;
|
|
178
|
+
} | {
|
|
179
|
+
type: "dropIndex";
|
|
180
|
+
tableName: string;
|
|
181
|
+
indexName: string;
|
|
182
|
+
} | {
|
|
183
|
+
type: "addForeignKey";
|
|
184
|
+
tableName: string;
|
|
185
|
+
fk: ForeignKeyDef;
|
|
186
|
+
} | {
|
|
187
|
+
type: "dropForeignKey";
|
|
188
|
+
tableName: string;
|
|
189
|
+
fkName: string;
|
|
190
|
+
} | {
|
|
191
|
+
type: "addUniqueConstraint";
|
|
192
|
+
tableName: string;
|
|
193
|
+
constraint: UniqueConstraintDef;
|
|
194
|
+
} | {
|
|
195
|
+
type: "dropUniqueConstraint";
|
|
196
|
+
tableName: string;
|
|
197
|
+
constraintName: string;
|
|
198
|
+
} | {
|
|
199
|
+
type: "addPrimaryKey";
|
|
200
|
+
tableName: string;
|
|
201
|
+
primaryKey: PrimaryKeyDef;
|
|
202
|
+
} | {
|
|
203
|
+
type: "dropPrimaryKey";
|
|
204
|
+
tableName: string;
|
|
205
|
+
constraintName: string;
|
|
206
|
+
} | {
|
|
207
|
+
type: "createEnum";
|
|
208
|
+
enumDef: EnumDef;
|
|
209
|
+
} | {
|
|
210
|
+
type: "dropEnum";
|
|
211
|
+
enumName: string;
|
|
212
|
+
/**
|
|
213
|
+
* Columns that depend on this enum.
|
|
214
|
+
* Required for SQLite to know which columns need CHECK constraint removal.
|
|
215
|
+
*/
|
|
216
|
+
dependentColumns?: Array<{
|
|
217
|
+
tableName: string;
|
|
218
|
+
columnName: string;
|
|
219
|
+
}>;
|
|
220
|
+
} | {
|
|
221
|
+
type: "alterEnum";
|
|
222
|
+
enumName: string;
|
|
223
|
+
addValues?: string[] | undefined;
|
|
224
|
+
removeValues?: string[] | undefined;
|
|
225
|
+
/**
|
|
226
|
+
* Full list of desired enum values (required when removing values)
|
|
227
|
+
* Used to recreate the enum type in PostgreSQL
|
|
228
|
+
*/
|
|
229
|
+
newValues?: string[] | undefined;
|
|
230
|
+
/**
|
|
231
|
+
* Columns that depend on this enum (required when removing values)
|
|
232
|
+
* Each entry is { tableName, columnName }
|
|
233
|
+
*/
|
|
234
|
+
dependentColumns?: Array<{
|
|
235
|
+
tableName: string;
|
|
236
|
+
columnName: string;
|
|
237
|
+
}> | undefined;
|
|
238
|
+
/**
|
|
239
|
+
* Global replacement values for removed enum values.
|
|
240
|
+
* Used as fallback when no column-specific mapping exists.
|
|
241
|
+
* Maps old value -> new value for data migration.
|
|
242
|
+
*
|
|
243
|
+
* Example: { "pending": "inactive" } - rows with "pending" become "inactive"
|
|
244
|
+
* Use null to set rows to NULL: { "pending": null }
|
|
245
|
+
*/
|
|
246
|
+
valueReplacements?: Record<string, string | null> | undefined;
|
|
247
|
+
/**
|
|
248
|
+
* Per-column replacement values for removed enum values.
|
|
249
|
+
* Allows different columns to have different mappings.
|
|
250
|
+
* Key is "tableName.columnName", value is the replacement map.
|
|
251
|
+
*
|
|
252
|
+
* Example: { "users.status": { "pending": "inactive" }, "orders.status": { "pending": "cancelled" } }
|
|
253
|
+
*/
|
|
254
|
+
columnValueReplacements?: Record<string, Record<string, string | null>> | undefined;
|
|
255
|
+
/**
|
|
256
|
+
* Default replacement for any removed values without explicit mapping.
|
|
257
|
+
* Useful when the enum field has a default value - use that here.
|
|
258
|
+
* Use null to set rows to NULL.
|
|
259
|
+
*
|
|
260
|
+
* Example: If your field has `.default("active")`, set defaultReplacement: "active"
|
|
261
|
+
*/
|
|
262
|
+
defaultReplacement?: string | null | undefined;
|
|
263
|
+
};
|
|
264
|
+
/** Detected when columns are added AND dropped in the same table */
|
|
265
|
+
type AmbiguousColumnChange = {
|
|
266
|
+
type: "ambiguousColumn";
|
|
267
|
+
tableName: string;
|
|
268
|
+
droppedColumn: ColumnDef;
|
|
269
|
+
addedColumn: ColumnDef;
|
|
270
|
+
};
|
|
271
|
+
/** Detected when tables are added AND dropped */
|
|
272
|
+
type AmbiguousTableChange = {
|
|
273
|
+
type: "ambiguousTable";
|
|
274
|
+
droppedTable: string;
|
|
275
|
+
addedTable: string;
|
|
276
|
+
};
|
|
277
|
+
type AmbiguousChange = AmbiguousColumnChange | AmbiguousTableChange;
|
|
278
|
+
/** User's resolution for an ambiguous change */
|
|
279
|
+
type ChangeResolution = {
|
|
280
|
+
type: "rename";
|
|
281
|
+
} | {
|
|
282
|
+
type: "addAndDrop";
|
|
283
|
+
};
|
|
284
|
+
interface DiffResult {
|
|
285
|
+
/** Operations that are unambiguous and ready to execute */
|
|
286
|
+
operations: DiffOperation[];
|
|
287
|
+
/** Changes that need user input to resolve */
|
|
288
|
+
ambiguousChanges: AmbiguousChange[];
|
|
289
|
+
}
|
|
290
|
+
/** Callback to resolve ambiguous changes (used by CLI or programmatic API) */
|
|
291
|
+
type Resolver = (changes: AmbiguousChange[]) => Promise<Map<AmbiguousChange, ChangeResolution>>;
|
|
292
|
+
/**
|
|
293
|
+
* Valid resolution results.
|
|
294
|
+
* - "proceed": Accept the change (for destructive)
|
|
295
|
+
* - "reject": Reject the change and abort
|
|
296
|
+
* - "rename": Treat as a rename (for ambiguous)
|
|
297
|
+
* - "addAndDrop": Treat as separate add + drop (for ambiguous)
|
|
298
|
+
* - "enumMapped": Enum values have been mapped (for enumValueRemoval)
|
|
299
|
+
*/
|
|
300
|
+
type ResolveResult = "proceed" | "reject" | "rename" | "addAndDrop" | "enumMapped";
|
|
301
|
+
/**
|
|
302
|
+
* Base properties shared by destructive and ambiguous changes.
|
|
303
|
+
*/
|
|
304
|
+
interface BaseResolveChange {
|
|
305
|
+
/** The specific operation type */
|
|
306
|
+
operation: "dropTable" | "dropColumn" | "alterColumn" | "renameTable" | "renameColumn";
|
|
307
|
+
/** The table involved */
|
|
308
|
+
table: string;
|
|
309
|
+
/** The column involved (if applicable) */
|
|
310
|
+
column?: string;
|
|
311
|
+
/** Human-readable description of the change */
|
|
312
|
+
description: string;
|
|
313
|
+
/** Reject this change and abort the operation */
|
|
314
|
+
reject(): ResolveResult;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* A destructive change that will cause data loss.
|
|
318
|
+
* Available methods: `proceed()`, `reject()`
|
|
319
|
+
*/
|
|
320
|
+
interface DestructiveResolveChange extends BaseResolveChange {
|
|
321
|
+
type: "destructive";
|
|
322
|
+
/** Accept the destructive change (causes data loss) */
|
|
323
|
+
proceed(): ResolveResult;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* An ambiguous change that could be a rename or add+drop.
|
|
327
|
+
* Available methods: `rename()`, `addAndDrop()`, `reject()`
|
|
328
|
+
*/
|
|
329
|
+
interface AmbiguousResolveChange extends BaseResolveChange {
|
|
330
|
+
type: "ambiguous";
|
|
331
|
+
/** The old name (for renames) */
|
|
332
|
+
oldName?: string;
|
|
333
|
+
/** The new name (for renames) */
|
|
334
|
+
newName?: string;
|
|
335
|
+
/** The old type (for alterColumn) */
|
|
336
|
+
oldType?: string;
|
|
337
|
+
/** The new type (for alterColumn) */
|
|
338
|
+
newType?: string;
|
|
339
|
+
/** Treat as a rename (preserves data) */
|
|
340
|
+
rename(): ResolveResult;
|
|
341
|
+
/** Treat as separate add + drop (causes data loss) */
|
|
342
|
+
addAndDrop(): ResolveResult;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* An enum value removal that needs replacement mappings for a specific column.
|
|
346
|
+
* Called once per column that uses the enum, allowing different mappings per column.
|
|
347
|
+
* Available methods: `mapValues()`, `useNull()`, `reject()`
|
|
348
|
+
*/
|
|
349
|
+
interface EnumValueRemovalChange {
|
|
350
|
+
type: "enumValueRemoval";
|
|
351
|
+
/** The enum name */
|
|
352
|
+
enumName: string;
|
|
353
|
+
/** The table containing the column */
|
|
354
|
+
tableName: string;
|
|
355
|
+
/** The column using this enum */
|
|
356
|
+
columnName: string;
|
|
357
|
+
/** Whether this column is nullable (if true, useNull() is safe) */
|
|
358
|
+
isNullable: boolean;
|
|
359
|
+
/** Values being removed */
|
|
360
|
+
removedValues: string[];
|
|
361
|
+
/** Available values to map to */
|
|
362
|
+
availableValues: string[];
|
|
363
|
+
/** Human-readable description */
|
|
364
|
+
description: string;
|
|
365
|
+
/**
|
|
366
|
+
* Map removed values to replacement values.
|
|
367
|
+
* @param replacements - Map of old value → new value (or null for NULL)
|
|
368
|
+
*/
|
|
369
|
+
mapValues(replacements: Record<string, string | null>): ResolveResult;
|
|
370
|
+
/**
|
|
371
|
+
* Set all removed values to NULL.
|
|
372
|
+
* This is a shorthand for mapValues where all values map to null.
|
|
373
|
+
* Note: Only safe if the column is nullable.
|
|
374
|
+
*/
|
|
375
|
+
useNull(): ResolveResult;
|
|
376
|
+
/** Reject this change and abort the operation */
|
|
377
|
+
reject(): ResolveResult;
|
|
378
|
+
/** Internal: stores the mapping result */
|
|
379
|
+
_mappings?: Record<string, string | null>;
|
|
380
|
+
/** Internal: flag indicating to use null as default */
|
|
381
|
+
_useNullDefault?: boolean;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* A change that requires user resolution.
|
|
385
|
+
* Use the type discriminant to determine which methods are available:
|
|
386
|
+
* - `destructive`: `proceed()`, `reject()`
|
|
387
|
+
* - `ambiguous`: `rename()`, `addAndDrop()`, `reject()`
|
|
388
|
+
* - `enumValueRemoval`: `mapValues()`, `reject()`
|
|
389
|
+
*/
|
|
390
|
+
type ResolveChange = DestructiveResolveChange | AmbiguousResolveChange | EnumValueRemovalChange;
|
|
391
|
+
/**
|
|
392
|
+
* Unified callback for resolving changes that need user input.
|
|
393
|
+
* Called once per change, allowing granular control over each decision.
|
|
394
|
+
*
|
|
395
|
+
* @param change - The change requiring resolution
|
|
396
|
+
* @returns The resolution decision (call a method on change to get this)
|
|
397
|
+
*
|
|
398
|
+
* @example
|
|
399
|
+
* ```ts
|
|
400
|
+
* resolve: async (change) => {
|
|
401
|
+
* console.log(change.description);
|
|
402
|
+
*
|
|
403
|
+
* if (change.type === "destructive") {
|
|
404
|
+
* return confirm("Accept data loss?") ? change.proceed() : change.reject();
|
|
405
|
+
* }
|
|
406
|
+
*
|
|
407
|
+
* if (change.type === "ambiguous") {
|
|
408
|
+
* return change.rename();
|
|
409
|
+
* }
|
|
410
|
+
*
|
|
411
|
+
* if (change.type === "enumValueRemoval") {
|
|
412
|
+
* // Map removed enum values to new values
|
|
413
|
+
* return change.mapValues({
|
|
414
|
+
* 'PENDING': 'INACTIVE',
|
|
415
|
+
* 'OLD_VALUE': null, // Set to NULL
|
|
416
|
+
* });
|
|
417
|
+
* }
|
|
418
|
+
* }
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
type ResolveCallback = (change: ResolveChange) => Promise<ResolveResult | undefined | void> | ResolveResult | undefined | void;
|
|
422
|
+
/**
|
|
423
|
+
* Create a destructive change object with resolution methods.
|
|
424
|
+
*/
|
|
425
|
+
declare function createDestructiveChange(props: {
|
|
426
|
+
operation: "dropTable" | "dropColumn" | "alterColumn";
|
|
427
|
+
table: string;
|
|
428
|
+
column?: string;
|
|
429
|
+
description: string;
|
|
430
|
+
}): DestructiveResolveChange;
|
|
431
|
+
/**
|
|
432
|
+
* Create an ambiguous change object with resolution methods.
|
|
433
|
+
*/
|
|
434
|
+
declare function createAmbiguousChange(props: {
|
|
435
|
+
operation: "renameTable" | "renameColumn";
|
|
436
|
+
table: string;
|
|
437
|
+
column?: string;
|
|
438
|
+
oldName?: string;
|
|
439
|
+
newName?: string;
|
|
440
|
+
oldType?: string;
|
|
441
|
+
newType?: string;
|
|
442
|
+
description: string;
|
|
443
|
+
}): AmbiguousResolveChange;
|
|
444
|
+
/**
|
|
445
|
+
* Create an enum value removal change object with resolution methods.
|
|
446
|
+
*/
|
|
447
|
+
declare function createEnumValueRemovalChange(props: {
|
|
448
|
+
enumName: string;
|
|
449
|
+
tableName: string;
|
|
450
|
+
columnName: string;
|
|
451
|
+
isNullable: boolean;
|
|
452
|
+
removedValues: string[];
|
|
453
|
+
availableValues: string[];
|
|
454
|
+
description: string;
|
|
455
|
+
}): EnumValueRemovalChange;
|
|
456
|
+
/** Information about an enum value removal that needs resolution */
|
|
457
|
+
interface EnumValueRemoval {
|
|
458
|
+
enumName: string;
|
|
459
|
+
removedValues: string[];
|
|
460
|
+
newValues: string[];
|
|
461
|
+
dependentColumns: Array<{
|
|
462
|
+
tableName: string;
|
|
463
|
+
columnName: string;
|
|
464
|
+
}>;
|
|
465
|
+
}
|
|
466
|
+
/** User's resolution for enum value removal */
|
|
467
|
+
interface EnumValueResolution {
|
|
468
|
+
/** Map specific old values to new values */
|
|
469
|
+
valueReplacements?: Record<string, string | null>;
|
|
470
|
+
/** Default replacement for any values not in valueReplacements */
|
|
471
|
+
defaultReplacement?: string | null;
|
|
472
|
+
}
|
|
473
|
+
/** Callback to resolve enum value removals (used by CLI or programmatic API) */
|
|
474
|
+
type EnumValueResolver = (removals: EnumValueRemoval[]) => Promise<Map<string, EnumValueResolution>>;
|
|
475
|
+
/**
|
|
476
|
+
* Resolution request types for the unified resolver.
|
|
477
|
+
*/
|
|
478
|
+
type ResolutionRequestType = "ambiguous_change" | "enum_value_removal";
|
|
479
|
+
/**
|
|
480
|
+
* Base interface for resolution requests.
|
|
481
|
+
*/
|
|
482
|
+
interface ResolutionRequestBase<T extends ResolutionRequestType> {
|
|
483
|
+
/** Unique identifier for this request */
|
|
484
|
+
id: string;
|
|
485
|
+
/** The type of resolution needed */
|
|
486
|
+
type: T;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Request to resolve an ambiguous schema change (rename vs add+drop).
|
|
490
|
+
*/
|
|
491
|
+
interface AmbiguousChangeRequest extends ResolutionRequestBase<"ambiguous_change"> {
|
|
492
|
+
/** The ambiguous change details */
|
|
493
|
+
change: AmbiguousChange;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Request to resolve enum value removals.
|
|
497
|
+
*/
|
|
498
|
+
interface EnumValueRemovalRequest extends ResolutionRequestBase<"enum_value_removal"> {
|
|
499
|
+
/** The enum value removal details */
|
|
500
|
+
removal: EnumValueRemoval;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Union type of all resolution request types.
|
|
504
|
+
*/
|
|
505
|
+
type ResolutionRequest = AmbiguousChangeRequest | EnumValueRemovalRequest;
|
|
506
|
+
/**
|
|
507
|
+
* Resolution result for an ambiguous change.
|
|
508
|
+
*/
|
|
509
|
+
interface AmbiguousChangeResolution {
|
|
510
|
+
type: "ambiguous_change";
|
|
511
|
+
resolution: ChangeResolution;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Resolution result for an enum value removal.
|
|
515
|
+
*/
|
|
516
|
+
interface EnumValueRemovalResolution {
|
|
517
|
+
type: "enum_value_removal";
|
|
518
|
+
resolution: EnumValueResolution;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Union type of all resolution result types.
|
|
522
|
+
*/
|
|
523
|
+
type ResolutionResult = AmbiguousChangeResolution | EnumValueRemovalResolution;
|
|
524
|
+
/**
|
|
525
|
+
* Unified resolver callback that handles all types of resolutions.
|
|
526
|
+
*
|
|
527
|
+
* This allows a single resolver to handle both ambiguous changes and
|
|
528
|
+
* enum value removals in one callback, simplifying the API.
|
|
529
|
+
*
|
|
530
|
+
* @param requests - Array of resolution requests
|
|
531
|
+
* @returns Map of request ID to resolution result
|
|
532
|
+
*/
|
|
533
|
+
type UnifiedResolver = (requests: ResolutionRequest[]) => Promise<Map<string, ResolutionResult>>;
|
|
534
|
+
/**
|
|
535
|
+
* Helper to create an AmbiguousChangeRequest.
|
|
536
|
+
*/
|
|
537
|
+
declare function createAmbiguousChangeRequest(id: string, change: AmbiguousChange): AmbiguousChangeRequest;
|
|
538
|
+
/**
|
|
539
|
+
* Helper to create an EnumValueRemovalRequest.
|
|
540
|
+
*/
|
|
541
|
+
declare function createEnumValueRemovalRequest(id: string, removal: EnumValueRemoval): EnumValueRemovalRequest;
|
|
542
|
+
interface PushResult {
|
|
543
|
+
/** All operations that were applied (or would be applied in dry-run) */
|
|
544
|
+
operations: DiffOperation[];
|
|
545
|
+
/** Whether the operations were actually applied to the database */
|
|
546
|
+
applied: boolean;
|
|
547
|
+
/** Generated SQL statements */
|
|
548
|
+
sql: string[];
|
|
549
|
+
}
|
|
550
|
+
type Dialect = "postgresql" | "sqlite" | "mysql";
|
|
551
|
+
interface MigrationJournal {
|
|
552
|
+
/** Schema version for the journal format */
|
|
553
|
+
version: string;
|
|
554
|
+
/** Database dialect */
|
|
555
|
+
dialect: Dialect;
|
|
556
|
+
/** List of migration entries */
|
|
557
|
+
entries: MigrationEntry[];
|
|
558
|
+
}
|
|
559
|
+
interface MigrationEntry {
|
|
560
|
+
/** Sequential index (0, 1, 2...) */
|
|
561
|
+
idx: number;
|
|
562
|
+
/** Timestamp version (e.g., "20240118143052") */
|
|
563
|
+
version: string;
|
|
564
|
+
/** Migration name (e.g., "add_users_table") */
|
|
565
|
+
name: string;
|
|
566
|
+
/** Unix timestamp when generated */
|
|
567
|
+
when: number;
|
|
568
|
+
/** SHA256 hash of SQL content for integrity verification */
|
|
569
|
+
checksum: string;
|
|
570
|
+
/** Optional tag for grouping migrations */
|
|
571
|
+
tag?: string;
|
|
572
|
+
}
|
|
573
|
+
interface GenerateOptions {
|
|
574
|
+
/** Custom migration name (will be kebab-cased) */
|
|
575
|
+
name?: string;
|
|
576
|
+
/** Output directory for migrations (default: ./migrations). Ignored if storageDriver is provided. */
|
|
577
|
+
dir?: string;
|
|
578
|
+
/** Custom storage driver. If not provided, uses filesystem driver with `dir`. */
|
|
579
|
+
storageDriver?: MigrationStorageDriver;
|
|
580
|
+
/** Resolver for ambiguous changes */
|
|
581
|
+
resolver?: Resolver;
|
|
582
|
+
/** Resolver for enum value removals */
|
|
583
|
+
enumValueResolver?: EnumValueResolver;
|
|
584
|
+
/** Don't write files, just return what would be generated */
|
|
585
|
+
dryRun?: boolean;
|
|
586
|
+
}
|
|
587
|
+
interface GenerateResult {
|
|
588
|
+
/** Generated migration entry (null if no changes) */
|
|
589
|
+
entry: MigrationEntry | null;
|
|
590
|
+
/** Generated SQL statements */
|
|
591
|
+
sql: string[];
|
|
592
|
+
/** Combined SQL content with statement breakpoints */
|
|
593
|
+
content: string;
|
|
594
|
+
/** Operations included in migration */
|
|
595
|
+
operations: DiffOperation[];
|
|
596
|
+
/** Whether files were written */
|
|
597
|
+
written: boolean;
|
|
598
|
+
/** Message describing the result */
|
|
599
|
+
message: string;
|
|
600
|
+
}
|
|
601
|
+
interface ApplyOptions {
|
|
602
|
+
/** Apply up to this migration index (inclusive) */
|
|
603
|
+
to?: number;
|
|
604
|
+
/** Don't apply, just return what would be applied */
|
|
605
|
+
dryRun?: boolean;
|
|
606
|
+
}
|
|
607
|
+
interface MigrationStatus {
|
|
608
|
+
/** Migration entry from journal */
|
|
609
|
+
entry: MigrationEntry;
|
|
610
|
+
/** Whether this migration has been applied */
|
|
611
|
+
applied: boolean;
|
|
612
|
+
/** When the migration was applied (if applied) */
|
|
613
|
+
appliedAt?: Date;
|
|
614
|
+
}
|
|
615
|
+
interface AppliedMigration {
|
|
616
|
+
/** Migration name (matches MigrationEntry.name) */
|
|
617
|
+
name: string;
|
|
618
|
+
/** Checksum at time of application */
|
|
619
|
+
checksum: string;
|
|
620
|
+
/** When the migration was applied */
|
|
621
|
+
appliedAt: Date;
|
|
622
|
+
}
|
|
623
|
+
//#endregion
|
|
624
|
+
//#region src/migrations/drivers/types.d.ts
|
|
625
|
+
/**
|
|
626
|
+
* Capabilities that differ between migration drivers.
|
|
627
|
+
* Used to determine behavior in push() and serializer.
|
|
628
|
+
*/
|
|
629
|
+
interface MigrationCapabilities {
|
|
630
|
+
/**
|
|
631
|
+
* Whether this database supports native enum types.
|
|
632
|
+
* - PostgreSQL: true (CREATE TYPE ... AS ENUM)
|
|
633
|
+
* - SQLite: false (uses TEXT)
|
|
634
|
+
*/
|
|
635
|
+
supportsNativeEnums: boolean;
|
|
636
|
+
/**
|
|
637
|
+
* Whether ALTER TYPE ... ADD VALUE can run inside a transaction.
|
|
638
|
+
* - PostgreSQL: false (must run outside transaction)
|
|
639
|
+
* - SQLite: N/A (no native enums)
|
|
640
|
+
*/
|
|
641
|
+
supportsAddEnumValueInTransaction: boolean;
|
|
642
|
+
/**
|
|
643
|
+
* Supported index types.
|
|
644
|
+
* - PostgreSQL: ["btree", "hash", "gin", "gist"]
|
|
645
|
+
* - SQLite: ["btree"]
|
|
646
|
+
*/
|
|
647
|
+
supportsIndexTypes: string[];
|
|
648
|
+
/**
|
|
649
|
+
* Whether this database supports native array types.
|
|
650
|
+
* - PostgreSQL: true (native arrays with operators)
|
|
651
|
+
* - SQLite: false (uses JSON)
|
|
652
|
+
*/
|
|
653
|
+
supportsNativeArrays: boolean;
|
|
654
|
+
}
|
|
655
|
+
//#endregion
|
|
656
|
+
//#region src/migrations/drivers/base.d.ts
|
|
657
|
+
/**
|
|
658
|
+
* Context passed to DDL generation methods.
|
|
659
|
+
* Contains information needed for operations that require knowledge of the current schema.
|
|
660
|
+
*/
|
|
661
|
+
interface DDLContext {
|
|
662
|
+
/**
|
|
663
|
+
* Current database schema snapshot.
|
|
664
|
+
* Required for SQLite table recreation operations.
|
|
665
|
+
*/
|
|
666
|
+
currentSchema?: SchemaSnapshot;
|
|
667
|
+
}
|
|
668
|
+
type CreateTableOperation = Extract<DiffOperation, {
|
|
669
|
+
type: "createTable";
|
|
670
|
+
}>;
|
|
671
|
+
type DropTableOperation = Extract<DiffOperation, {
|
|
672
|
+
type: "dropTable";
|
|
673
|
+
}>;
|
|
674
|
+
type RenameTableOperation = Extract<DiffOperation, {
|
|
675
|
+
type: "renameTable";
|
|
676
|
+
}>;
|
|
677
|
+
type AddColumnOperation = Extract<DiffOperation, {
|
|
678
|
+
type: "addColumn";
|
|
679
|
+
}>;
|
|
680
|
+
type DropColumnOperation = Extract<DiffOperation, {
|
|
681
|
+
type: "dropColumn";
|
|
682
|
+
}>;
|
|
683
|
+
type RenameColumnOperation = Extract<DiffOperation, {
|
|
684
|
+
type: "renameColumn";
|
|
685
|
+
}>;
|
|
686
|
+
type AlterColumnOperation = Extract<DiffOperation, {
|
|
687
|
+
type: "alterColumn";
|
|
688
|
+
}>;
|
|
689
|
+
type CreateIndexOperation = Extract<DiffOperation, {
|
|
690
|
+
type: "createIndex";
|
|
691
|
+
}>;
|
|
692
|
+
type DropIndexOperation = Extract<DiffOperation, {
|
|
693
|
+
type: "dropIndex";
|
|
694
|
+
}>;
|
|
695
|
+
type AddForeignKeyOperation = Extract<DiffOperation, {
|
|
696
|
+
type: "addForeignKey";
|
|
697
|
+
}>;
|
|
698
|
+
type DropForeignKeyOperation = Extract<DiffOperation, {
|
|
699
|
+
type: "dropForeignKey";
|
|
700
|
+
}>;
|
|
701
|
+
type AddUniqueConstraintOperation = Extract<DiffOperation, {
|
|
702
|
+
type: "addUniqueConstraint";
|
|
703
|
+
}>;
|
|
704
|
+
type DropUniqueConstraintOperation = Extract<DiffOperation, {
|
|
705
|
+
type: "dropUniqueConstraint";
|
|
706
|
+
}>;
|
|
707
|
+
type AddPrimaryKeyOperation = Extract<DiffOperation, {
|
|
708
|
+
type: "addPrimaryKey";
|
|
709
|
+
}>;
|
|
710
|
+
type DropPrimaryKeyOperation = Extract<DiffOperation, {
|
|
711
|
+
type: "dropPrimaryKey";
|
|
712
|
+
}>;
|
|
713
|
+
type CreateEnumOperation = Extract<DiffOperation, {
|
|
714
|
+
type: "createEnum";
|
|
715
|
+
}>;
|
|
716
|
+
type DropEnumOperation = Extract<DiffOperation, {
|
|
717
|
+
type: "dropEnum";
|
|
718
|
+
}>;
|
|
719
|
+
type AlterEnumOperation = Extract<DiffOperation, {
|
|
720
|
+
type: "alterEnum";
|
|
721
|
+
}>;
|
|
722
|
+
/**
|
|
723
|
+
* Abstract base class for migration drivers.
|
|
724
|
+
*
|
|
725
|
+
* Each database (PostgreSQL, SQLite, LibSQL, etc.) extends this class
|
|
726
|
+
* to provide database-specific DDL generation and schema introspection.
|
|
727
|
+
*/
|
|
728
|
+
declare abstract class MigrationDriver {
|
|
729
|
+
/**
|
|
730
|
+
* The SQL dialect this driver targets.
|
|
731
|
+
*/
|
|
732
|
+
abstract readonly dialect: Dialect$1;
|
|
733
|
+
/**
|
|
734
|
+
* The specific driver name (e.g., "pg", "postgres", "pglite", "sqlite3", "libsql").
|
|
735
|
+
* Used for driver-specific registration and lookup.
|
|
736
|
+
*/
|
|
737
|
+
abstract readonly driverName: string;
|
|
738
|
+
/**
|
|
739
|
+
* Capabilities supported by this driver.
|
|
740
|
+
*/
|
|
741
|
+
abstract readonly capabilities: MigrationCapabilities;
|
|
742
|
+
/**
|
|
743
|
+
* Introspects the current database schema.
|
|
744
|
+
*
|
|
745
|
+
* @param executeRaw - Function to execute raw SQL queries
|
|
746
|
+
* @returns The current schema snapshot
|
|
747
|
+
*/
|
|
748
|
+
abstract introspect(executeRaw: <T>(sql: string, params?: unknown[]) => Promise<{
|
|
749
|
+
rows: T[];
|
|
750
|
+
}>): Promise<SchemaSnapshot>;
|
|
751
|
+
/**
|
|
752
|
+
* Maps a VibORM field type to the native database column type.
|
|
753
|
+
*
|
|
754
|
+
* @param field - The field definition
|
|
755
|
+
* @param fieldState - The field state with type info
|
|
756
|
+
* @returns The native column type string
|
|
757
|
+
*/
|
|
758
|
+
abstract mapFieldType(field: Field, fieldState: FieldState): string;
|
|
759
|
+
/**
|
|
760
|
+
* Gets the column type for an enum field.
|
|
761
|
+
*
|
|
762
|
+
* @param tableName - The table name
|
|
763
|
+
* @param columnName - The column name
|
|
764
|
+
* @param values - The enum values
|
|
765
|
+
* @returns The column type (e.g., "users_status_enum" for PG, "TEXT" for SQLite)
|
|
766
|
+
*/
|
|
767
|
+
abstract getEnumColumnType(tableName: string, columnName: string, values: string[]): string;
|
|
768
|
+
abstract generateCreateTable(op: CreateTableOperation): string;
|
|
769
|
+
abstract generateDropTable(op: DropTableOperation): string;
|
|
770
|
+
abstract generateRenameTable(op: RenameTableOperation): string;
|
|
771
|
+
abstract generateAddColumn(op: AddColumnOperation): string;
|
|
772
|
+
abstract generateDropColumn(op: DropColumnOperation): string;
|
|
773
|
+
abstract generateRenameColumn(op: RenameColumnOperation): string;
|
|
774
|
+
abstract generateAlterColumn(op: AlterColumnOperation, context?: DDLContext): string;
|
|
775
|
+
abstract generateCreateIndex(op: CreateIndexOperation): string;
|
|
776
|
+
abstract generateDropIndex(op: DropIndexOperation): string;
|
|
777
|
+
abstract generateAddForeignKey(op: AddForeignKeyOperation, context?: DDLContext): string;
|
|
778
|
+
abstract generateDropForeignKey(op: DropForeignKeyOperation, context?: DDLContext): string;
|
|
779
|
+
abstract generateAddUniqueConstraint(op: AddUniqueConstraintOperation): string;
|
|
780
|
+
abstract generateDropUniqueConstraint(op: DropUniqueConstraintOperation): string;
|
|
781
|
+
abstract generateAddPrimaryKey(op: AddPrimaryKeyOperation, context?: DDLContext): string;
|
|
782
|
+
abstract generateDropPrimaryKey(op: DropPrimaryKeyOperation, context?: DDLContext): string;
|
|
783
|
+
abstract generateCreateEnum(op: CreateEnumOperation, context?: DDLContext): string;
|
|
784
|
+
abstract generateDropEnum(op: DropEnumOperation, context?: DDLContext): string;
|
|
785
|
+
abstract generateAlterEnum(op: AlterEnumOperation, context?: DDLContext): string;
|
|
786
|
+
/**
|
|
787
|
+
* Escapes an identifier (table name, column name, etc.).
|
|
788
|
+
* Default uses double quotes with proper escaping.
|
|
789
|
+
*/
|
|
790
|
+
escapeIdentifier(name: string): string;
|
|
791
|
+
/**
|
|
792
|
+
* Escapes a string value for SQL.
|
|
793
|
+
* Default uses single quotes with proper escaping.
|
|
794
|
+
* Returns "NULL" for null/undefined values.
|
|
795
|
+
*/
|
|
796
|
+
escapeValue(value: string | null | undefined): string;
|
|
797
|
+
/**
|
|
798
|
+
* Formats a referential action for foreign keys.
|
|
799
|
+
*/
|
|
800
|
+
formatReferentialAction(action: ReferentialAction): string;
|
|
801
|
+
/**
|
|
802
|
+
* Gets the SQL default expression for a field.
|
|
803
|
+
* Common implementation that handles most cases.
|
|
804
|
+
* Override for database-specific behavior (e.g., UUID generation, boolean representation).
|
|
805
|
+
*
|
|
806
|
+
* @param fieldState - The field state
|
|
807
|
+
* @returns SQL default expression or undefined if no default
|
|
808
|
+
*/
|
|
809
|
+
getDefaultExpression(fieldState: FieldState): string | undefined;
|
|
810
|
+
/**
|
|
811
|
+
* Formats a boolean default value.
|
|
812
|
+
* Override for databases that use different representations (e.g., SQLite uses 1/0).
|
|
813
|
+
*/
|
|
814
|
+
protected formatBooleanDefault(value: boolean): string;
|
|
815
|
+
/**
|
|
816
|
+
* Gets the SQL expression for an auto-generated value.
|
|
817
|
+
* Override for databases that support specific generation functions.
|
|
818
|
+
*
|
|
819
|
+
* By default, returns undefined for all types (handled at application level).
|
|
820
|
+
* PostgreSQL overrides this to use gen_random_uuid() for UUID.
|
|
821
|
+
*
|
|
822
|
+
* @param autoGenerate - The auto-generate type
|
|
823
|
+
* @returns SQL expression or undefined if handled at application level
|
|
824
|
+
*/
|
|
825
|
+
protected getAutoGenerateExpression(_autoGenerate: FieldState["autoGenerate"]): string | undefined;
|
|
826
|
+
/**
|
|
827
|
+
* Validates an index type against supported capabilities.
|
|
828
|
+
* Throws MigrationError if the index type is not supported.
|
|
829
|
+
*
|
|
830
|
+
* @param indexType - The index type to validate (e.g., "btree", "hash", "gin")
|
|
831
|
+
* @param indexName - The index name (for error messages)
|
|
832
|
+
*/
|
|
833
|
+
protected validateIndexType(indexType: string | undefined, indexName: string): void;
|
|
834
|
+
/**
|
|
835
|
+
* Generates a column definition string.
|
|
836
|
+
* Common implementation that can be overridden for database-specific syntax.
|
|
837
|
+
*
|
|
838
|
+
* @param column - The column definition
|
|
839
|
+
* @returns SQL column definition string
|
|
840
|
+
*/
|
|
841
|
+
protected generateColumnDef(column: ColumnDef): string;
|
|
842
|
+
/**
|
|
843
|
+
* Formats the column type, handling auto-increment and other special cases.
|
|
844
|
+
* Override for database-specific type handling (e.g., PostgreSQL SERIAL).
|
|
845
|
+
*/
|
|
846
|
+
protected formatColumnType(column: ColumnDef): string;
|
|
847
|
+
/**
|
|
848
|
+
* Generates DDL for creating the migration tracking table.
|
|
849
|
+
* Override for database-specific syntax.
|
|
850
|
+
*
|
|
851
|
+
* @param tableName - The tracking table name (already validated)
|
|
852
|
+
* @returns SQL DDL statement
|
|
853
|
+
*/
|
|
854
|
+
abstract generateCreateTrackingTable(tableName: string): string;
|
|
855
|
+
/**
|
|
856
|
+
* Generates SQL for selecting applied migrations.
|
|
857
|
+
*
|
|
858
|
+
* @param tableName - The tracking table name
|
|
859
|
+
* @returns SQL SELECT statement
|
|
860
|
+
*/
|
|
861
|
+
generateSelectAppliedMigrations(tableName: string): string;
|
|
862
|
+
/**
|
|
863
|
+
* Generates SQL for inserting a migration record.
|
|
864
|
+
*
|
|
865
|
+
* @param tableName - The tracking table name
|
|
866
|
+
* @returns SQL INSERT statement with placeholders
|
|
867
|
+
*/
|
|
868
|
+
abstract generateInsertMigration(tableName: string): {
|
|
869
|
+
sql: string;
|
|
870
|
+
paramCount: number;
|
|
871
|
+
};
|
|
872
|
+
/**
|
|
873
|
+
* Generates SQL for deleting a migration record.
|
|
874
|
+
*
|
|
875
|
+
* @param tableName - The tracking table name
|
|
876
|
+
* @returns SQL DELETE statement with placeholders
|
|
877
|
+
*/
|
|
878
|
+
abstract generateDeleteMigration(tableName: string): {
|
|
879
|
+
sql: string;
|
|
880
|
+
paramCount: number;
|
|
881
|
+
};
|
|
882
|
+
/**
|
|
883
|
+
* Generates SQL for clearing all migration records.
|
|
884
|
+
* Used by reset command.
|
|
885
|
+
*
|
|
886
|
+
* @param tableName - The tracking table name
|
|
887
|
+
* @returns SQL DELETE statement
|
|
888
|
+
*/
|
|
889
|
+
generateClearMigrations(tableName: string): string;
|
|
890
|
+
/**
|
|
891
|
+
* Generates SQL for acquiring a migration lock.
|
|
892
|
+
* Returns null if locking is handled differently (e.g., SQLite transactions).
|
|
893
|
+
*
|
|
894
|
+
* @param lockId - A numeric lock identifier
|
|
895
|
+
* @returns SQL statement or null
|
|
896
|
+
*/
|
|
897
|
+
abstract generateAcquireLock(lockId: number): string | null;
|
|
898
|
+
/**
|
|
899
|
+
* Generates SQL for releasing a migration lock.
|
|
900
|
+
* Returns null if locking is handled differently (e.g., SQLite transactions).
|
|
901
|
+
*
|
|
902
|
+
* @param lockId - A numeric lock identifier
|
|
903
|
+
* @returns SQL statement or null
|
|
904
|
+
*/
|
|
905
|
+
abstract generateReleaseLock(lockId: number): string | null;
|
|
906
|
+
/**
|
|
907
|
+
* Generates SQL statements for resetting the database (dropping all tables and enums).
|
|
908
|
+
* Used by `--force-reset` flag in CLI.
|
|
909
|
+
*
|
|
910
|
+
* @returns Array of SQL statements to execute
|
|
911
|
+
*/
|
|
912
|
+
abstract generateResetSQL(): string[];
|
|
913
|
+
/**
|
|
914
|
+
* Generates SQL for listing all user tables.
|
|
915
|
+
* Used by reset command.
|
|
916
|
+
*
|
|
917
|
+
* @returns SQL SELECT statement that returns rows with 'name' column
|
|
918
|
+
*/
|
|
919
|
+
abstract generateListTables(): string;
|
|
920
|
+
/**
|
|
921
|
+
* Generates SQL for listing all enum types.
|
|
922
|
+
* Returns null for databases that don't support enums.
|
|
923
|
+
*
|
|
924
|
+
* @returns SQL SELECT statement or null
|
|
925
|
+
*/
|
|
926
|
+
abstract generateListEnums(): string | null;
|
|
927
|
+
/**
|
|
928
|
+
* Generates SQL for dropping a table.
|
|
929
|
+
*
|
|
930
|
+
* @param tableName - The table name
|
|
931
|
+
* @param cascade - Whether to cascade (drop dependent objects)
|
|
932
|
+
* @returns SQL DROP TABLE statement
|
|
933
|
+
*/
|
|
934
|
+
generateDropTableSQL(tableName: string, cascade?: boolean): string;
|
|
935
|
+
/**
|
|
936
|
+
* Generates SQL for dropping an enum type.
|
|
937
|
+
* Returns null for databases that don't support enums.
|
|
938
|
+
*
|
|
939
|
+
* @param enumName - The enum name
|
|
940
|
+
* @returns SQL DROP TYPE statement or null
|
|
941
|
+
*/
|
|
942
|
+
generateDropEnumSQL(_enumName: string): string | null;
|
|
943
|
+
/**
|
|
944
|
+
* Generates DDL for a diff operation.
|
|
945
|
+
* Dispatches to the appropriate generate* method.
|
|
946
|
+
*
|
|
947
|
+
* @param operation - The diff operation
|
|
948
|
+
* @param context - Optional context for operations that need schema info (e.g., SQLite table recreation)
|
|
949
|
+
* @returns SQL DDL statement(s)
|
|
950
|
+
*/
|
|
951
|
+
generateDDL(operation: DiffOperation, context?: DDLContext): string;
|
|
952
|
+
}
|
|
953
|
+
//#endregion
|
|
954
|
+
//#region src/migrations/push.d.ts
|
|
955
|
+
/**
|
|
956
|
+
* Minimal client interface required for migrations.
|
|
957
|
+
* This allows push() to work with any object that has $driver and $schema.
|
|
958
|
+
*/
|
|
959
|
+
interface MigrationClient {
|
|
960
|
+
$driver: AnyDriver;
|
|
961
|
+
$schema: Record<string, AnyModel>;
|
|
962
|
+
}
|
|
963
|
+
interface PushOptions {
|
|
964
|
+
/** Skip confirmations for destructive and ambiguous changes */
|
|
965
|
+
force?: boolean;
|
|
966
|
+
/** Preview SQL without executing */
|
|
967
|
+
dryRun?: boolean;
|
|
968
|
+
/**
|
|
969
|
+
* Drop all tables and enums before pushing schema.
|
|
970
|
+
* If a storage driver is configured, also clears migration tracking.
|
|
971
|
+
* Use with extreme caution - all data will be lost.
|
|
972
|
+
*/
|
|
973
|
+
forceReset?: boolean;
|
|
974
|
+
/**
|
|
975
|
+
* Unified callback for resolving changes that require user input.
|
|
976
|
+
* Called once per change, allowing granular control over each decision.
|
|
977
|
+
*
|
|
978
|
+
* Each change has methods for valid resolutions:
|
|
979
|
+
* - Destructive: `change.proceed()`, `change.reject()`
|
|
980
|
+
* - Ambiguous: `change.rename()`, `change.addAndDrop()`, `change.reject()`
|
|
981
|
+
* - Enum value removal: `change.mapValues({...})`, `change.useNull()`, `change.reject()`
|
|
982
|
+
*
|
|
983
|
+
* **Combining with `force`:**
|
|
984
|
+
* When both `resolve` and `force: true` are provided, the resolver takes precedence.
|
|
985
|
+
* If the resolver returns `undefined` (doesn't handle a change), force mode kicks in:
|
|
986
|
+
* - Destructive: auto-accepted
|
|
987
|
+
* - Ambiguous: treated as add+drop
|
|
988
|
+
* - Enum removal: set to NULL
|
|
989
|
+
*
|
|
990
|
+
* This allows patterns like "accept everything except dropping table X".
|
|
991
|
+
*
|
|
992
|
+
* @example
|
|
993
|
+
* ```ts
|
|
994
|
+
* // Protect specific tables while auto-accepting everything else
|
|
995
|
+
* await migrations.push({
|
|
996
|
+
* force: true,
|
|
997
|
+
* resolve: async (change) => {
|
|
998
|
+
* if (change.type === "destructive" && change.table === "users") {
|
|
999
|
+
* return change.reject(); // Protect users table
|
|
1000
|
+
* }
|
|
1001
|
+
* // Return undefined to let force handle the rest
|
|
1002
|
+
* },
|
|
1003
|
+
* });
|
|
1004
|
+
* ```
|
|
1005
|
+
*/
|
|
1006
|
+
resolve?: ResolveCallback;
|
|
1007
|
+
/**
|
|
1008
|
+
* Internal: storage driver passed from migration client.
|
|
1009
|
+
* Used for forceReset to clear migration tracking if available.
|
|
1010
|
+
* @internal
|
|
1011
|
+
*/
|
|
1012
|
+
_storageDriver?: MigrationStorageDriver;
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Pushes schema changes directly to the database.
|
|
1016
|
+
*
|
|
1017
|
+
* @param client - VibORM client containing driver and schema
|
|
1018
|
+
* @param options - Push options
|
|
1019
|
+
* @returns Push result with operations and SQL statements
|
|
1020
|
+
*/
|
|
1021
|
+
declare function push(client: MigrationClient, options?: PushOptions): Promise<PushResult>;
|
|
1022
|
+
/**
|
|
1023
|
+
* Introspects the current database schema without making any changes.
|
|
1024
|
+
* Useful for debugging or displaying current state.
|
|
1025
|
+
*/
|
|
1026
|
+
declare function introspect(client: MigrationClient): Promise<SchemaSnapshot>;
|
|
1027
|
+
/**
|
|
1028
|
+
* Generates DDL statements for transforming current schema to desired schema
|
|
1029
|
+
* without executing them. Useful for generating migration files.
|
|
1030
|
+
*/
|
|
1031
|
+
declare function generateDDL(client: MigrationClient, options?: {
|
|
1032
|
+
resolver?: Resolver;
|
|
1033
|
+
}): Promise<{
|
|
1034
|
+
operations: DiffOperation[];
|
|
1035
|
+
sql: string[];
|
|
1036
|
+
}>;
|
|
1037
|
+
/**
|
|
1038
|
+
* Formats an operation for human-readable display
|
|
1039
|
+
*/
|
|
1040
|
+
declare function formatOperation(op: DiffOperation): string;
|
|
1041
|
+
/**
|
|
1042
|
+
* Formats all operations for human-readable display
|
|
1043
|
+
*/
|
|
1044
|
+
declare function formatOperations(operations: DiffOperation[]): string;
|
|
1045
|
+
//#endregion
|
|
1046
|
+
//#region src/migrations/utils.d.ts
|
|
1047
|
+
declare const DEFAULT_MIGRATIONS_DIR = "./migrations";
|
|
1048
|
+
declare const DEFAULT_TABLE_NAME = "_viborm_migrations";
|
|
1049
|
+
/**
|
|
1050
|
+
* Validates and normalizes the database dialect for migrations.
|
|
1051
|
+
* Throws if the dialect is not supported.
|
|
1052
|
+
*/
|
|
1053
|
+
declare function normalizeDialect(dialect: string): Dialect;
|
|
1054
|
+
/**
|
|
1055
|
+
* Type for executing SQL queries and returning rows.
|
|
1056
|
+
*/
|
|
1057
|
+
type QueryExecutor = (sql: string, params?: unknown[]) => Promise<unknown[]>;
|
|
1058
|
+
/**
|
|
1059
|
+
* Sorts operations for proper execution order:
|
|
1060
|
+
* 1. Create enums (before tables that use them)
|
|
1061
|
+
* 2. Drop foreign keys (before dropping tables/columns)
|
|
1062
|
+
* 3. Drop indexes, constraints, columns
|
|
1063
|
+
* 4. Drop tables
|
|
1064
|
+
* 5. Create tables
|
|
1065
|
+
* 6. Add columns, constraints
|
|
1066
|
+
* 7. Create indexes
|
|
1067
|
+
* 8. Add foreign keys (after tables/columns exist)
|
|
1068
|
+
*/
|
|
1069
|
+
declare function sortOperations(operations: DiffOperation[]): DiffOperation[];
|
|
1070
|
+
/**
|
|
1071
|
+
* Formats a migration filename from entry.
|
|
1072
|
+
* Example: 0000_initial.sql, 0001_add-users.sql
|
|
1073
|
+
*/
|
|
1074
|
+
declare function formatMigrationFilename(entry: MigrationEntry): string;
|
|
1075
|
+
/**
|
|
1076
|
+
* Generate a migration name based on the operations.
|
|
1077
|
+
*/
|
|
1078
|
+
declare function generateMigrationName(operations: DiffOperation[]): string;
|
|
1079
|
+
//#endregion
|
|
1080
|
+
//#region src/migrations/context.d.ts
|
|
1081
|
+
interface MigrationContextOptions {
|
|
1082
|
+
/** Migrations directory (default: ./migrations) */
|
|
1083
|
+
dir?: string;
|
|
1084
|
+
/** Migration tracking table name (default: _viborm_migrations) */
|
|
1085
|
+
tableName?: string;
|
|
1086
|
+
/** Storage driver for migration files (required) */
|
|
1087
|
+
storageDriver: MigrationStorageDriver;
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Migration context that encapsulates configuration and provides
|
|
1091
|
+
* shared operations for migration commands.
|
|
1092
|
+
*/
|
|
1093
|
+
declare class MigrationContext {
|
|
1094
|
+
readonly driver: AnyDriver;
|
|
1095
|
+
readonly dialect: Dialect;
|
|
1096
|
+
readonly migrationsDir: string;
|
|
1097
|
+
readonly tableName: string;
|
|
1098
|
+
readonly executor: QueryExecutor;
|
|
1099
|
+
readonly migrationDriver: MigrationDriver;
|
|
1100
|
+
readonly storage: MigrationStorageDriver;
|
|
1101
|
+
private _hasLock;
|
|
1102
|
+
constructor(client: MigrationClient, options: MigrationContextOptions);
|
|
1103
|
+
/**
|
|
1104
|
+
* Ensures the migrations tracking table exists.
|
|
1105
|
+
*/
|
|
1106
|
+
ensureTrackingTable(): Promise<void>;
|
|
1107
|
+
/**
|
|
1108
|
+
* Get all applied migrations from the database.
|
|
1109
|
+
*/
|
|
1110
|
+
getAppliedMigrations(): Promise<AppliedMigration[]>;
|
|
1111
|
+
/**
|
|
1112
|
+
* Mark a migration as applied in the database.
|
|
1113
|
+
*/
|
|
1114
|
+
markMigrationApplied(entry: MigrationEntry): Promise<void>;
|
|
1115
|
+
/**
|
|
1116
|
+
* Mark a migration as rolled back (remove from tracking table).
|
|
1117
|
+
*/
|
|
1118
|
+
markMigrationRolledBack(name: string): Promise<void>;
|
|
1119
|
+
/**
|
|
1120
|
+
* Delete a migration from the tracking table by name.
|
|
1121
|
+
* Alias for markMigrationRolledBack, used by squash.
|
|
1122
|
+
*/
|
|
1123
|
+
deleteMigration(name: string): Promise<void>;
|
|
1124
|
+
/**
|
|
1125
|
+
* Acquires a migration lock to prevent concurrent migrations.
|
|
1126
|
+
* Uses PostgreSQL advisory locks or SQLite exclusive transactions.
|
|
1127
|
+
*/
|
|
1128
|
+
acquireLock(): Promise<void>;
|
|
1129
|
+
/**
|
|
1130
|
+
* Releases the migration lock.
|
|
1131
|
+
*/
|
|
1132
|
+
releaseLock(): Promise<void>;
|
|
1133
|
+
/**
|
|
1134
|
+
* Execute a function while holding the migration lock.
|
|
1135
|
+
*/
|
|
1136
|
+
withLock<T>(fn: () => Promise<T>): Promise<T>;
|
|
1137
|
+
/**
|
|
1138
|
+
* Execute a function within a transaction.
|
|
1139
|
+
* The callback receives a transaction-bound context.
|
|
1140
|
+
*/
|
|
1141
|
+
transaction<T>(fn: (txCtx: MigrationContext) => Promise<T>): Promise<T>;
|
|
1142
|
+
/**
|
|
1143
|
+
* Execute raw SQL.
|
|
1144
|
+
*/
|
|
1145
|
+
executeRaw(sqlStr: string, params?: unknown[]): Promise<void>;
|
|
1146
|
+
/**
|
|
1147
|
+
* Executes parsed SQL statements from a migration file.
|
|
1148
|
+
* Filters out empty lines and comments, ensures semicolons.
|
|
1149
|
+
*
|
|
1150
|
+
* Uses the following priority for atomicity:
|
|
1151
|
+
* 1. Native batch (if driver.supportsBatch) - D1, D1-HTTP, Neon-HTTP
|
|
1152
|
+
* 2. Transaction wrapper (if driver.supportsTransactions) - most drivers
|
|
1153
|
+
* 3. Sequential execution with warning (no atomicity)
|
|
1154
|
+
*
|
|
1155
|
+
* @param statements - Array of SQL statements to execute
|
|
1156
|
+
*/
|
|
1157
|
+
executeMigrationStatements(statements: string[]): Promise<void>;
|
|
1158
|
+
}
|
|
1159
|
+
//#endregion
|
|
1160
|
+
//#region src/migrations/apply/down.d.ts
|
|
1161
|
+
interface DownOptions extends MigrationContextOptions {
|
|
1162
|
+
/** Number of migrations to roll back (default: 1) */
|
|
1163
|
+
steps?: number;
|
|
1164
|
+
/** Roll back to a specific migration (by name or index) */
|
|
1165
|
+
to?: string | number;
|
|
1166
|
+
/** Preview without executing */
|
|
1167
|
+
dryRun?: boolean;
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Result of rolling back migrations.
|
|
1171
|
+
* Throws MigrationError on failure instead of returning error object.
|
|
1172
|
+
*/
|
|
1173
|
+
interface DownResult {
|
|
1174
|
+
/** Migrations that were rolled back */
|
|
1175
|
+
rolledBack: MigrationEntry[];
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Rolls back applied migrations.
|
|
1179
|
+
* Throws MigrationError on failure.
|
|
1180
|
+
*
|
|
1181
|
+
* This will:
|
|
1182
|
+
* 1. Verify checksums before rolling back
|
|
1183
|
+
* 2. Check for down migration files (if available)
|
|
1184
|
+
* 3. Execute down SQL in reverse order (atomically in a transaction)
|
|
1185
|
+
* 4. Remove migrations from tracking table
|
|
1186
|
+
*
|
|
1187
|
+
* @param client - VibORM client with driver
|
|
1188
|
+
* @param options - Down options
|
|
1189
|
+
* @returns Result with rolled back migrations
|
|
1190
|
+
* @throws MigrationError if rollback fails
|
|
1191
|
+
*/
|
|
1192
|
+
declare function down(client: MigrationClient, options: DownOptions): Promise<DownResult>;
|
|
1193
|
+
//#endregion
|
|
1194
|
+
//#region src/migrations/apply/index.d.ts
|
|
1195
|
+
interface ApplyFullOptions extends ApplyOptions {
|
|
1196
|
+
/** Migrations directory */
|
|
1197
|
+
dir?: string;
|
|
1198
|
+
/** Name of the migrations tracking table */
|
|
1199
|
+
tableName?: string;
|
|
1200
|
+
/** Storage driver for migration files */
|
|
1201
|
+
storageDriver: MigrationStorageDriver;
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Result of applying migrations.
|
|
1205
|
+
* Throws MigrationError on failure instead of returning error object.
|
|
1206
|
+
*/
|
|
1207
|
+
interface ApplyResult {
|
|
1208
|
+
/** Migrations that were applied */
|
|
1209
|
+
applied: MigrationEntry[];
|
|
1210
|
+
/** Migrations that are still pending */
|
|
1211
|
+
pending: MigrationEntry[];
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Applies pending migrations to the database.
|
|
1215
|
+
* Throws MigrationError on failure.
|
|
1216
|
+
*
|
|
1217
|
+
* @param client - VibORM client with driver
|
|
1218
|
+
* @param options - Apply options
|
|
1219
|
+
* @returns Apply result with applied and pending migrations
|
|
1220
|
+
* @throws MigrationError if migration fails
|
|
1221
|
+
*/
|
|
1222
|
+
declare function apply(client: MigrationClient, options: ApplyFullOptions): Promise<ApplyResult>;
|
|
1223
|
+
/**
|
|
1224
|
+
* Get the status of all migrations (applied vs pending).
|
|
1225
|
+
*/
|
|
1226
|
+
declare function status(client: MigrationClient, options: {
|
|
1227
|
+
dir?: string;
|
|
1228
|
+
tableName?: string;
|
|
1229
|
+
storageDriver: MigrationStorageDriver;
|
|
1230
|
+
}): Promise<MigrationStatus[]>;
|
|
1231
|
+
/**
|
|
1232
|
+
* Get pending migrations that haven't been applied yet.
|
|
1233
|
+
*/
|
|
1234
|
+
declare function pending(client: MigrationClient, options: {
|
|
1235
|
+
dir?: string;
|
|
1236
|
+
tableName?: string;
|
|
1237
|
+
storageDriver: MigrationStorageDriver;
|
|
1238
|
+
}): Promise<MigrationEntry[]>;
|
|
1239
|
+
/**
|
|
1240
|
+
* Rollback the last N migrations (marks them as not applied).
|
|
1241
|
+
* Note: This does NOT run down migrations - it only removes from tracking table.
|
|
1242
|
+
*/
|
|
1243
|
+
declare function rollback(client: MigrationClient, options: {
|
|
1244
|
+
count?: number;
|
|
1245
|
+
dir?: string;
|
|
1246
|
+
tableName?: string;
|
|
1247
|
+
storageDriver: MigrationStorageDriver;
|
|
1248
|
+
}): Promise<MigrationEntry[]>;
|
|
1249
|
+
//#endregion
|
|
1250
|
+
//#region src/migrations/reset.d.ts
|
|
1251
|
+
interface ResetOptions extends MigrationContextOptions {
|
|
1252
|
+
/** Skip confirmation (dangerous!) */
|
|
1253
|
+
force?: boolean;
|
|
1254
|
+
/** Preview without executing */
|
|
1255
|
+
dryRun?: boolean;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Result of resetting the database.
|
|
1259
|
+
* Throws MigrationError on failure instead of returning error object.
|
|
1260
|
+
*/
|
|
1261
|
+
interface ResetResult {
|
|
1262
|
+
/** Tables that were dropped */
|
|
1263
|
+
dropped: string[];
|
|
1264
|
+
/** Migrations that were re-applied */
|
|
1265
|
+
applied: MigrationEntry[];
|
|
1266
|
+
}
|
|
1267
|
+
/**
|
|
1268
|
+
* Resets the database by dropping all tables and re-applying migrations.
|
|
1269
|
+
* Throws MigrationError on failure.
|
|
1270
|
+
*
|
|
1271
|
+
* WARNING: This is destructive! All data will be lost.
|
|
1272
|
+
*
|
|
1273
|
+
* The entire operation is wrapped in a transaction for atomicity.
|
|
1274
|
+
* If any step fails, the database is rolled back to its previous state.
|
|
1275
|
+
*
|
|
1276
|
+
* @param client - VibORM client with driver
|
|
1277
|
+
* @param options - Reset options
|
|
1278
|
+
* @returns Result with dropped tables and applied migrations
|
|
1279
|
+
* @throws MigrationError if reset fails
|
|
1280
|
+
*/
|
|
1281
|
+
declare function reset(client: MigrationClient, options: ResetOptions): Promise<ResetResult>;
|
|
1282
|
+
//#endregion
|
|
1283
|
+
//#region src/migrations/squash.d.ts
|
|
1284
|
+
interface SquashOptions extends MigrationContextOptions {
|
|
1285
|
+
/** Name for the squashed migration */
|
|
1286
|
+
name?: string;
|
|
1287
|
+
/** Start index (default: 0) */
|
|
1288
|
+
from?: number;
|
|
1289
|
+
/** End index (default: latest) */
|
|
1290
|
+
to?: number;
|
|
1291
|
+
/** Preview without executing */
|
|
1292
|
+
dryRun?: boolean;
|
|
1293
|
+
/** Remove old migration files after squashing (moves to meta/_backup) */
|
|
1294
|
+
cleanup?: boolean;
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Result of squashing migrations.
|
|
1298
|
+
* Throws MigrationError on failure instead of returning error object.
|
|
1299
|
+
*/
|
|
1300
|
+
interface SquashResult {
|
|
1301
|
+
/** The new squashed migration entry */
|
|
1302
|
+
entry: MigrationEntry;
|
|
1303
|
+
/** Number of migrations that were squashed */
|
|
1304
|
+
squashedCount: number;
|
|
1305
|
+
/** Combined SQL statements */
|
|
1306
|
+
sql: string[];
|
|
1307
|
+
/** Paths to archived migration files (if cleanup was enabled) */
|
|
1308
|
+
archivedFiles?: string[];
|
|
1309
|
+
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Squashes multiple migrations into a single migration.
|
|
1312
|
+
* Throws MigrationError on failure.
|
|
1313
|
+
*
|
|
1314
|
+
* This will:
|
|
1315
|
+
* 1. Read all migrations in the range
|
|
1316
|
+
* 2. Combine their SQL into a single migration
|
|
1317
|
+
* 3. Update the journal with the new squashed entry
|
|
1318
|
+
* 4. Update the database tracking table (if migrations were applied)
|
|
1319
|
+
* 5. Optionally remove old migration files
|
|
1320
|
+
*
|
|
1321
|
+
* NOTE: Only squash migrations that haven't been applied to production!
|
|
1322
|
+
*
|
|
1323
|
+
* @param client - VibORM client with driver
|
|
1324
|
+
* @param options - Squash options
|
|
1325
|
+
* @returns Result with the new squashed migration
|
|
1326
|
+
* @throws MigrationError if squash fails
|
|
1327
|
+
*/
|
|
1328
|
+
declare function squash(client: MigrationClient, options: SquashOptions): Promise<SquashResult>;
|
|
1329
|
+
//#endregion
|
|
1330
|
+
//#region src/migrations/client.d.ts
|
|
1331
|
+
interface MigrationClientOptions {
|
|
1332
|
+
/**
|
|
1333
|
+
* Storage driver for migration files.
|
|
1334
|
+
* Required for file-based migration operations (generate, apply, down, etc.).
|
|
1335
|
+
* Not required for push() which works directly with the database.
|
|
1336
|
+
*
|
|
1337
|
+
* @example
|
|
1338
|
+
* ```typescript
|
|
1339
|
+
* import { createFsStorageDriver } from "viborm/migrations/storage/fs";
|
|
1340
|
+
*
|
|
1341
|
+
* const migrations = createMigrationClient(client, {
|
|
1342
|
+
* storageDriver: createFsStorageDriver("./migrations"),
|
|
1343
|
+
* });
|
|
1344
|
+
* ```
|
|
1345
|
+
*/
|
|
1346
|
+
storageDriver?: MigrationStorageDriver;
|
|
1347
|
+
/** Migration tracking table name (default: _viborm_migrations) */
|
|
1348
|
+
tableName?: string;
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Migration client instance with all migration operations.
|
|
1352
|
+
*/
|
|
1353
|
+
interface Migrations {
|
|
1354
|
+
/** The storage driver used by this client */
|
|
1355
|
+
readonly storage: MigrationStorageDriver;
|
|
1356
|
+
/**
|
|
1357
|
+
* List all migrations from the journal.
|
|
1358
|
+
* Useful for displaying available migrations (e.g., before squash).
|
|
1359
|
+
*/
|
|
1360
|
+
list(): Promise<MigrationEntry[]>;
|
|
1361
|
+
/**
|
|
1362
|
+
* Get the migration journal.
|
|
1363
|
+
* Returns null if no journal exists yet.
|
|
1364
|
+
*/
|
|
1365
|
+
journal(): Promise<MigrationJournal | null>;
|
|
1366
|
+
/**
|
|
1367
|
+
* Get the current schema snapshot.
|
|
1368
|
+
* Returns null if no snapshot exists yet.
|
|
1369
|
+
*/
|
|
1370
|
+
snapshot(): Promise<SchemaSnapshot | null>;
|
|
1371
|
+
/**
|
|
1372
|
+
* Read a migration's SQL content.
|
|
1373
|
+
* Returns null if migration file doesn't exist.
|
|
1374
|
+
*/
|
|
1375
|
+
read(entry: MigrationEntry): Promise<string | null>;
|
|
1376
|
+
/**
|
|
1377
|
+
* Get the status of all migrations (journal + applied state).
|
|
1378
|
+
*/
|
|
1379
|
+
status(): Promise<MigrationStatus[]>;
|
|
1380
|
+
/**
|
|
1381
|
+
* Get pending migrations that haven't been applied.
|
|
1382
|
+
*/
|
|
1383
|
+
pending(): Promise<MigrationEntry[]>;
|
|
1384
|
+
/**
|
|
1385
|
+
* Generate a new migration by comparing schema with previous snapshot.
|
|
1386
|
+
*/
|
|
1387
|
+
generate(options?: Omit<GenerateOptions, "dir" | "storageDriver">): Promise<GenerateResult>;
|
|
1388
|
+
/**
|
|
1389
|
+
* Preview what migration would be generated without writing files.
|
|
1390
|
+
*/
|
|
1391
|
+
preview(options?: Omit<GenerateOptions, "dir" | "storageDriver" | "dryRun">): Promise<GenerateResult>;
|
|
1392
|
+
/**
|
|
1393
|
+
* Apply pending migrations to the database.
|
|
1394
|
+
*/
|
|
1395
|
+
apply(options?: ApplyOptions): Promise<ApplyResult>;
|
|
1396
|
+
/**
|
|
1397
|
+
* Rollback migrations (remove from tracking table only).
|
|
1398
|
+
*/
|
|
1399
|
+
rollback(options?: {
|
|
1400
|
+
count?: number;
|
|
1401
|
+
}): Promise<MigrationEntry[]>;
|
|
1402
|
+
/**
|
|
1403
|
+
* Roll back migrations with down SQL execution.
|
|
1404
|
+
*/
|
|
1405
|
+
down(options?: Omit<DownOptions, "dir" | "tableName" | "storageDriver">): Promise<DownResult>;
|
|
1406
|
+
/**
|
|
1407
|
+
* Reset the database (drop all tables and re-apply migrations).
|
|
1408
|
+
*/
|
|
1409
|
+
reset(options?: Omit<ResetOptions, "dir" | "tableName" | "storageDriver">): Promise<ResetResult>;
|
|
1410
|
+
/**
|
|
1411
|
+
* Squash multiple migrations into one.
|
|
1412
|
+
*/
|
|
1413
|
+
squash(options?: Omit<SquashOptions, "dir" | "tableName" | "storageDriver">): Promise<SquashResult>;
|
|
1414
|
+
/**
|
|
1415
|
+
* Push schema changes directly to the database without creating migration files.
|
|
1416
|
+
* Useful for development and rapid prototyping.
|
|
1417
|
+
*/
|
|
1418
|
+
push(options?: PushOptions): Promise<PushResult>;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Creates a migration client with shared configuration.
|
|
1422
|
+
*
|
|
1423
|
+
* @example
|
|
1424
|
+
* ```typescript
|
|
1425
|
+
* // For push-only usage (e.g., Cloudflare Workers)
|
|
1426
|
+
* const migrations = createMigrationClient(client);
|
|
1427
|
+
* await migrations.push();
|
|
1428
|
+
* ```
|
|
1429
|
+
*
|
|
1430
|
+
* @example
|
|
1431
|
+
* ```typescript
|
|
1432
|
+
* // For file-based migrations
|
|
1433
|
+
* import { createFsStorageDriver } from "viborm/migrations/storage/fs";
|
|
1434
|
+
*
|
|
1435
|
+
* const migrations = createMigrationClient(client, {
|
|
1436
|
+
* storageDriver: createFsStorageDriver("./migrations"),
|
|
1437
|
+
* });
|
|
1438
|
+
*
|
|
1439
|
+
* await migrations.generate({ name: "add-users" });
|
|
1440
|
+
* await migrations.apply();
|
|
1441
|
+
* ```
|
|
1442
|
+
*/
|
|
1443
|
+
declare function createMigrationClient(client: MigrationClient, options?: MigrationClientOptions): Migrations;
|
|
1444
|
+
//#endregion
|
|
1445
|
+
//#region src/migrations/differ.d.ts
|
|
1446
|
+
/**
|
|
1447
|
+
* Compares two schema snapshots and returns the operations needed to
|
|
1448
|
+
* transform the current schema into the desired schema.
|
|
1449
|
+
*/
|
|
1450
|
+
declare function diff(current: SchemaSnapshot, desired: SchemaSnapshot): DiffResult;
|
|
1451
|
+
/**
|
|
1452
|
+
* Checks if any operations are destructive (could cause data loss)
|
|
1453
|
+
*/
|
|
1454
|
+
declare function hasDestructiveOperations(operations: DiffOperation[]): boolean;
|
|
1455
|
+
/**
|
|
1456
|
+
* Gets a human-readable description of destructive operations
|
|
1457
|
+
*/
|
|
1458
|
+
declare function getDestructiveOperationDescriptions(operations: DiffOperation[]): string[];
|
|
1459
|
+
//#endregion
|
|
1460
|
+
//#region src/migrations/generate/index.d.ts
|
|
1461
|
+
/**
|
|
1462
|
+
* Generates a new migration file by comparing the schema with the last snapshot.
|
|
1463
|
+
*
|
|
1464
|
+
* @param client - VibORM client with driver and schema
|
|
1465
|
+
* @param options - Generation options
|
|
1466
|
+
* @returns Generation result with entry, SQL, and operations
|
|
1467
|
+
*/
|
|
1468
|
+
declare function generate(client: MigrationClient, options?: GenerateOptions): Promise<GenerateResult>;
|
|
1469
|
+
/**
|
|
1470
|
+
* Preview what migration would be generated without writing files.
|
|
1471
|
+
*/
|
|
1472
|
+
declare function preview(client: MigrationClient, options?: Omit<GenerateOptions, "dryRun">): Promise<GenerateResult>;
|
|
1473
|
+
//#endregion
|
|
1474
|
+
//#region src/migrations/resolver.d.ts
|
|
1475
|
+
/**
|
|
1476
|
+
* Converts resolved ambiguous changes into concrete diff operations
|
|
1477
|
+
*/
|
|
1478
|
+
declare function applyResolutions(changes: AmbiguousChange[], resolutions: Map<AmbiguousChange, ChangeResolution>): DiffOperation[];
|
|
1479
|
+
/**
|
|
1480
|
+
* Resolves ambiguous changes and returns the final operations list.
|
|
1481
|
+
* Handles both rename and addAndDrop resolutions, including adding
|
|
1482
|
+
* createTable operations for table renames resolved as addAndDrop.
|
|
1483
|
+
*
|
|
1484
|
+
* @param diffResult - The result from the differ containing operations and ambiguous changes
|
|
1485
|
+
* @param desiredSnapshot - The desired schema snapshot (used for table definitions)
|
|
1486
|
+
* @param resolver - The resolver function to use for ambiguous changes
|
|
1487
|
+
* @returns Sorted list of final operations
|
|
1488
|
+
*/
|
|
1489
|
+
declare function resolveAmbiguousChanges(diffResult: DiffResult, desiredSnapshot: SchemaSnapshot, resolver: Resolver): Promise<DiffOperation[]>;
|
|
1490
|
+
/**
|
|
1491
|
+
* Resolver that always chooses "rename" for all ambiguous changes.
|
|
1492
|
+
* Useful for preserving data when the intent is clear.
|
|
1493
|
+
*/
|
|
1494
|
+
declare const alwaysRenameResolver: Resolver;
|
|
1495
|
+
/**
|
|
1496
|
+
* Resolver that always chooses "addAndDrop" for all ambiguous changes.
|
|
1497
|
+
* Useful for clean slate scenarios where data loss is acceptable.
|
|
1498
|
+
*/
|
|
1499
|
+
declare const alwaysAddDropResolver: Resolver;
|
|
1500
|
+
/**
|
|
1501
|
+
* Resolver that throws an error if any ambiguous changes are detected.
|
|
1502
|
+
* Useful for CI/CD pipelines where human intervention is not possible.
|
|
1503
|
+
*/
|
|
1504
|
+
declare const strictResolver: Resolver;
|
|
1505
|
+
/**
|
|
1506
|
+
* Creates a resolver from a simple decision function
|
|
1507
|
+
*/
|
|
1508
|
+
declare function createResolver(decide: (change: AmbiguousChange) => "rename" | "addAndDrop" | Promise<"rename" | "addAndDrop">): Resolver;
|
|
1509
|
+
/**
|
|
1510
|
+
* Creates a resolver that uses predefined resolutions
|
|
1511
|
+
*/
|
|
1512
|
+
declare function createPredefinedResolver(predefined: Array<{
|
|
1513
|
+
type: "column" | "table";
|
|
1514
|
+
from: string;
|
|
1515
|
+
to: string;
|
|
1516
|
+
tableName?: string;
|
|
1517
|
+
resolution: "rename" | "addAndDrop";
|
|
1518
|
+
}>): Resolver;
|
|
1519
|
+
/**
|
|
1520
|
+
* Formats ambiguous changes for display
|
|
1521
|
+
*/
|
|
1522
|
+
declare function formatAmbiguousChange(change: AmbiguousChange): string;
|
|
1523
|
+
/**
|
|
1524
|
+
* Formats all ambiguous changes for display
|
|
1525
|
+
*/
|
|
1526
|
+
declare function formatAmbiguousChanges(changes: AmbiguousChange[]): string;
|
|
1527
|
+
/**
|
|
1528
|
+
* Rejects all changes requiring resolution.
|
|
1529
|
+
* Useful for CI/CD pipelines where human intervention is not possible.
|
|
1530
|
+
*/
|
|
1531
|
+
declare const rejectAllResolver: ResolveCallback;
|
|
1532
|
+
/**
|
|
1533
|
+
* Accepts destructive changes, treats ambiguous changes as renames,
|
|
1534
|
+
* and maps enum value removals to NULL.
|
|
1535
|
+
* Useful for development when you know all changes are intentional renames.
|
|
1536
|
+
*/
|
|
1537
|
+
declare const lenientResolver: ResolveCallback;
|
|
1538
|
+
/**
|
|
1539
|
+
* Accepts destructive changes, treats ambiguous changes as add+drop,
|
|
1540
|
+
* and maps enum value removals to NULL.
|
|
1541
|
+
* Useful when you don't care about preserving data in ambiguous scenarios.
|
|
1542
|
+
*/
|
|
1543
|
+
declare const addDropResolver: ResolveCallback;
|
|
1544
|
+
/**
|
|
1545
|
+
* Creates a unified resolver from a decision function.
|
|
1546
|
+
*
|
|
1547
|
+
* @example
|
|
1548
|
+
* ```ts
|
|
1549
|
+
* const resolver = createUnifiedResolver(async (change) => {
|
|
1550
|
+
* if (change.type === "destructive") {
|
|
1551
|
+
* return confirm(`Accept: ${change.description}?`) ? change.proceed() : change.reject();
|
|
1552
|
+
* }
|
|
1553
|
+
* if (change.type === "ambiguous") {
|
|
1554
|
+
* return change.rename();
|
|
1555
|
+
* }
|
|
1556
|
+
* if (change.type === "enumValueRemoval") {
|
|
1557
|
+
* return change.mapValues({ 'OLD': 'NEW' });
|
|
1558
|
+
* }
|
|
1559
|
+
* return change.reject();
|
|
1560
|
+
* });
|
|
1561
|
+
* ```
|
|
1562
|
+
*/
|
|
1563
|
+
declare function createUnifiedResolver(decide: (change: ResolveChange) => Promise<"proceed" | "reject" | "rename" | "addAndDrop" | "enumMapped">): ResolveCallback;
|
|
1564
|
+
//#endregion
|
|
1565
|
+
//#region src/migrations/serializer.d.ts
|
|
1566
|
+
interface SerializeOptions {
|
|
1567
|
+
migrationDriver: MigrationDriver;
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* Serializes a collection of VibORM models into a SchemaSnapshot
|
|
1571
|
+
*/
|
|
1572
|
+
declare function serializeModels(models: Record<string, AnyModel>, options: SerializeOptions): SchemaSnapshot;
|
|
1573
|
+
/**
|
|
1574
|
+
* Gets the SQL column name for a field using the model's nameRegistry.
|
|
1575
|
+
* This supports field reuse across multiple models.
|
|
1576
|
+
*
|
|
1577
|
+
* @param model - The model containing the field
|
|
1578
|
+
* @param fieldName - The field key in the schema
|
|
1579
|
+
* @returns The SQL column name
|
|
1580
|
+
*/
|
|
1581
|
+
declare function getColumnName(model: AnyModel, fieldName: string): string;
|
|
1582
|
+
/**
|
|
1583
|
+
* Gets the SQL table name for a model
|
|
1584
|
+
*/
|
|
1585
|
+
declare function getTableName(model: AnyModel, modelName: string): string;
|
|
1586
|
+
//#endregion
|
|
1587
|
+
export { type AmbiguousChange, type AmbiguousChangeRequest, type AmbiguousChangeResolution, type AmbiguousColumnChange, type AmbiguousResolveChange, type AmbiguousTableChange, type AppliedMigration, type ApplyFullOptions, type ApplyOptions, type ApplyResult, type ChangeResolution, type ColumnDef, DEFAULT_MIGRATIONS_DIR, DEFAULT_TABLE_NAME, type DestructiveResolveChange, type Dialect, type DiffOperation, type DiffResult, type DownOptions, type DownResult, type EnumDef, type EnumValueRemoval, type EnumValueRemovalChange, type EnumValueRemovalRequest, type EnumValueRemovalResolution, type EnumValueResolution, type EnumValueResolver, type ForeignKeyDef, type GenerateOptions, type GenerateResult, type IndexDef, type MigrationClient, type MigrationClientOptions, MigrationContext, type MigrationContextOptions, type MigrationEntry, MigrationError, type MigrationStatus, MigrationStorageDriver, type Migrations, type PrimaryKeyDef, type PushOptions, type PushResult, type ReferentialAction, type ResetOptions, type ResetResult, type ResolutionRequest, type ResolutionRequestType, type ResolutionResult, type ResolveCallback, type ResolveChange, type ResolveResult, type Resolver, type SchemaSnapshot, type SerializeOptions, type SquashOptions, type SquashResult, type TableDef, type UnifiedResolver, type UniqueConstraintDef, addDropResolver, alwaysAddDropResolver, alwaysRenameResolver, apply, applyResolutions, createAmbiguousChange, createAmbiguousChangeRequest, createDestructiveChange, createEnumValueRemovalChange, createEnumValueRemovalRequest, createMigrationClient, createPredefinedResolver, createResolver, createUnifiedResolver, diff, down, formatAmbiguousChange, formatAmbiguousChanges, formatMigrationFilename, formatOperation, formatOperations, generate, generateDDL, generateMigrationName, getColumnName, getDestructiveOperationDescriptions, getTableName, hasDestructiveOperations, introspect, isMigrationError, lenientResolver, normalizeDialect, pending, preview, push, rejectAllResolver, reset, resolveAmbiguousChanges, rollback, serializeModels, sortOperations, squash, status, strictResolver };
|
|
1588
|
+
//# sourceMappingURL=migrations.d.mts.map
|