rake-db 2.20.7 → 2.20.9
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 +25 -8
- package/dist/index.js +139 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +140 -64
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as orchid_core from 'orchid-core';
|
|
2
|
-
import { MaybeArray, RawSQLBase, ColumnDataCheckBase, RecordString, EmptyObject, ColumnSchemaConfig, RecordOptionalString, ColumnTypeBase, ForeignKeyTable, Sql
|
|
2
|
+
import { MaybeArray, RawSQLBase, ColumnDataCheckBase, RecordString, EmptyObject, ColumnSchemaConfig, MaybePromise, RecordOptionalString, ColumnTypeBase, ForeignKeyTable, Sql } from 'orchid-core';
|
|
3
3
|
import * as pqb from 'pqb';
|
|
4
4
|
import { TableData, ColumnsShape, NoPrimaryKeyOption, ColumnType, EnumColumn, raw, Adapter, DbResult, TransactionAdapter, QueryLogObject, TableDataFn, TableDataItem, DbDomainArg, TextColumn, Db as Db$1, QueryArraysResult, AdapterOptions, DefaultColumnTypes, DefaultSchemaConfig, QueryLogOptions, SearchWeight, ColumnsByType, ArrayColumn } from 'pqb';
|
|
5
5
|
|
|
@@ -1247,7 +1247,7 @@ interface MigrationItem {
|
|
|
1247
1247
|
load(): Promise<unknown>;
|
|
1248
1248
|
}
|
|
1249
1249
|
interface MigrationsSet {
|
|
1250
|
-
renameTo?:
|
|
1250
|
+
renameTo?: RakeDbRenameMigrations;
|
|
1251
1251
|
migrations: MigrationItem[];
|
|
1252
1252
|
}
|
|
1253
1253
|
|
|
@@ -1260,6 +1260,7 @@ interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColu
|
|
|
1260
1260
|
migrationsPath: string;
|
|
1261
1261
|
migrationId: RakeDbMigrationId;
|
|
1262
1262
|
migrations?: ModuleExportsRecord;
|
|
1263
|
+
renameMigrations?: RakeDbRenameMigrationsInput;
|
|
1263
1264
|
recurrentPath: string;
|
|
1264
1265
|
migrationsTable: string;
|
|
1265
1266
|
snakeCase: boolean;
|
|
@@ -1284,7 +1285,7 @@ interface InputRakeDbConfigBase<SchemaConfig extends ColumnSchemaConfig, CT> ext
|
|
|
1284
1285
|
basePath?: string;
|
|
1285
1286
|
dbScript?: string;
|
|
1286
1287
|
migrationsPath?: string;
|
|
1287
|
-
migrationId?: RakeDbMigrationId;
|
|
1288
|
+
migrationId?: 'serial' | RakeDbMigrationId;
|
|
1288
1289
|
recurrentPath?: string;
|
|
1289
1290
|
migrationsTable?: string;
|
|
1290
1291
|
snakeCase?: boolean;
|
|
@@ -1360,6 +1361,7 @@ type InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> = InputRakeD
|
|
|
1360
1361
|
* To specify array of migrations explicitly, without loading them from files.
|
|
1361
1362
|
*/
|
|
1362
1363
|
migrations: ModuleExportsRecord;
|
|
1364
|
+
renameMigrations?: RakeDbRenameMigrationsInput;
|
|
1363
1365
|
/**
|
|
1364
1366
|
* It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
|
|
1365
1367
|
*/
|
|
@@ -1395,11 +1397,26 @@ interface RakeDbBaseTable<CT> {
|
|
|
1395
1397
|
interface ModuleExportsRecord {
|
|
1396
1398
|
[K: string]: () => Promise<unknown>;
|
|
1397
1399
|
}
|
|
1398
|
-
type RakeDbMigrationId = '
|
|
1400
|
+
type RakeDbMigrationId = 'timestamp' | {
|
|
1401
|
+
serial: number;
|
|
1402
|
+
};
|
|
1403
|
+
interface RakeDbRenameMigrationsMap {
|
|
1404
|
+
[K: string]: number;
|
|
1405
|
+
}
|
|
1406
|
+
interface RakeDbRenameMigrations {
|
|
1407
|
+
to: RakeDbMigrationId;
|
|
1408
|
+
map(): MaybePromise<RakeDbRenameMigrationsMap>;
|
|
1409
|
+
}
|
|
1410
|
+
interface RakeDbRenameMigrationsInput {
|
|
1411
|
+
to: RakeDbMigrationId;
|
|
1412
|
+
map: RakeDbRenameMigrationsMap;
|
|
1413
|
+
}
|
|
1399
1414
|
declare const migrationConfigDefaults: {
|
|
1400
1415
|
schemaConfig: DefaultSchemaConfig;
|
|
1401
1416
|
migrationsPath: string;
|
|
1402
|
-
migrationId:
|
|
1417
|
+
migrationId: {
|
|
1418
|
+
serial: number;
|
|
1419
|
+
};
|
|
1403
1420
|
migrationsTable: string;
|
|
1404
1421
|
snakeCase: false;
|
|
1405
1422
|
commands: {};
|
|
@@ -1435,7 +1452,7 @@ type RakeDbAppliedVersions = {
|
|
|
1435
1452
|
map: RecordOptionalString;
|
|
1436
1453
|
sequence: number[];
|
|
1437
1454
|
};
|
|
1438
|
-
declare const getMigratedVersionsMap: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(ctx: RakeDbCtx, adapter: Adapter | TransactionAdapter, config: RakeDbConfig<SchemaConfig, CT>, renameTo?:
|
|
1455
|
+
declare const getMigratedVersionsMap: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(ctx: RakeDbCtx, adapter: Adapter | TransactionAdapter, config: RakeDbConfig<SchemaConfig, CT>, renameTo?: RakeDbRenameMigrations) => Promise<RakeDbAppliedVersions>;
|
|
1439
1456
|
|
|
1440
1457
|
declare const RAKE_DB_LOCK_KEY = "8582141715823621641";
|
|
1441
1458
|
type MigrateFn = <SchemaConfig extends ColumnSchemaConfig, CT>(ctx: RakeDbCtx, options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args?: string[], adapters?: Adapter[], dontClose?: boolean) => Promise<Adapter[]>;
|
|
@@ -1461,7 +1478,7 @@ declare const rollback: MigrateFn;
|
|
|
1461
1478
|
* Takes the same options as {@link migrate}.
|
|
1462
1479
|
*/
|
|
1463
1480
|
declare const redo: MigrateFn;
|
|
1464
|
-
declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, unknown>, set: MigrationsSet, versions: RakeDbAppliedVersions, count: number, up: boolean, redo: boolean, force?: boolean, skipLock?: boolean) => Promise<
|
|
1481
|
+
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[]>;
|
|
1465
1482
|
declare const changeCache: Record<string, ChangeCallback<unknown>[] | undefined>;
|
|
1466
1483
|
|
|
1467
1484
|
declare namespace DbStructure {
|
|
@@ -1777,4 +1794,4 @@ declare const promptText: ({ message, default: def, password, min, }: {
|
|
|
1777
1794
|
min?: number;
|
|
1778
1795
|
}) => Promise<string>;
|
|
1779
1796
|
|
|
1780
|
-
export { AnyRakeDbConfig, ChangeCallback, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, CommandFn, DbMigration, DbStructure, DbStructureDomainsMap, DropMode, InputRakeDbConfig, InputRakeDbConfigBase, IntrospectedStructure, Migration, MigrationAdapter, MigrationColumnTypes, ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAppliedVersions, RakeDbAst, RakeDbBaseTable, RakeDbChangeFn, RakeDbConfig, RakeDbCtx, RakeDbFn, RakeDbFnReturns, RakeDbLazyFn, RakeDbMigrationId, RakeDbResult, SilentQueries, StructureToAstCtx, StructureToAstTableData, TableOptions, addColumnComment, addColumnIndex, addOrDropEnumValues, astToMigration, changeCache, changeEnumValues, clearChanges, colors, columnToSql, columnTypeToSql, commentsToQuery, concatSchemaAndName, constraintToSql, createDb, createMigrationInterface, dbColumnToAst, deleteMigratedVersion, dropDb, encodeColumnDefault, exhaustive, generateTimeStamp, getColumnName, getConstraintName, getCurrentChanges, getDatabaseAndUserFromOptions, getDbStructureTableData, getDbTableColumnsChecks, getFirstWordAndRest, getForeignKeyTable, getIndexName, getMigratedVersionsMap, getSchemaAndTableFromName, getTextAfterFrom, getTextAfterTo, identityToSql, indexesToQuery, instantiateDbColumn, introspectDbSchema, joinColumns, joinWords, makeDbStructureColumnsShape, makeDomainsMap, makeFileVersion, makePopulateEnumQuery, makeStructureToAstCtx, migrate, migrateOrRollback, migrationConfigDefaults, newMigration, pluralize, primaryKeyToSql, processRakeDbConfig, promptConfirm, promptSelect, promptText, pushChange, queryLock, quoteNameFromString, quoteSchemaTable, quoteTable, quoteWithSchema, rakeDb, rakeDbAliases, rakeDbCommands, redo, referencesToSql, renameType, resetDb, rollback, saveMigratedVersion, structureToAst, tableToAst, transaction, versionToString, writeMigrationFile };
|
|
1797
|
+
export { AnyRakeDbConfig, ChangeCallback, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, CommandFn, DbMigration, DbStructure, DbStructureDomainsMap, DropMode, InputRakeDbConfig, InputRakeDbConfigBase, IntrospectedStructure, Migration, MigrationAdapter, MigrationColumnTypes, ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAppliedVersions, RakeDbAst, RakeDbBaseTable, RakeDbChangeFn, RakeDbConfig, RakeDbCtx, RakeDbFn, RakeDbFnReturns, RakeDbLazyFn, RakeDbMigrationId, RakeDbRenameMigrations, RakeDbRenameMigrationsInput, RakeDbRenameMigrationsMap, RakeDbResult, SilentQueries, StructureToAstCtx, StructureToAstTableData, TableOptions, addColumnComment, addColumnIndex, addOrDropEnumValues, astToMigration, changeCache, changeEnumValues, clearChanges, colors, columnToSql, columnTypeToSql, commentsToQuery, concatSchemaAndName, constraintToSql, createDb, createMigrationInterface, dbColumnToAst, deleteMigratedVersion, dropDb, encodeColumnDefault, exhaustive, generateTimeStamp, getColumnName, getConstraintName, getCurrentChanges, getDatabaseAndUserFromOptions, getDbStructureTableData, getDbTableColumnsChecks, getFirstWordAndRest, getForeignKeyTable, getIndexName, getMigratedVersionsMap, getSchemaAndTableFromName, getTextAfterFrom, getTextAfterTo, identityToSql, indexesToQuery, instantiateDbColumn, introspectDbSchema, joinColumns, joinWords, makeDbStructureColumnsShape, makeDomainsMap, makeFileVersion, makePopulateEnumQuery, makeStructureToAstCtx, migrate, migrateOrRollback, migrationConfigDefaults, newMigration, pluralize, primaryKeyToSql, processRakeDbConfig, promptConfirm, promptSelect, promptText, pushChange, queryLock, quoteNameFromString, quoteSchemaTable, quoteTable, quoteWithSchema, rakeDb, rakeDbAliases, rakeDbCommands, redo, referencesToSql, renameType, resetDb, rollback, saveMigratedVersion, structureToAst, tableToAst, transaction, versionToString, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -112,7 +112,7 @@ var __spreadValues$9 = (a, b) => {
|
|
|
112
112
|
return a;
|
|
113
113
|
};
|
|
114
114
|
var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
115
|
-
const versionToString = (config, version) => config.migrationId === "
|
|
115
|
+
const versionToString = (config, version) => config.migrationId === "timestamp" ? `${version}` : `${version}`.padStart(config.migrationId.serial, "0");
|
|
116
116
|
const columnTypeToSql = (item) => {
|
|
117
117
|
return item.data.isOfCustomType ? quoteNameFromString(item.toSQL()) : item.toSQL();
|
|
118
118
|
};
|
|
@@ -2490,19 +2490,17 @@ const fileNamesToChangeMigrationId = {
|
|
|
2490
2490
|
const fileNamesToChangeMigrationIdMap = Object.fromEntries(
|
|
2491
2491
|
Object.entries(fileNamesToChangeMigrationId).map(([_, name]) => [name, true])
|
|
2492
2492
|
);
|
|
2493
|
-
const changeIds = async (options, config, [arg]) => {
|
|
2494
|
-
var _a;
|
|
2493
|
+
const changeIds = async (options, config, [arg, digitsArg]) => {
|
|
2494
|
+
var _a, _b, _c, _d;
|
|
2495
2495
|
if (arg !== "serial" && arg !== "timestamp") {
|
|
2496
2496
|
throw new Error(
|
|
2497
2497
|
`Pass "serial" or "timestamp" argument to the "change-ids" command`
|
|
2498
2498
|
);
|
|
2499
2499
|
}
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
}
|
|
2505
|
-
const data = await getMigrationsFromFiles(config, false, (_, filePath) => {
|
|
2500
|
+
let digits = digitsArg && parseInt(digitsArg);
|
|
2501
|
+
if (!digits || isNaN(digits))
|
|
2502
|
+
digits = 4;
|
|
2503
|
+
const data = await getMigrations({}, config, true, false, (_, filePath) => {
|
|
2506
2504
|
const fileName = path.basename(filePath);
|
|
2507
2505
|
const match = fileName.match(/^(\d+)\D/);
|
|
2508
2506
|
if (!match) {
|
|
@@ -2513,47 +2511,87 @@ const changeIds = async (options, config, [arg]) => {
|
|
|
2513
2511
|
return match[1];
|
|
2514
2512
|
});
|
|
2515
2513
|
if (data.renameTo) {
|
|
2516
|
-
if (data.renameTo === arg) {
|
|
2517
|
-
(_a = config.logger) == null ? void 0 : _a.log(
|
|
2514
|
+
if (arg === "serial" && typeof data.renameTo.to === "object" && digits === data.renameTo.to.serial || arg === "timestamp" && data.renameTo.to === "timestamp") {
|
|
2515
|
+
(_a = config.logger) == null ? void 0 : _a.log(
|
|
2516
|
+
config.migrations ? "`renameMigrations` setting is already set" : `${fileNamesToChangeMigrationId[arg]} already exists`
|
|
2517
|
+
);
|
|
2518
2518
|
return;
|
|
2519
2519
|
}
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2520
|
+
if (!config.migrations) {
|
|
2521
|
+
await fs.unlink(
|
|
2522
|
+
path.join(
|
|
2523
|
+
config.migrationsPath,
|
|
2524
|
+
fileNamesToChangeMigrationId[data.renameTo.to === "timestamp" ? "timestamp" : "serial"]
|
|
2525
|
+
)
|
|
2526
|
+
);
|
|
2527
|
+
}
|
|
2526
2528
|
}
|
|
2527
2529
|
const version = arg === "timestamp" ? parseInt(generateTimeStamp()) : 1;
|
|
2528
2530
|
const rename = Object.fromEntries(
|
|
2529
2531
|
data.migrations.map((item, i) => [path.basename(item.path), version + i])
|
|
2530
2532
|
);
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2533
|
+
if (config.migrations) {
|
|
2534
|
+
const to = arg === "timestamp" ? `'${arg}'` : `{ serial: ${digits} }`;
|
|
2535
|
+
(_b = config.logger) == null ? void 0 : _b.log(
|
|
2536
|
+
`Save the following settings into your rake-db config under the \`migrations\` setting, it will instruct rake-db to rename migration entries during the next deploy:
|
|
2537
|
+
${arg !== "serial" || digits !== 4 ? `
|
|
2538
|
+
migrationId: ${to},` : ""}
|
|
2539
|
+
renameMigrations: {
|
|
2540
|
+
to: ${to},
|
|
2541
|
+
map: {
|
|
2542
|
+
` + Object.entries(rename).map(([key, value]) => `"${key}": ${value},`).join("\n ") + "\n },\n},\n\n"
|
|
2543
|
+
);
|
|
2544
|
+
} else {
|
|
2545
|
+
await fs.writeFile(
|
|
2546
|
+
path.join(config.migrationsPath, fileNamesToChangeMigrationId[arg]),
|
|
2547
|
+
JSON.stringify(rename, null, 2)
|
|
2548
|
+
);
|
|
2549
|
+
}
|
|
2550
|
+
const values = data.migrations.map(
|
|
2551
|
+
(item, i) => {
|
|
2538
2552
|
let newVersion = String(version + i);
|
|
2539
2553
|
if (arg === "serial")
|
|
2540
|
-
newVersion = newVersion.padStart(
|
|
2554
|
+
newVersion = newVersion.padStart(digits, "0");
|
|
2541
2555
|
const name = path.basename(item.path).slice(item.version.length + 1);
|
|
2556
|
+
return [item.version, name, newVersion];
|
|
2557
|
+
}
|
|
2558
|
+
);
|
|
2559
|
+
if (!values.length)
|
|
2560
|
+
return;
|
|
2561
|
+
if (config.migrations) {
|
|
2562
|
+
(_c = config.logger) == null ? void 0 : _c.log(
|
|
2563
|
+
`If your migrations are stored in files, navigate to migrations directory and run the following commands to rename them:
|
|
2564
|
+
|
|
2565
|
+
${values.map(
|
|
2566
|
+
([version2, name, newVersion]) => `mv "${version2}_${name}" "${newVersion}_${name}"`
|
|
2567
|
+
).join(
|
|
2568
|
+
"\n"
|
|
2569
|
+
)}
|
|
2570
|
+
|
|
2571
|
+
After setting \`renameMigrations\` (see above) and renaming the files, run the db up command to rename migration entries in your database`
|
|
2572
|
+
);
|
|
2573
|
+
return;
|
|
2574
|
+
}
|
|
2575
|
+
await Promise.all(
|
|
2576
|
+
data.migrations.map(async (item, i) => {
|
|
2577
|
+
const [, name, newVersion] = values[i];
|
|
2542
2578
|
await fs.rename(
|
|
2543
2579
|
item.path,
|
|
2544
2580
|
path.join(path.dirname(item.path), `${newVersion}_${name}`)
|
|
2545
2581
|
);
|
|
2546
|
-
values.push([item.version, name, newVersion]);
|
|
2547
2582
|
})
|
|
2548
2583
|
);
|
|
2549
|
-
if (!values.length)
|
|
2550
|
-
return;
|
|
2551
2584
|
await options.map((opts) => {
|
|
2552
2585
|
const adapter = new pqb.Adapter(opts);
|
|
2553
2586
|
renameMigrationVersionsInDb(config, adapter, values).then(
|
|
2554
2587
|
() => adapter.close()
|
|
2555
2588
|
);
|
|
2556
2589
|
});
|
|
2590
|
+
(_d = config.logger) == null ? void 0 : _d.log(
|
|
2591
|
+
`Migration files were renamed, a config file ${fileNamesToChangeMigrationId[arg]} for renaming migrations after deploy was created, and migrations in local db were renamed successfully.
|
|
2592
|
+
|
|
2593
|
+
${arg === "timestamp" || digits !== 4 ? `Set \`migrationId\`: ${arg === "timestamp" ? `'timestamp'` : `{ serial: ${digits} }`}` : `Remove \`migrationId\``} setting in the rake-db config`
|
|
2594
|
+
);
|
|
2557
2595
|
};
|
|
2558
2596
|
const renameMigrationVersionsInDb = async (config, adapter, values) => {
|
|
2559
2597
|
await adapter.arrays({
|
|
@@ -2585,18 +2623,25 @@ var __spreadValues$5 = (a, b) => {
|
|
|
2585
2623
|
return a;
|
|
2586
2624
|
};
|
|
2587
2625
|
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
2588
|
-
const getMigrations = async (ctx, config, up, allowDuplicates) => {
|
|
2626
|
+
const getMigrations = async (ctx, config, up, allowDuplicates, getVersion = getMigrationVersionOrThrow) => {
|
|
2589
2627
|
var _a;
|
|
2590
|
-
return ((_a = ctx.migrationsPromise) != null ? _a : ctx.migrationsPromise = config.migrations ? getMigrationsFromConfig(
|
|
2591
|
-
(
|
|
2628
|
+
return ((_a = ctx.migrationsPromise) != null ? _a : ctx.migrationsPromise = config.migrations ? getMigrationsFromConfig(
|
|
2629
|
+
__spreadProps$3(__spreadValues$5({}, config), { migrations: config.migrations }),
|
|
2630
|
+
allowDuplicates,
|
|
2631
|
+
getVersion
|
|
2632
|
+
) : getMigrationsFromFiles(config, allowDuplicates, getVersion)).then(
|
|
2633
|
+
(data) => up ? data : {
|
|
2634
|
+
renameTo: data.renameTo,
|
|
2635
|
+
migrations: [...data.migrations].reverse()
|
|
2636
|
+
}
|
|
2592
2637
|
);
|
|
2593
2638
|
};
|
|
2594
|
-
function getMigrationsFromConfig(config, allowDuplicates) {
|
|
2639
|
+
function getMigrationsFromConfig(config, allowDuplicates, getVersion = getMigrationVersionOrThrow) {
|
|
2595
2640
|
const result = [];
|
|
2596
2641
|
const versions = {};
|
|
2597
2642
|
const { migrations, basePath } = config;
|
|
2598
2643
|
for (const key in migrations) {
|
|
2599
|
-
const version =
|
|
2644
|
+
const version = getVersion(config, path.basename(key));
|
|
2600
2645
|
if (versions[version] && !allowDuplicates) {
|
|
2601
2646
|
throw new Error(
|
|
2602
2647
|
`Migration ${key} has the same version as ${versions[version]}`
|
|
@@ -2609,8 +2654,10 @@ function getMigrationsFromConfig(config, allowDuplicates) {
|
|
|
2609
2654
|
load: migrations[key]
|
|
2610
2655
|
});
|
|
2611
2656
|
}
|
|
2657
|
+
const { renameMigrations } = config;
|
|
2612
2658
|
return Promise.resolve({
|
|
2613
|
-
migrations: result
|
|
2659
|
+
migrations: result,
|
|
2660
|
+
renameTo: renameMigrations ? { to: renameMigrations.to, map: () => renameMigrations.map } : void 0
|
|
2614
2661
|
});
|
|
2615
2662
|
}
|
|
2616
2663
|
const sortMigrationsAsc = (a, b) => +a.version - +b.version;
|
|
@@ -2630,7 +2677,19 @@ async function getMigrationsFromFiles(config, allowDuplicates, getVersion = getM
|
|
|
2630
2677
|
`Both files for renaming to serial and timestamp found, only one must remain`
|
|
2631
2678
|
);
|
|
2632
2679
|
}
|
|
2633
|
-
|
|
2680
|
+
const isSerialFile = file.name === ".rename-to-serial.json";
|
|
2681
|
+
const isSerialConfig = config.migrationId !== "timestamp";
|
|
2682
|
+
if (isSerialFile && !isSerialConfig || !isSerialFile && isSerialConfig) {
|
|
2683
|
+
throw new Error(
|
|
2684
|
+
`File ${file.name} to rename migrations does not match \`migrationId\` ${JSON.stringify(
|
|
2685
|
+
config.migrationId
|
|
2686
|
+
)} set in config`
|
|
2687
|
+
);
|
|
2688
|
+
}
|
|
2689
|
+
data.renameTo = {
|
|
2690
|
+
to: config.migrationId,
|
|
2691
|
+
map: () => renameMigrationsMap(config, file.name)
|
|
2692
|
+
};
|
|
2634
2693
|
return data;
|
|
2635
2694
|
} else {
|
|
2636
2695
|
checkExt(file.name);
|
|
@@ -2668,6 +2727,21 @@ Run \`**db command** rebase\` to reorganize files with duplicated versions.`
|
|
|
2668
2727
|
result.migrations.sort(sortMigrationsAsc);
|
|
2669
2728
|
return result;
|
|
2670
2729
|
}
|
|
2730
|
+
const renameMigrationsMap = async (config, fileName) => {
|
|
2731
|
+
const filePath = path.join(config.migrationsPath, fileName);
|
|
2732
|
+
const json = await fs.readFile(filePath, "utf-8");
|
|
2733
|
+
let data;
|
|
2734
|
+
try {
|
|
2735
|
+
data = JSON.parse(json);
|
|
2736
|
+
if (typeof data !== "object")
|
|
2737
|
+
throw new Error("Config for renaming is not an object");
|
|
2738
|
+
} catch (err) {
|
|
2739
|
+
throw new Error(`Failed to read ${node_url.pathToFileURL(filePath)}`, {
|
|
2740
|
+
cause: err
|
|
2741
|
+
});
|
|
2742
|
+
}
|
|
2743
|
+
return data;
|
|
2744
|
+
};
|
|
2671
2745
|
function checkExt(filePath) {
|
|
2672
2746
|
const ext = path.extname(filePath);
|
|
2673
2747
|
if (ext !== ".ts" && ext !== ".js" && ext !== ".mjs") {
|
|
@@ -2782,32 +2856,20 @@ async function renameMigrations(config, trx, versions, renameTo) {
|
|
|
2782
2856
|
}
|
|
2783
2857
|
if (!first || getMigrationVersion(config, first))
|
|
2784
2858
|
return versions;
|
|
2785
|
-
const fileName = fileNamesToChangeMigrationId[renameTo];
|
|
2786
|
-
const filePath = path.join(config.migrationsPath, fileName);
|
|
2787
|
-
const json = await fs.readFile(filePath, "utf-8");
|
|
2788
|
-
let data;
|
|
2789
|
-
try {
|
|
2790
|
-
data = JSON.parse(json);
|
|
2791
|
-
if (typeof data !== "object")
|
|
2792
|
-
throw new Error("Config for renaming is not an object");
|
|
2793
|
-
} catch (err) {
|
|
2794
|
-
throw new Error(`Failed to read ${node_url.pathToFileURL(filePath)}`, {
|
|
2795
|
-
cause: err
|
|
2796
|
-
});
|
|
2797
|
-
}
|
|
2798
2859
|
const values = [];
|
|
2799
2860
|
const updatedVersions = {};
|
|
2861
|
+
const data = await renameTo.map();
|
|
2800
2862
|
for (const version in versions) {
|
|
2801
2863
|
const name = versions[version];
|
|
2802
2864
|
const key = `${version}_${name}`;
|
|
2803
2865
|
let newVersion = data[key];
|
|
2804
2866
|
if (!newVersion) {
|
|
2805
2867
|
throw new Error(
|
|
2806
|
-
`Failed to find an entry for the migrated ${key} in the
|
|
2868
|
+
`Failed to find an entry for the migrated ${key} in the renaming config`
|
|
2807
2869
|
);
|
|
2808
2870
|
}
|
|
2809
|
-
if (renameTo === "
|
|
2810
|
-
newVersion = String(newVersion).padStart(
|
|
2871
|
+
if (typeof renameTo.to === "object") {
|
|
2872
|
+
newVersion = String(newVersion).padStart(renameTo.to.serial, "0");
|
|
2811
2873
|
}
|
|
2812
2874
|
updatedVersions[newVersion] = name;
|
|
2813
2875
|
values.push([version, name, newVersion]);
|
|
@@ -2863,6 +2925,7 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2863
2925
|
for (let i = 0; i < length; i++) {
|
|
2864
2926
|
const opts = options[i];
|
|
2865
2927
|
const adapter = adapters[i];
|
|
2928
|
+
let migrations;
|
|
2866
2929
|
try {
|
|
2867
2930
|
await transaction(adapter, async (trx) => {
|
|
2868
2931
|
const versions = await getMigratedVersionsMap(
|
|
@@ -2871,7 +2934,7 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2871
2934
|
config,
|
|
2872
2935
|
set.renameTo
|
|
2873
2936
|
);
|
|
2874
|
-
await fn(
|
|
2937
|
+
migrations = await fn(
|
|
2875
2938
|
trx,
|
|
2876
2939
|
conf,
|
|
2877
2940
|
set,
|
|
@@ -2891,7 +2954,14 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2891
2954
|
config2,
|
|
2892
2955
|
set.renameTo
|
|
2893
2956
|
);
|
|
2894
|
-
await fn(
|
|
2957
|
+
migrations = await fn(
|
|
2958
|
+
trx,
|
|
2959
|
+
config2,
|
|
2960
|
+
set,
|
|
2961
|
+
versions,
|
|
2962
|
+
count != null ? count : defaultCount,
|
|
2963
|
+
force
|
|
2964
|
+
);
|
|
2895
2965
|
});
|
|
2896
2966
|
} else {
|
|
2897
2967
|
throw err;
|
|
@@ -2903,7 +2973,7 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2903
2973
|
(_a = config.afterChangeCommit) == null ? void 0 : _a.call(config, {
|
|
2904
2974
|
options: opts,
|
|
2905
2975
|
up,
|
|
2906
|
-
migrations
|
|
2976
|
+
migrations
|
|
2907
2977
|
});
|
|
2908
2978
|
}
|
|
2909
2979
|
return adapters;
|
|
@@ -2926,7 +2996,7 @@ const redo = makeMigrateFn(
|
|
|
2926
2996
|
set.migrations.reverse();
|
|
2927
2997
|
await migrateOrRollback(trx, config, set, versions, count, false, true);
|
|
2928
2998
|
set.migrations.reverse();
|
|
2929
|
-
|
|
2999
|
+
return migrateOrRollback(
|
|
2930
3000
|
trx,
|
|
2931
3001
|
config,
|
|
2932
3002
|
set,
|
|
@@ -2974,11 +3044,12 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
2974
3044
|
const beforeMigrate = config[up ? "beforeMigrate" : "beforeRollback"];
|
|
2975
3045
|
if (beforeMigrate || config.beforeChange) {
|
|
2976
3046
|
db != null ? db : db = getDb(trx);
|
|
2977
|
-
const { migrations } = set;
|
|
2978
|
-
await (beforeMigrate == null ? void 0 : beforeMigrate({ db, migrations }));
|
|
2979
|
-
await ((_a = config.beforeChange) == null ? void 0 : _a.call(config, { db, migrations, up, redo: redo2 }));
|
|
3047
|
+
const { migrations: migrations2 } = set;
|
|
3048
|
+
await (beforeMigrate == null ? void 0 : beforeMigrate({ db, migrations: migrations2 }));
|
|
3049
|
+
await ((_a = config.beforeChange) == null ? void 0 : _a.call(config, { db, migrations: migrations2, up, redo: redo2 }));
|
|
2980
3050
|
}
|
|
2981
3051
|
let loggedAboutStarting = false;
|
|
3052
|
+
let migrations;
|
|
2982
3053
|
for (const file of set.migrations) {
|
|
2983
3054
|
if (up && versionsMap[file.version] || !up && !versionsMap[file.version]) {
|
|
2984
3055
|
continue;
|
|
@@ -2993,6 +3064,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
2993
3064
|
);
|
|
2994
3065
|
}
|
|
2995
3066
|
await runMigration(trx, up, file, config);
|
|
3067
|
+
(migrations != null ? migrations : migrations = []).push(file);
|
|
2996
3068
|
if (up) {
|
|
2997
3069
|
const name = path.basename(file.path);
|
|
2998
3070
|
versionsMap[file.version] = name;
|
|
@@ -3006,13 +3078,14 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3006
3078
|
`
|
|
3007
3079
|
);
|
|
3008
3080
|
}
|
|
3081
|
+
migrations != null ? migrations : migrations = orchidCore.emptyArray;
|
|
3009
3082
|
const afterMigrate = config[up ? "afterMigrate" : "afterRollback"];
|
|
3010
3083
|
if (config.afterChange || afterMigrate) {
|
|
3011
3084
|
db != null ? db : db = getDb(trx);
|
|
3012
|
-
const { migrations } = set;
|
|
3013
3085
|
await ((_d = config.afterChange) == null ? void 0 : _d.call(config, { db, up, redo: redo2, migrations }));
|
|
3014
3086
|
await (afterMigrate == null ? void 0 : afterMigrate({ db, migrations }));
|
|
3015
3087
|
}
|
|
3088
|
+
return migrations;
|
|
3016
3089
|
};
|
|
3017
3090
|
const checkMigrationOrder = (config, set, { sequence, map }, force) => {
|
|
3018
3091
|
const last = sequence[sequence.length - 1];
|
|
@@ -3085,7 +3158,7 @@ var __spreadValues$4 = (a, b) => {
|
|
|
3085
3158
|
const migrationConfigDefaults = {
|
|
3086
3159
|
schemaConfig: pqb.defaultSchemaConfig,
|
|
3087
3160
|
migrationsPath: path.join("src", "db", "migrations"),
|
|
3088
|
-
migrationId:
|
|
3161
|
+
migrationId: { serial: 4 },
|
|
3089
3162
|
migrationsTable: "schemaMigrations",
|
|
3090
3163
|
snakeCase: false,
|
|
3091
3164
|
commands: {},
|
|
@@ -3145,6 +3218,9 @@ const processRakeDbConfig = (config) => {
|
|
|
3145
3218
|
pqb.makeColumnTypes(pqb.defaultSchemaConfig)
|
|
3146
3219
|
) : ct) || pqb.makeColumnTypes;
|
|
3147
3220
|
}
|
|
3221
|
+
if (config.migrationId === "serial") {
|
|
3222
|
+
result.migrationId = { serial: 4 };
|
|
3223
|
+
}
|
|
3148
3224
|
return result;
|
|
3149
3225
|
};
|
|
3150
3226
|
const getDatabaseAndUserFromOptions = (options) => {
|
|
@@ -5346,9 +5422,9 @@ const rebase = async (options, config) => {
|
|
|
5346
5422
|
if (config.migrations) {
|
|
5347
5423
|
throw new Error("Cannot rebase migrations defined in the config");
|
|
5348
5424
|
}
|
|
5349
|
-
if (config.migrationId
|
|
5425
|
+
if (config.migrationId === "timestamp") {
|
|
5350
5426
|
throw new Error(
|
|
5351
|
-
`Cannot rebase when the 'migrationId' is set to '
|
|
5427
|
+
`Cannot rebase when the 'migrationId' is set to 'timestamp' in the config`
|
|
5352
5428
|
);
|
|
5353
5429
|
}
|
|
5354
5430
|
const adapters = options.map((opts) => new pqb.Adapter(opts));
|
|
@@ -5707,7 +5783,7 @@ const rakeDbCommands = {
|
|
|
5707
5783
|
helpArguments: {
|
|
5708
5784
|
serial: "change ids to 4 digit serial",
|
|
5709
5785
|
"serial *number*": "change ids to serial number of custom length",
|
|
5710
|
-
|
|
5786
|
+
timestamp: "change ids to timestamps"
|
|
5711
5787
|
}
|
|
5712
5788
|
}
|
|
5713
5789
|
};
|