rake-db 2.25.17 → 2.26.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/dist/index.d.ts CHANGED
@@ -1,7 +1,11 @@
1
- import * as orchid_core from 'orchid-core';
2
- import { MaybeArray, RawSQLBase, ColumnDataCheckBase, RecordString, ColumnTypeBase, EmptyObject, ColumnSchemaConfig, QueryLogObject, QueryLogOptions, MaybePromise, RecordOptionalString, ForeignKeyTable, SingleSql } from 'orchid-core';
3
1
  import * as pqb from 'pqb';
4
- import { TableData, ColumnsShape, NoPrimaryKeyOption, ColumnType, EnumColumn, raw, Adapter, DbResult, TransactionAdapter, TableDataFn, TableDataItem, DbDomainArg, Db, QueryArraysResult, AdapterOptions, DefaultColumnTypes, DefaultSchemaConfig, SearchWeight, ColumnsByType, DbStructureDomainsMap } from 'pqb';
2
+ import { ColumnsShape, Db, TableData, NoPrimaryKeyOption, ColumnType, EnumColumn, DefaultColumnTypes, DefaultSchemaConfig, AdapterOptions, DbResult, Adapter, TransactionAdapter, TableDataFn, TableDataItem, DbDomainArg, raw, SearchWeight, ColumnsByType, DbStructureDomainsMap } from 'pqb';
3
+ import * as orchid_core from 'orchid-core';
4
+ import { MaybeArray, RawSQLBase, ColumnDataCheckBase, RecordString, ColumnTypeBase, EmptyObject, ColumnSchemaConfig, QueryLogOptions, MaybePromise, QueryLogObject, QueryBase } from 'orchid-core';
5
+
6
+ interface CreateTableResult<Table extends string, Shape extends ColumnsShape> {
7
+ table: Db<Table, Shape>;
8
+ }
5
9
 
6
10
  type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameType | RakeDbAst.Schema | RakeDbAst.RenameSchema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.EnumValues | RakeDbAst.RenameEnumValues | RakeDbAst.ChangeEnumValues | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.RenameTableItem | RakeDbAst.View;
7
11
  declare namespace RakeDbAst {
@@ -266,6 +270,206 @@ declare const tableChangeMethods: {
266
270
  type TableChanger<CT> = MigrationColumnTypes<CT> & TableChangeMethods;
267
271
  type TableChangeData = Record<string, RakeDbAst.ChangeTableItem.Column | RakeDbAst.ChangeTableItem.Rename | Change | SpecialChange | ColumnTypeBase>;
268
272
 
273
+ interface RakeDbCtx {
274
+ migrationsPromise?: Promise<MigrationsSet>;
275
+ }
276
+ declare const getSchemaAndTableFromName: (name: string) => [string | undefined, string];
277
+ declare const concatSchemaAndName: ({ schema, name, }: {
278
+ schema?: string;
279
+ name: string;
280
+ }) => string;
281
+
282
+ interface MigrationItemHasLoad {
283
+ path?: string;
284
+ /**
285
+ * Function that loads the migration content,
286
+ * can store lazy import of a migration file.
287
+ * Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
288
+ */
289
+ load(): Promise<unknown>;
290
+ }
291
+ interface MigrationItem extends MigrationItemHasLoad {
292
+ path: string;
293
+ version: string;
294
+ }
295
+ interface MigrationsSet {
296
+ renameTo?: RakeDbRenameMigrations;
297
+ migrations: MigrationItem[];
298
+ }
299
+
300
+ interface CommandFn<SchemaConfig extends ColumnSchemaConfig, CT> {
301
+ (options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args: string[]): void | Promise<void>;
302
+ }
303
+ interface RakeDbBaseConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends QueryLogOptions {
304
+ schemaConfig: SchemaConfig;
305
+ migrationsPath: string;
306
+ migrationId: RakeDbMigrationId;
307
+ migrations?: ModuleExportsRecord;
308
+ renameMigrations?: RakeDbRenameMigrationsInput;
309
+ migrationsTable: string;
310
+ snakeCase: boolean;
311
+ language?: string;
312
+ commands: Record<string, CommandFn<SchemaConfig, CT>>;
313
+ noPrimaryKey?: NoPrimaryKeyOption;
314
+ baseTable?: RakeDbBaseTable<CT>;
315
+ forceDefaultExports?: boolean;
316
+ import(path: string): Promise<unknown>;
317
+ beforeChange?: ChangeCallback$1;
318
+ afterChange?: ChangeCallback$1;
319
+ afterChangeCommit?: ChangeCommitCallback;
320
+ beforeMigrate?: MigrationCallback;
321
+ afterMigrate?: MigrationCallback;
322
+ beforeRollback?: MigrationCallback;
323
+ afterRollback?: MigrationCallback;
324
+ }
325
+ interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends RakeDbBaseConfig<SchemaConfig, CT> {
326
+ columnTypes: CT;
327
+ basePath: string;
328
+ dbScript: string;
329
+ recurrentPath: string;
330
+ }
331
+ interface InputRakeDbConfigBase<SchemaConfig extends ColumnSchemaConfig, CT> extends QueryLogOptions {
332
+ columnTypes?: CT | ((t: DefaultColumnTypes<DefaultSchemaConfig>) => CT);
333
+ baseTable?: RakeDbBaseTable<CT>;
334
+ schemaConfig?: SchemaConfig;
335
+ basePath?: string;
336
+ dbScript?: string;
337
+ migrationsPath?: string;
338
+ migrationId?: 'serial' | RakeDbMigrationId;
339
+ recurrentPath?: string;
340
+ migrationsTable?: string;
341
+ snakeCase?: boolean;
342
+ language?: string;
343
+ commands?: Record<string, (options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args: string[]) => void | Promise<void>>;
344
+ noPrimaryKey?: NoPrimaryKeyOption;
345
+ forceDefaultExports?: boolean;
346
+ /**
347
+ * Is called once per db before migrating or rolling back a set of migrations.
348
+ *
349
+ * @param arg.db - query builder
350
+ * @param arg.up - whether it's migrating up or down
351
+ * @param arg.redo - whether it's migrating down and then up for `redo` command
352
+ * @param arg.migrations - array of executed (up or down) migrations
353
+ */
354
+ beforeChange?: ChangeCallback$1;
355
+ /**
356
+ * Is called once per db after migrating or rolling back a set of migrations.
357
+ * Runs inside the same transaction as migrations,
358
+ * for running after commit use {@link afterChangeCommit}.
359
+ *
360
+ * @param arg.db - query builder
361
+ * @param arg.up - whether it's migrating up or down
362
+ * @param arg.redo - whether it's migrating down and then up for `redo` command
363
+ * @param arg.migrations - array of executed (up or down) migrations
364
+ */
365
+ afterChange?: ChangeCallback$1;
366
+ /**
367
+ * Is called once per db after migrating or rolling back a set of migrations.
368
+ * Runs **after** committing migrations transaction.
369
+ *
370
+ * @param arg.options - database connection options
371
+ * @param arg.up - whether it's migrating up or down
372
+ * @param arg.migrations - array of executed (up or down) migrations
373
+ */
374
+ afterChangeCommit?: ChangeCommitCallback;
375
+ /**
376
+ * Is called once per db before migrating (up) a set of migrations.
377
+ *
378
+ * @param arg.db - query builder
379
+ * @param arg.migrations - applied migrations
380
+ */
381
+ beforeMigrate?: MigrationCallback;
382
+ /**
383
+ * Is called once per db after migrating (up) a set of migrations.
384
+ *
385
+ * @param arg.db - query builder
386
+ * @param arg.migrations - applied migrations
387
+ */
388
+ afterMigrate?: MigrationCallback;
389
+ /**
390
+ * Is called once per db before rolling back a set of migrations.
391
+ *
392
+ * @param arg.db - query builder
393
+ * @param arg.migrations - rolled back migrations
394
+ */
395
+ beforeRollback?: MigrationCallback;
396
+ /**
397
+ * Is called once per db before rolling back a set of migrations.
398
+ *
399
+ * @param arg.db - query builder
400
+ * @param arg.migrations - rolled back migrations
401
+ */
402
+ afterRollback?: MigrationCallback;
403
+ }
404
+ interface InputRakeDbConfigFileBased<SchemaConfig extends ColumnSchemaConfig, CT> extends InputRakeDbConfigBase<SchemaConfig, CT> {
405
+ /**
406
+ * It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
407
+ */
408
+ import(path: string): Promise<unknown>;
409
+ }
410
+ interface InputRakeDbConfigCodeBased<SchemaConfig extends ColumnSchemaConfig, CT> extends InputRakeDbConfigBase<SchemaConfig, CT> {
411
+ /**
412
+ * To specify array of migrations explicitly, without loading them from files.
413
+ */
414
+ migrations: ModuleExportsRecord;
415
+ renameMigrations?: RakeDbRenameMigrationsInput;
416
+ /**
417
+ * It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
418
+ */
419
+ import?(path: string): Promise<unknown>;
420
+ }
421
+ type InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> = InputRakeDbConfigFileBased<SchemaConfig, CT> | InputRakeDbConfigCodeBased<SchemaConfig, CT>;
422
+ interface ChangeCallback$1 {
423
+ (arg: {
424
+ db: DbResult<unknown>;
425
+ up: boolean;
426
+ redo: boolean;
427
+ migrations: MigrationItem[];
428
+ }): void | Promise<void>;
429
+ }
430
+ interface ChangeCommitCallback {
431
+ (arg: {
432
+ options: AdapterOptions;
433
+ up: boolean;
434
+ migrations: MigrationItem[];
435
+ }): void | Promise<void>;
436
+ }
437
+ interface MigrationCallback {
438
+ (arg: {
439
+ db: DbResult<unknown>;
440
+ migrations: MigrationItem[];
441
+ }): void | Promise<void>;
442
+ }
443
+ type AnyRakeDbConfig = RakeDbConfig<any, any>;
444
+ interface RakeDbBaseTable<CT> {
445
+ exportAs: string;
446
+ getFilePath(): string;
447
+ nowSQL?: string;
448
+ new (): {
449
+ types: CT;
450
+ snakeCase?: boolean;
451
+ language?: string;
452
+ };
453
+ }
454
+ interface ModuleExportsRecord {
455
+ [K: string]: () => Promise<unknown>;
456
+ }
457
+ type RakeDbMigrationId = 'timestamp' | {
458
+ serial: number;
459
+ };
460
+ interface RakeDbRenameMigrationsMap {
461
+ [K: string]: number;
462
+ }
463
+ interface RakeDbRenameMigrations {
464
+ to: RakeDbMigrationId;
465
+ map(): MaybePromise<RakeDbRenameMigrationsMap>;
466
+ }
467
+ interface RakeDbRenameMigrationsInput {
468
+ to: RakeDbMigrationId;
469
+ map: RakeDbRenameMigrationsMap;
470
+ }
471
+ declare const migrationConfigDefaults: RakeDbBaseConfig<ColumnSchemaConfig>;
472
+
269
473
  type DropMode = 'CASCADE' | 'RESTRICT';
270
474
  type TableOptions = {
271
475
  createIfNotExists?: boolean;
@@ -288,10 +492,6 @@ type ChangeTableOptions = {
288
492
  comment?: string | [string, string] | null;
289
493
  };
290
494
  type ChangeTableCallback<CT> = (t: TableChanger<CT>) => TableChangeData;
291
- type ColumnComment = {
292
- column: string;
293
- comment: string | null;
294
- };
295
495
  type SilentQueries = {
296
496
  silentQuery: Adapter['query'];
297
497
  silentArrays: Adapter['arrays'];
@@ -1214,280 +1414,98 @@ declare class Migration<CT> {
1214
1414
  */
1215
1415
  constraintExists(constraintName: string): Promise<boolean>;
1216
1416
  }
1217
- declare const renameType: (migration: Migration<unknown>, from: string, to: string, kind: RakeDbAst.RenameType['kind']) => Promise<void>;
1218
1417
  interface AddEnumValueOptions {
1219
1418
  ifNotExists?: boolean;
1220
1419
  before?: string;
1221
1420
  after?: string;
1222
1421
  }
1223
- declare const addOrDropEnumValues: (migration: Migration<unknown>, up: boolean, enumName: string, values: string[], options?: AddEnumValueOptions) => Promise<void>;
1224
- declare const changeEnumValues: (migration: Migration<unknown>, enumName: string, fromValues: string[], toValues: string[]) => Promise<void>;
1225
1422
 
1226
- interface TableQuery {
1227
- text: string;
1228
- values?: unknown[];
1229
- then?(result: QueryArraysResult): void;
1230
- }
1231
- interface CreateTableResult<Table extends string, Shape extends ColumnsShape> {
1232
- table: Db<Table, Shape>;
1423
+ interface MigrationChange {
1424
+ fn: ChangeCallback<unknown>;
1425
+ config: RakeDbConfig<ColumnSchemaConfig, unknown>;
1233
1426
  }
1427
+ type ChangeCallback<CT> = (db: DbMigration<CT>, up: boolean) => Promise<void>;
1234
1428
 
1235
- interface RakeDbCtx {
1236
- migrationsPromise?: Promise<MigrationsSet>;
1237
- }
1238
- declare const getFirstWordAndRest: (input: string) => [
1239
- string
1240
- ] | [
1241
- string,
1242
- string
1243
- ];
1244
- declare const getTextAfterTo: (input: string) => string | undefined;
1245
- declare const getTextAfterFrom: (input: string) => string | undefined;
1246
- declare const joinWords: (...words: string[]) => string;
1247
- declare const joinColumns: (columns: string[]) => string;
1248
- declare const quoteWithSchema: ({ schema, name, }: {
1249
- schema?: string;
1250
- name: string;
1251
- }) => string;
1252
- declare const quoteTable: (schema: string | undefined, table: string) => string;
1253
- declare const getSchemaAndTableFromName: (name: string) => [string | undefined, string];
1254
- declare const quoteNameFromString: (string: string) => string;
1255
1429
  /**
1256
- * Do not quote the type itself because it can be an expression like `geography(point)` for postgis.
1430
+ * Type of {@link rakeDb} function
1257
1431
  */
1258
- declare const quoteCustomType: (s: string) => string;
1259
- declare const quoteSchemaTable: (arg: {
1260
- schema?: string;
1261
- name: string;
1262
- }) => string;
1263
- declare const concatSchemaAndName: ({ schema, name, }: {
1264
- schema?: string;
1265
- name: string;
1266
- }) => string;
1267
- declare const makePopulateEnumQuery: (item: EnumColumn<ColumnSchemaConfig, unknown, readonly string[]>) => TableQuery;
1268
- declare const transaction: <T>(adapter: Adapter, fn: (trx: TransactionAdapter) => Promise<T>) => Promise<T>;
1269
- declare const queryLock: (trx: TransactionAdapter) => Promise<pqb.QueryResult<any>>;
1270
- declare const exhaustive: (_: never) => never;
1271
- declare const pluralize: (w: string, count: number, append?: string) => string;
1272
-
1273
- interface MigrationItem {
1274
- path: string;
1275
- version: string;
1432
+ interface RakeDbFn {
1433
+ <SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>>(options: MaybeArray<AdapterOptions>, partialConfig: InputRakeDbConfig<SchemaConfig, CT>, args?: string[]): RakeDbChangeFnWithPromise<CT>;
1276
1434
  /**
1277
- * Function that loads the migration content,
1278
- * can store lazy import of a migration file.
1279
- * Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
1435
+ * Unlike the original `rakeDb` that executes immediately,
1436
+ * `rakeDb.lazy` returns the `run` function to be later called programmatically.
1437
+ *
1438
+ * @param options - {@link AdapterOptions} or an array of such options to migrate multiple dbs
1439
+ * @param config - {@link RakeDbConfig}
1440
+ * @returns `change` is to be used in migrations, `run` takes an array cli args to execute a command
1280
1441
  */
1281
- load(): Promise<unknown>;
1282
- }
1283
- interface MigrationsSet {
1284
- renameTo?: RakeDbRenameMigrations;
1285
- migrations: MigrationItem[];
1442
+ lazy<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>>(options: MaybeArray<AdapterOptions>, config: InputRakeDbConfig<SchemaConfig, CT>): {
1443
+ change: RakeDbChangeFn<CT>;
1444
+ run(args: string[], config?: Partial<RakeDbConfig<SchemaConfig, CT>>): Promise<RakeDbResult>;
1445
+ };
1286
1446
  }
1287
-
1288
- interface CommandFn<SchemaConfig extends ColumnSchemaConfig, CT> {
1289
- (options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args: string[]): void | Promise<void>;
1447
+ interface RakeDbResult {
1448
+ options: AdapterOptions[];
1449
+ config: AnyRakeDbConfig;
1450
+ args: string[];
1290
1451
  }
1291
- interface RakeDbBaseConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends QueryLogOptions {
1292
- schemaConfig: SchemaConfig;
1293
- migrationsPath: string;
1294
- migrationId: RakeDbMigrationId;
1295
- migrations?: ModuleExportsRecord;
1296
- renameMigrations?: RakeDbRenameMigrationsInput;
1297
- migrationsTable: string;
1298
- snakeCase: boolean;
1299
- language?: string;
1300
- commands: Record<string, CommandFn<SchemaConfig, CT>>;
1301
- noPrimaryKey?: NoPrimaryKeyOption;
1302
- baseTable?: RakeDbBaseTable<CT>;
1303
- forceDefaultExports?: boolean;
1304
- import(path: string): Promise<unknown>;
1305
- beforeChange?: ChangeCallback$1;
1306
- afterChange?: ChangeCallback$1;
1307
- afterChangeCommit?: ChangeCommitCallback;
1308
- beforeMigrate?: MigrationCallback;
1309
- afterMigrate?: MigrationCallback;
1310
- beforeRollback?: MigrationCallback;
1311
- afterRollback?: MigrationCallback;
1312
- }
1313
- interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends RakeDbBaseConfig<SchemaConfig, CT> {
1314
- columnTypes: CT;
1315
- basePath: string;
1316
- dbScript: string;
1317
- recurrentPath: string;
1318
- }
1319
- interface InputRakeDbConfigBase<SchemaConfig extends ColumnSchemaConfig, CT> extends QueryLogOptions {
1320
- columnTypes?: CT | ((t: DefaultColumnTypes<DefaultSchemaConfig>) => CT);
1321
- baseTable?: RakeDbBaseTable<CT>;
1322
- schemaConfig?: SchemaConfig;
1323
- basePath?: string;
1324
- dbScript?: string;
1325
- migrationsPath?: string;
1326
- migrationId?: 'serial' | RakeDbMigrationId;
1327
- recurrentPath?: string;
1328
- migrationsTable?: string;
1329
- snakeCase?: boolean;
1330
- language?: string;
1331
- commands?: Record<string, (options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args: string[]) => void | Promise<void>>;
1332
- noPrimaryKey?: NoPrimaryKeyOption;
1333
- forceDefaultExports?: boolean;
1334
- /**
1335
- * Is called once per db before migrating or rolling back a set of migrations.
1336
- *
1337
- * @param arg.db - query builder
1338
- * @param arg.up - whether it's migrating up or down
1339
- * @param arg.redo - whether it's migrating down and then up for `redo` command
1340
- * @param arg.migrations - array of executed (up or down) migrations
1341
- */
1342
- beforeChange?: ChangeCallback$1;
1343
- /**
1344
- * Is called once per db after migrating or rolling back a set of migrations.
1345
- * Runs inside the same transaction as migrations,
1346
- * for running after commit use {@link afterChangeCommit}.
1347
- *
1348
- * @param arg.db - query builder
1349
- * @param arg.up - whether it's migrating up or down
1350
- * @param arg.redo - whether it's migrating down and then up for `redo` command
1351
- * @param arg.migrations - array of executed (up or down) migrations
1352
- */
1353
- afterChange?: ChangeCallback$1;
1354
- /**
1355
- * Is called once per db after migrating or rolling back a set of migrations.
1356
- * Runs **after** committing migrations transaction.
1357
- *
1358
- * @param arg.options - database connection options
1359
- * @param arg.up - whether it's migrating up or down
1360
- * @param arg.migrations - array of executed (up or down) migrations
1361
- */
1362
- afterChangeCommit?: ChangeCommitCallback;
1363
- /**
1364
- * Is called once per db before migrating (up) a set of migrations.
1365
- *
1366
- * @param arg.db - query builder
1367
- * @param arg.migrations - applied migrations
1368
- */
1369
- beforeMigrate?: MigrationCallback;
1370
- /**
1371
- * Is called once per db after migrating (up) a set of migrations.
1372
- *
1373
- * @param arg.db - query builder
1374
- * @param arg.migrations - applied migrations
1375
- */
1376
- afterMigrate?: MigrationCallback;
1377
- /**
1378
- * Is called once per db before rolling back a set of migrations.
1379
- *
1380
- * @param arg.db - query builder
1381
- * @param arg.migrations - rolled back migrations
1382
- */
1383
- beforeRollback?: MigrationCallback;
1384
- /**
1385
- * Is called once per db before rolling back a set of migrations.
1386
- *
1387
- * @param arg.db - query builder
1388
- * @param arg.migrations - rolled back migrations
1389
- */
1390
- afterRollback?: MigrationCallback;
1391
- }
1392
- interface InputRakeDbConfigFileBased<SchemaConfig extends ColumnSchemaConfig, CT> extends InputRakeDbConfigBase<SchemaConfig, CT> {
1393
- /**
1394
- * It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
1395
- */
1396
- import(path: string): Promise<unknown>;
1397
- }
1398
- interface InputRakeDbConfigCodeBased<SchemaConfig extends ColumnSchemaConfig, CT> extends InputRakeDbConfigBase<SchemaConfig, CT> {
1399
- /**
1400
- * To specify array of migrations explicitly, without loading them from files.
1401
- */
1402
- migrations: ModuleExportsRecord;
1403
- renameMigrations?: RakeDbRenameMigrationsInput;
1404
- /**
1405
- * It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
1406
- */
1407
- import?(path: string): Promise<unknown>;
1452
+ /**
1453
+ * Function to use in migrations to wrap database changes
1454
+ * Saves the given callback to an internal queue,
1455
+ * and also returns the callback in case you want to export it from migration.
1456
+ */
1457
+ interface RakeDbChangeFn<CT> {
1458
+ (fn: ChangeCallback<CT>): MigrationChange;
1408
1459
  }
1409
- type InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> = InputRakeDbConfigFileBased<SchemaConfig, CT> | InputRakeDbConfigCodeBased<SchemaConfig, CT>;
1410
- interface ChangeCallback$1 {
1411
- (arg: {
1412
- db: DbResult<unknown>;
1413
- up: boolean;
1414
- redo: boolean;
1415
- migrations: MigrationItem[];
1416
- }): void | Promise<void>;
1460
+ interface RakeDbChangeFnWithPromise<CT> extends RakeDbChangeFn<CT> {
1461
+ promise: Promise<RakeDbResult>;
1417
1462
  }
1418
- interface ChangeCommitCallback {
1419
- (arg: {
1420
- options: AdapterOptions;
1421
- up: boolean;
1422
- migrations: MigrationItem[];
1423
- }): void | Promise<void>;
1463
+ /**
1464
+ * Function to configure and run `rakeDb`.
1465
+ *
1466
+ * @param options - {@link AdapterOptions} or an array of such options to migrate multiple dbs
1467
+ * @param config - {@link RakeDbConfig}
1468
+ * @param args - optionally provide an array of cli args. Default is `process.argv.slice(2)`.
1469
+ */
1470
+ declare const rakeDb: RakeDbFn;
1471
+ interface RakeDbCommand {
1472
+ run(options: AdapterOptions[], config: AnyRakeDbConfig, args: string[]): MaybePromise<unknown>;
1473
+ help: string;
1474
+ helpArguments?: RecordString;
1475
+ helpAfter?: string;
1424
1476
  }
1425
- interface MigrationCallback {
1426
- (arg: {
1427
- db: DbResult<unknown>;
1428
- migrations: MigrationItem[];
1429
- }): void | Promise<void>;
1477
+ interface RakeDbCommands {
1478
+ [K: string]: RakeDbCommand;
1430
1479
  }
1431
- type AnyRakeDbConfig = RakeDbConfig<any, any>;
1432
- interface RakeDbBaseTable<CT> {
1433
- exportAs: string;
1434
- getFilePath(): string;
1435
- nowSQL?: string;
1436
- new (): {
1437
- types: CT;
1438
- snakeCase?: boolean;
1439
- language?: string;
1480
+ declare const rakeDbCommands: RakeDbCommands;
1481
+
1482
+ declare const encodeColumnDefault: (def: unknown, values: unknown[], column?: ColumnTypeBase) => string | null;
1483
+ declare const getConstraintName: (table: string, constraint: {
1484
+ references?: {
1485
+ columns: string[];
1440
1486
  };
1487
+ check?: unknown;
1488
+ identity?: unknown;
1489
+ }, snakeCase: boolean | undefined) => string;
1490
+ interface GetIndexOrExcludeName {
1491
+ (table: string, columns: ({
1492
+ column?: string;
1493
+ } | {
1494
+ expression: string;
1495
+ })[]): string;
1441
1496
  }
1442
- interface ModuleExportsRecord {
1443
- [K: string]: () => Promise<unknown>;
1444
- }
1445
- type RakeDbMigrationId = 'timestamp' | {
1446
- serial: number;
1447
- };
1448
- interface RakeDbRenameMigrationsMap {
1449
- [K: string]: number;
1450
- }
1451
- interface RakeDbRenameMigrations {
1452
- to: RakeDbMigrationId;
1453
- map(): MaybePromise<RakeDbRenameMigrationsMap>;
1454
- }
1455
- interface RakeDbRenameMigrationsInput {
1456
- to: RakeDbMigrationId;
1457
- map: RakeDbRenameMigrationsMap;
1458
- }
1459
- declare const migrationConfigDefaults: RakeDbBaseConfig<ColumnSchemaConfig>;
1460
- declare const processRakeDbConfig: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(config: InputRakeDbConfig<SchemaConfig, CT>) => RakeDbConfig<SchemaConfig, CT>;
1461
- declare const getDatabaseAndUserFromOptions: (options: AdapterOptions) => {
1462
- database: string;
1463
- user: string;
1464
- };
1465
-
1466
- declare const createDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1467
- declare const dropDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1468
- declare const resetDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1469
-
1470
- declare const writeMigrationFile: (config: AnyRakeDbConfig, version: string, name: string, migrationCode: string) => Promise<void>;
1471
- declare const newMigration: (config: AnyRakeDbConfig, [name]: string[]) => Promise<void>;
1472
- declare const makeFileVersion: (ctx: RakeDbCtx, config: AnyRakeDbConfig) => Promise<string>;
1473
- declare const generateTimeStamp: () => string;
1497
+ declare const getIndexName: GetIndexOrExcludeName;
1498
+ declare const getExcludeName: GetIndexOrExcludeName;
1474
1499
 
1475
- type ChangeCallback<CT> = (db: DbMigration<CT>, up: boolean) => Promise<void>;
1476
- declare const clearChanges: () => void;
1477
- declare const getCurrentChanges: () => ChangeCallback<unknown>[];
1478
- declare const pushChange: (fn: ChangeCallback<unknown>) => number;
1500
+ declare const promptSelect: ({ message, options, active, inactive, }: {
1501
+ message: string;
1502
+ options: string[];
1503
+ active?: (s: string) => string;
1504
+ inactive?: (s: string) => string;
1505
+ }) => Promise<number>;
1479
1506
 
1480
1507
  declare const saveMigratedVersion: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(db: SilentQueries, version: string, name: string, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1481
- declare const deleteMigratedVersion: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(db: SilentQueries, version: string, name: string, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1482
- declare class NoMigrationsTableError extends Error {
1483
- }
1484
- type RakeDbAppliedVersions = {
1485
- map: RecordOptionalString;
1486
- sequence: number[];
1487
- };
1488
- declare const getMigratedVersionsMap: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(ctx: RakeDbCtx, adapter: Adapter | TransactionAdapter, config: RakeDbConfig<SchemaConfig, CT>, renameTo?: RakeDbRenameMigrations) => Promise<RakeDbAppliedVersions>;
1489
1508
 
1490
- declare const RAKE_DB_LOCK_KEY = "8582141715823621641";
1491
1509
  type MigrateFn = <SchemaConfig extends ColumnSchemaConfig, CT>(ctx: RakeDbCtx, options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args?: string[], adapters?: Adapter[], dontClose?: boolean) => Promise<Adapter[]>;
1492
1510
  /**
1493
1511
  * Will run all pending yet migrations, sequentially in order,
@@ -1497,22 +1515,7 @@ type MigrateFn = <SchemaConfig extends ColumnSchemaConfig, CT>(ctx: RakeDbCtx, o
1497
1515
  * @param config - specifies how to load migrations, callbacks, and logger
1498
1516
  * @param args - pass none or `all` to run all migrations, pass int for how many to migrate
1499
1517
  */
1500
- declare const migrate: MigrateFn;
1501
- /**
1502
- * Will roll back one latest applied migration,
1503
- * will apply `change` functions bottom-to-top.
1504
- *
1505
- * Takes the same options as {@link migrate}.
1506
- */
1507
- declare const rollback: MigrateFn;
1508
- /**
1509
- * Calls {@link rollback} and then {@link migrate}.
1510
- *
1511
- * Takes the same options as {@link migrate}.
1512
- */
1513
- declare const redo: MigrateFn;
1514
- declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, unknown>, set: MigrationsSet, versions: RakeDbAppliedVersions, count: number, up: boolean, redo: boolean, force?: boolean, skipLock?: boolean) => Promise<MigrationItem[]>;
1515
- declare const changeCache: Record<string, ChangeCallback<unknown>[] | undefined>;
1518
+ declare const fullMigrate: MigrateFn;
1516
1519
 
1517
1520
  declare namespace DbStructure {
1518
1521
  interface TableNameAndSchemaName {
@@ -1678,6 +1681,8 @@ interface IntrospectedStructure {
1678
1681
  }
1679
1682
  declare function introspectDbSchema(db: Adapter): Promise<IntrospectedStructure>;
1680
1683
 
1684
+ declare const astToMigration: (currentSchema: string, config: AnyRakeDbConfig, asts: RakeDbAst[]) => string | undefined;
1685
+
1681
1686
  interface StructureToAstCtx {
1682
1687
  snakeCase?: boolean;
1683
1688
  unsupportedTypes: Record<string, string[]>;
@@ -1697,151 +1702,19 @@ declare const makeDomainsMap: (ctx: StructureToAstCtx, data: IntrospectedStructu
1697
1702
  declare const instantiateDbColumn: (ctx: StructureToAstCtx, data: IntrospectedStructure, domains: DbStructureDomainsMap, dbColumn: DbStructure.Column) => ColumnType<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any>;
1698
1703
  declare const tableToAst: (ctx: StructureToAstCtx, data: IntrospectedStructure, table: DbStructure.Table, action: 'create' | 'drop', domains: DbStructureDomainsMap) => RakeDbAst.Table;
1699
1704
  declare const getDbStructureTableData: (data: IntrospectedStructure, { name, schemaName }: DbStructure.Table) => StructureToAstTableData;
1700
- declare const makeDbStructureColumnsShape: (ctx: StructureToAstCtx, data: IntrospectedStructure, domains: DbStructureDomainsMap, table: DbStructure.Table | DbStructure.View, tableData?: StructureToAstTableData) => ColumnsShape;
1701
1705
  interface ColumnChecks {
1702
1706
  [K: string]: string[];
1703
1707
  }
1704
1708
  declare const getDbTableColumnsChecks: (tableData: StructureToAstTableData) => ColumnChecks;
1705
1709
  declare const dbColumnToAst: (ctx: StructureToAstCtx, data: IntrospectedStructure, domains: DbStructureDomainsMap, tableName: string, item: DbStructure.Column, table?: DbStructure.Table, tableData?: StructureToAstTableData, checks?: ColumnChecks) => [key: string, column: ColumnType];
1706
1710
 
1707
- declare const astToMigration: (currentSchema: string, config: AnyRakeDbConfig, asts: RakeDbAst[]) => string | undefined;
1708
-
1709
- declare const versionToString: (config: AnyRakeDbConfig, version: number) => string;
1710
- declare const columnTypeToSql: (item: ColumnTypeBase) => string;
1711
- declare const getColumnName: (item: {
1712
- data: {
1713
- name?: string;
1714
- };
1715
- }, key: string, snakeCase: boolean | undefined) => string;
1716
- declare const columnToSql: (name: string, item: ColumnType, values: unknown[], hasMultiplePrimaryKeys: boolean, snakeCase: boolean | undefined) => string;
1717
- declare const encodeColumnDefault: (def: unknown, values: unknown[], column?: ColumnTypeBase) => string | null;
1718
- declare const identityToSql: (identity: TableData.Identity) => string;
1719
- declare const addColumnIndex: (indexes: TableData.Index[], name: string, item: ColumnType) => void;
1720
- declare const addColumnExclude: (excludes: TableData.Exclude[], name: string, item: ColumnType) => void;
1721
- declare const addColumnComment: (comments: ColumnComment[], name: string, item: ColumnType) => void;
1722
- declare const getForeignKeyTable: (fnOrTable: (() => ForeignKeyTable) | string) => [string | undefined, string];
1723
- declare const getConstraintName: (table: string, constraint: {
1724
- references?: {
1725
- columns: string[];
1726
- };
1727
- check?: unknown;
1728
- identity?: unknown;
1729
- }, snakeCase: boolean | undefined) => string;
1730
- declare const constraintToSql: ({ name }: {
1731
- schema?: string;
1732
- name: string;
1733
- }, up: boolean, constraint: TableData.Constraint, values: unknown[], snakeCase: boolean | undefined) => string;
1734
- declare const referencesToSql: (references: TableData.References, snakeCase: boolean | undefined) => string;
1735
- interface GetIndexOrExcludeName {
1736
- (table: string, columns: ({
1737
- column?: string;
1738
- } | {
1739
- expression: string;
1740
- })[]): string;
1741
- }
1742
- declare const getIndexName: GetIndexOrExcludeName;
1743
- declare const getExcludeName: GetIndexOrExcludeName;
1744
- declare const indexesToQuery: (up: boolean, { schema, name: tableName }: {
1745
- schema?: string;
1746
- name: string;
1747
- }, indexes: TableData.Index[], snakeCase: boolean | undefined, language?: string) => SingleSql[];
1748
- declare const excludesToQuery: (up: boolean, { schema, name: tableName }: {
1749
- schema?: string;
1750
- name: string;
1751
- }, excludes: TableData.Exclude[], snakeCase: boolean | undefined) => SingleSql[];
1752
- declare const commentsToQuery: (schemaTable: {
1753
- schema?: string;
1754
- name: string;
1755
- }, comments: ColumnComment[]) => SingleSql[];
1756
- declare const primaryKeyToSql: (primaryKey: Exclude<TableData['primaryKey'], undefined>) => string;
1757
- declare const interpolateSqlValues: ({ text, values }: TableQuery) => string;
1758
- interface ColumnNamedCheck extends ColumnDataCheckBase {
1759
- name: string;
1760
- }
1761
- declare const nameColumnChecks: (table: string, column: string, checks: ColumnDataCheckBase[]) => ColumnNamedCheck[];
1762
- declare const cmpRawSql: (a: RawSQLBase, b: RawSQLBase) => boolean;
1711
+ declare const writeMigrationFile: (config: AnyRakeDbConfig, version: string, name: string, migrationCode: string) => Promise<void>;
1712
+ declare const makeFileVersion: (ctx: RakeDbCtx, config: AnyRakeDbConfig) => Promise<string>;
1763
1713
 
1764
- /**
1765
- * Type of {@link rakeDb} function
1766
- */
1767
- interface RakeDbFn {
1768
- <SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>>(options: MaybeArray<AdapterOptions>, partialConfig: InputRakeDbConfig<SchemaConfig, CT>, args?: string[]): RakeDbChangeFnWithPromise<CT>;
1769
- /**
1770
- * Unlike the original `rakeDb` that executes immediately,
1771
- * `rakeDb.lazy` returns the `run` function to be later called programmatically.
1772
- *
1773
- * @param options - {@link AdapterOptions} or an array of such options to migrate multiple dbs
1774
- * @param config - {@link RakeDbConfig}
1775
- * @returns `change` is to be used in migrations, `run` takes an array cli args to execute a command
1776
- */
1777
- lazy<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>>(options: MaybeArray<AdapterOptions>, config: InputRakeDbConfig<SchemaConfig, CT>): {
1778
- change: RakeDbChangeFn<CT>;
1779
- run(args: string[], config?: Partial<RakeDbConfig<SchemaConfig, CT>>): Promise<RakeDbResult>;
1780
- };
1781
- }
1782
- interface RakeDbResult {
1783
- options: AdapterOptions[];
1784
- config: AnyRakeDbConfig;
1785
- args: string[];
1714
+ interface OrmParam {
1715
+ $qb: QueryBase;
1786
1716
  }
1787
- /**
1788
- * Function to use in migrations to wrap database changes
1789
- * Saves the given callback to an internal queue,
1790
- * and also returns the callback in case you want to export it from migration.
1791
- */
1792
- interface RakeDbChangeFn<CT> {
1793
- (fn: ChangeCallback<CT>): ChangeCallback<CT>;
1794
- }
1795
- interface RakeDbChangeFnWithPromise<CT> extends RakeDbChangeFn<CT> {
1796
- promise: Promise<RakeDbResult>;
1797
- }
1798
- /**
1799
- * Function to configure and run `rakeDb`.
1800
- *
1801
- * @param options - {@link AdapterOptions} or an array of such options to migrate multiple dbs
1802
- * @param config - {@link RakeDbConfig}
1803
- * @param args - optionally provide an array of cli args. Default is `process.argv.slice(2)`.
1804
- */
1805
- declare const rakeDb: RakeDbFn;
1806
- declare const rakeDbAliases: RecordOptionalString;
1807
- interface RakeDbCommand {
1808
- run(options: AdapterOptions[], config: AnyRakeDbConfig, args: string[]): MaybePromise<unknown>;
1809
- help: string;
1810
- helpArguments?: RecordString;
1811
- helpAfter?: string;
1812
- }
1813
- interface RakeDbCommands {
1814
- [K: string]: RakeDbCommand;
1815
- }
1816
- declare const rakeDbCommands: RakeDbCommands;
1817
-
1818
- declare const colors: {
1819
- yellow: (s: string) => string;
1820
- green: (s: string) => string;
1821
- red: (s: string) => string;
1822
- blue: (s: string) => string;
1823
- bright: (s: string) => string;
1824
- blueBold: (s: string) => string;
1825
- yellowBold: (s: string) => string;
1826
- greenBold: (s: string) => string;
1827
- pale: (s: string) => string;
1828
- };
1829
-
1830
- declare const promptSelect: ({ message, options, active, inactive, }: {
1831
- message: string;
1832
- options: string[];
1833
- active?: (s: string) => string;
1834
- inactive?: (s: string) => string;
1835
- }) => Promise<number>;
1836
- declare const promptConfirm: ({ message, }: {
1837
- message: string;
1838
- password?: boolean;
1839
- }) => Promise<boolean>;
1840
- declare const promptText: ({ message, default: def, password, min, }: {
1841
- message: string;
1842
- default?: string;
1843
- password?: boolean;
1844
- min?: number;
1845
- }) => Promise<string>;
1717
+ type UnknownPromiseFns = (() => Promise<unknown>)[];
1718
+ declare const migrateFiles: (db: OrmParam, files: UnknownPromiseFns) => Promise<void>;
1846
1719
 
1847
- export { type AnyRakeDbConfig, type ChangeCallback, type ChangeTableCallback, type ChangeTableOptions, type ColumnChecks, type ColumnComment, type ColumnNamedCheck, type ColumnsShapeCallback, type CommandFn, type DbMigration, DbStructure, type DropMode, type GetIndexOrExcludeName, type InputRakeDbConfig, type InputRakeDbConfigBase, type IntrospectedStructure, Migration, type MigrationAdapter, type MigrationColumnTypes, type ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, type RakeDbAppliedVersions, RakeDbAst, type RakeDbBaseTable, type RakeDbConfig, type RakeDbCtx, type RakeDbFn, type RakeDbMigrationId, type RakeDbRenameMigrations, type RakeDbRenameMigrationsInput, type RakeDbRenameMigrationsMap, type RakeDbResult, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, type TableOptions, addColumnComment, addColumnExclude, addColumnIndex, addOrDropEnumValues, astToMigration, changeCache, changeEnumValues, clearChanges, cmpRawSql, colors, columnToSql, columnTypeToSql, commentsToQuery, concatSchemaAndName, constraintToSql, createDb, createMigrationInterface, dbColumnToAst, deleteMigratedVersion, dropDb, encodeColumnDefault, excludesToQuery, exhaustive, generateTimeStamp, getColumnName, getConstraintName, getCurrentChanges, getDatabaseAndUserFromOptions, getDbStructureTableData, getDbTableColumnsChecks, getExcludeName, getFirstWordAndRest, getForeignKeyTable, getIndexName, getMigratedVersionsMap, getSchemaAndTableFromName, getTextAfterFrom, getTextAfterTo, identityToSql, indexesToQuery, instantiateDbColumn, interpolateSqlValues, introspectDbSchema, joinColumns, joinWords, makeDbStructureColumnsShape, makeDomainsMap, makeFileVersion, makePopulateEnumQuery, makeStructureToAstCtx, migrate, migrateOrRollback, migrationConfigDefaults, nameColumnChecks, newMigration, pluralize, primaryKeyToSql, processRakeDbConfig, promptConfirm, promptSelect, promptText, pushChange, queryLock, quoteCustomType, quoteNameFromString, quoteSchemaTable, quoteTable, quoteWithSchema, rakeDb, rakeDbAliases, rakeDbCommands, redo, referencesToSql, renameType, resetDb, rollback, saveMigratedVersion, structureToAst, tableToAst, transaction, versionToString, writeMigrationFile };
1720
+ export { type AnyRakeDbConfig, type ChangeCallback, type DbMigration, DbStructure, type InputRakeDbConfigBase, type IntrospectedStructure, RakeDbAst, type RakeDbChangeFn, type RakeDbChangeFnWithPromise, type RakeDbConfig, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, astToMigration, concatSchemaAndName, createMigrationInterface, dbColumnToAst, encodeColumnDefault, fullMigrate, getConstraintName, getDbStructureTableData, getDbTableColumnsChecks, getExcludeName, getIndexName, getSchemaAndTableFromName, instantiateDbColumn, introspectDbSchema, makeDomainsMap, makeFileVersion, makeStructureToAstCtx, migrateFiles, migrationConfigDefaults, promptSelect, rakeDb, rakeDbCommands, saveMigratedVersion, structureToAst, tableToAst, writeMigrationFile };