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 +285 -412
- package/dist/index.js +60 -103
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +60 -44
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var pqb = require('pqb');
|
|
4
3
|
var orchidCore = require('orchid-core');
|
|
4
|
+
var pqb = require('pqb');
|
|
5
5
|
var path = require('path');
|
|
6
6
|
var node_url = require('node:url');
|
|
7
7
|
var fs = require('fs/promises');
|
|
8
|
+
require('url');
|
|
9
|
+
require('node:path');
|
|
8
10
|
|
|
9
11
|
const getFirstWordAndRest = (input) => {
|
|
10
12
|
const i = input.search(/(?=[A-Z])|[-_ ]/);
|
|
@@ -31,12 +33,6 @@ const getTextAfterTo = (input) => {
|
|
|
31
33
|
const getTextAfterFrom = (input) => {
|
|
32
34
|
return getTextAfterRegExp(input, /(From|-from|_from| from)[A-Z-_ ]/, 4);
|
|
33
35
|
};
|
|
34
|
-
const joinWords = (...words) => {
|
|
35
|
-
return words.slice(1).reduce(
|
|
36
|
-
(acc, word) => acc + word[0].toUpperCase() + word.slice(1),
|
|
37
|
-
words[0]
|
|
38
|
-
);
|
|
39
|
-
};
|
|
40
36
|
const joinColumns = (columns) => {
|
|
41
37
|
return columns.map((column) => `"${column}"`).join(", ");
|
|
42
38
|
};
|
|
@@ -81,19 +77,13 @@ const transaction = (adapter, fn) => {
|
|
|
81
77
|
return adapter.transaction(begin, fn);
|
|
82
78
|
};
|
|
83
79
|
const queryLock = (trx) => trx.query(`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`);
|
|
84
|
-
const exhaustive = (_) => {
|
|
85
|
-
throw new Error("Condition was not exhaustive");
|
|
86
|
-
};
|
|
87
|
-
const pluralize = (w, count, append = "s") => {
|
|
88
|
-
return count === 1 ? w : w + append;
|
|
89
|
-
};
|
|
90
80
|
|
|
91
81
|
let currentChanges = [];
|
|
92
82
|
const clearChanges = () => {
|
|
93
83
|
currentChanges = [];
|
|
94
84
|
};
|
|
95
85
|
const getCurrentChanges = () => currentChanges;
|
|
96
|
-
const pushChange = (
|
|
86
|
+
const pushChange = (change) => currentChanges.push(change);
|
|
97
87
|
|
|
98
88
|
const versionToString = (config, version) => config.migrationId === "timestamp" ? `${version}` : `${version}`.padStart(config.migrationId.serial, "0");
|
|
99
89
|
const columnTypeToSql = (item) => {
|
|
@@ -3064,17 +3054,17 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
3064
3054
|
return adapters;
|
|
3065
3055
|
};
|
|
3066
3056
|
}
|
|
3067
|
-
const
|
|
3057
|
+
const fullMigrate = makeMigrateFn(
|
|
3068
3058
|
Infinity,
|
|
3069
3059
|
true,
|
|
3070
3060
|
(trx, config, set, versions, count, force) => migrateOrRollback(trx, config, set, versions, count, true, false, force)
|
|
3071
3061
|
);
|
|
3072
|
-
const
|
|
3062
|
+
const fullRollback = makeMigrateFn(
|
|
3073
3063
|
1,
|
|
3074
3064
|
false,
|
|
3075
3065
|
(trx, config, set, versions, count, force) => migrateOrRollback(trx, config, set, versions, count, false, false, force)
|
|
3076
3066
|
);
|
|
3077
|
-
const
|
|
3067
|
+
const fullRedo = makeMigrateFn(
|
|
3078
3068
|
1,
|
|
3079
3069
|
true,
|
|
3080
3070
|
async (trx, config, set, versions, count, force) => {
|
|
@@ -3095,7 +3085,7 @@ const redo = makeMigrateFn(
|
|
|
3095
3085
|
}
|
|
3096
3086
|
);
|
|
3097
3087
|
const getDb = (adapter) => pqb.createDb({ adapter });
|
|
3098
|
-
const migrateOrRollback = async (trx, config, set, versions, count, up,
|
|
3088
|
+
const migrateOrRollback = async (trx, config, set, versions, count, up, redo, force, skipLock) => {
|
|
3099
3089
|
const { sequence, map: versionsMap } = versions;
|
|
3100
3090
|
if (up) {
|
|
3101
3091
|
const rollbackTo = checkMigrationOrder(config, set, versions, force);
|
|
@@ -3116,7 +3106,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3116
3106
|
versions,
|
|
3117
3107
|
sequence.length - i,
|
|
3118
3108
|
false,
|
|
3119
|
-
|
|
3109
|
+
redo
|
|
3120
3110
|
);
|
|
3121
3111
|
set.migrations.reverse();
|
|
3122
3112
|
}
|
|
@@ -3128,7 +3118,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3128
3118
|
db ?? (db = getDb(trx));
|
|
3129
3119
|
const { migrations: migrations2 } = set;
|
|
3130
3120
|
await beforeMigrate?.({ db, migrations: migrations2 });
|
|
3131
|
-
await config.beforeChange?.({ db, migrations: migrations2, up, redo
|
|
3121
|
+
await config.beforeChange?.({ db, migrations: migrations2, up, redo });
|
|
3132
3122
|
}
|
|
3133
3123
|
let loggedAboutStarting = false;
|
|
3134
3124
|
let migrations;
|
|
@@ -3137,14 +3127,16 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3137
3127
|
continue;
|
|
3138
3128
|
}
|
|
3139
3129
|
if (count-- <= 0) break;
|
|
3140
|
-
if (!loggedAboutStarting && (!
|
|
3130
|
+
if (!loggedAboutStarting && (!redo || !up)) {
|
|
3141
3131
|
loggedAboutStarting = true;
|
|
3142
3132
|
config.logger?.log(
|
|
3143
|
-
`${
|
|
3133
|
+
`${redo ? "Reapplying migrations for" : up ? "Migrating" : "Rolling back"} database ${trx.config.connectionString ? new URL(trx.config.connectionString).pathname.slice(1) : trx.config.database}
|
|
3144
3134
|
`
|
|
3145
3135
|
);
|
|
3146
3136
|
}
|
|
3147
|
-
await
|
|
3137
|
+
const changes = await getChanges(file, config);
|
|
3138
|
+
const adapter = await runMigration(trx, up, changes, config);
|
|
3139
|
+
await changeMigratedVersion(adapter, up, file, config);
|
|
3148
3140
|
(migrations ?? (migrations = [])).push(file);
|
|
3149
3141
|
if (up) {
|
|
3150
3142
|
const name = path.basename(file.path);
|
|
@@ -3163,7 +3155,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3163
3155
|
const afterMigrate = config[up ? "afterMigrate" : "afterRollback"];
|
|
3164
3156
|
if (config.afterChange || afterMigrate) {
|
|
3165
3157
|
db ?? (db = getDb(trx));
|
|
3166
|
-
await config.afterChange?.({ db, up, redo
|
|
3158
|
+
await config.afterChange?.({ db, up, redo, migrations });
|
|
3167
3159
|
await afterMigrate?.({ db, migrations });
|
|
3168
3160
|
}
|
|
3169
3161
|
return migrations;
|
|
@@ -3206,31 +3198,37 @@ Run \`**db command** up force\` to rollback the above migrations and migrate all
|
|
|
3206
3198
|
return;
|
|
3207
3199
|
};
|
|
3208
3200
|
const changeCache = {};
|
|
3209
|
-
const
|
|
3201
|
+
const getChanges = async (file, config) => {
|
|
3210
3202
|
clearChanges();
|
|
3211
|
-
let changes = changeCache[file.path];
|
|
3203
|
+
let changes = file.path ? changeCache[file.path] : void 0;
|
|
3212
3204
|
if (!changes) {
|
|
3213
3205
|
const module = await file.load();
|
|
3214
3206
|
const exported = module?.default && orchidCore.toArray(module.default);
|
|
3215
|
-
if (config
|
|
3207
|
+
if (config?.forceDefaultExports && !exported) {
|
|
3216
3208
|
throw new RakeDbError(
|
|
3217
3209
|
`Missing a default export in ${file.path} migration`
|
|
3218
3210
|
);
|
|
3219
3211
|
}
|
|
3220
3212
|
changes = exported || getCurrentChanges();
|
|
3221
|
-
changeCache[file.path] = changes;
|
|
3213
|
+
if (file.path) changeCache[file.path] = changes;
|
|
3222
3214
|
}
|
|
3215
|
+
return changes;
|
|
3216
|
+
};
|
|
3217
|
+
const runMigration = async (trx, up, changes, config) => {
|
|
3223
3218
|
const db = createMigrationInterface(trx, up, config);
|
|
3224
3219
|
if (changes.length) {
|
|
3225
3220
|
const from = up ? 0 : changes.length - 1;
|
|
3226
3221
|
const to = up ? changes.length : -1;
|
|
3227
3222
|
const step = up ? 1 : -1;
|
|
3228
3223
|
for (let i = from; i !== to; i += step) {
|
|
3229
|
-
await changes[i](db, up);
|
|
3224
|
+
await changes[i].fn(db, up);
|
|
3230
3225
|
}
|
|
3231
3226
|
}
|
|
3227
|
+
return db.adapter;
|
|
3228
|
+
};
|
|
3229
|
+
const changeMigratedVersion = async (adapter, up, file, config) => {
|
|
3232
3230
|
await (up ? saveMigratedVersion : deleteMigratedVersion)(
|
|
3233
|
-
|
|
3231
|
+
adapter,
|
|
3234
3232
|
file.version,
|
|
3235
3233
|
path.basename(file.path).slice(file.version.length + 1),
|
|
3236
3234
|
config
|
|
@@ -3640,7 +3638,7 @@ const dropDb = async (options, config) => {
|
|
|
3640
3638
|
const resetDb = async (options, config) => {
|
|
3641
3639
|
await dropDb(options, config);
|
|
3642
3640
|
await createDb(options, config);
|
|
3643
|
-
await
|
|
3641
|
+
await fullMigrate({}, options, config);
|
|
3644
3642
|
};
|
|
3645
3643
|
|
|
3646
3644
|
const filterSchema = (table) => `${table} !~ '^pg_' AND ${table} != 'information_schema'`;
|
|
@@ -4604,6 +4602,10 @@ const checkIfIsOuterRecursiveFkey = (data, table, references) => {
|
|
|
4604
4602
|
return false;
|
|
4605
4603
|
};
|
|
4606
4604
|
|
|
4605
|
+
const exhaustive = (_) => {
|
|
4606
|
+
throw new Error("Condition was not exhaustive");
|
|
4607
|
+
};
|
|
4608
|
+
|
|
4607
4609
|
const astToGenerateItems = (config, asts, currentSchema) => {
|
|
4608
4610
|
return asts.map((ast) => astToGenerateItem(config, ast, currentSchema));
|
|
4609
4611
|
};
|
|
@@ -5644,7 +5646,7 @@ const rebase = async (options, config) => {
|
|
|
5644
5646
|
return files2;
|
|
5645
5647
|
}, []).sort((a, b) => +b.version - +a.version);
|
|
5646
5648
|
set.migrations = migrationsDown;
|
|
5647
|
-
await
|
|
5649
|
+
await fullRedo(
|
|
5648
5650
|
ctx,
|
|
5649
5651
|
options,
|
|
5650
5652
|
{
|
|
@@ -5684,25 +5686,26 @@ const rakeDb = (options, partialConfig, args = process.argv.slice(2)) => {
|
|
|
5684
5686
|
}
|
|
5685
5687
|
throw err;
|
|
5686
5688
|
});
|
|
5687
|
-
return Object.assign(
|
|
5689
|
+
return Object.assign(makeChange(config), {
|
|
5688
5690
|
promise
|
|
5689
5691
|
});
|
|
5690
5692
|
};
|
|
5691
5693
|
rakeDb.lazy = (options, partialConfig) => {
|
|
5692
5694
|
const config = processRakeDbConfig(partialConfig);
|
|
5693
5695
|
return {
|
|
5694
|
-
change,
|
|
5696
|
+
change: makeChange(config),
|
|
5695
5697
|
run(args, conf) {
|
|
5696
5698
|
return runCommand(options, conf ? { ...config, ...conf } : config, args);
|
|
5697
5699
|
}
|
|
5698
5700
|
};
|
|
5699
5701
|
};
|
|
5700
|
-
|
|
5701
|
-
|
|
5702
|
-
|
|
5703
|
-
|
|
5702
|
+
const makeChange = (config) => (fn) => {
|
|
5703
|
+
const change = { fn, config };
|
|
5704
|
+
pushChange(change);
|
|
5705
|
+
return change;
|
|
5706
|
+
};
|
|
5704
5707
|
const rakeDbAliases = {
|
|
5705
|
-
|
|
5708
|
+
fullMigrate: "up",
|
|
5706
5709
|
rollback: "down",
|
|
5707
5710
|
s: "status",
|
|
5708
5711
|
rec: "recurrent"
|
|
@@ -5778,7 +5781,7 @@ ${Object.entries(helpArguments).map(
|
|
|
5778
5781
|
};
|
|
5779
5782
|
};
|
|
5780
5783
|
const upCommand = {
|
|
5781
|
-
run: (options, config, args) =>
|
|
5784
|
+
run: (options, config, args) => fullMigrate({}, options, config, args).then(
|
|
5782
5785
|
() => runRecurrentMigrations(options, config)
|
|
5783
5786
|
),
|
|
5784
5787
|
help: "migrate pending migrations",
|
|
@@ -5789,7 +5792,7 @@ const upCommand = {
|
|
|
5789
5792
|
}
|
|
5790
5793
|
};
|
|
5791
5794
|
const downCommand = {
|
|
5792
|
-
run: (options, config, args) =>
|
|
5795
|
+
run: (options, config, args) => fullRollback({}, options, config, args),
|
|
5793
5796
|
help: "rollback migrated migrations",
|
|
5794
5797
|
helpArguments: {
|
|
5795
5798
|
"no arguments": "rollback one last migration",
|
|
@@ -5829,7 +5832,7 @@ const rakeDbCommands = {
|
|
|
5829
5832
|
down: downCommand,
|
|
5830
5833
|
rollback: downCommand,
|
|
5831
5834
|
redo: {
|
|
5832
|
-
run: (options, config, args) =>
|
|
5835
|
+
run: (options, config, args) => fullRedo({}, options, config, args).then(
|
|
5833
5836
|
() => runRecurrentMigrations(options, config)
|
|
5834
5837
|
),
|
|
5835
5838
|
help: "rollback and migrate, run recurrent"
|
|
@@ -5861,89 +5864,43 @@ const rakeDbCommands = {
|
|
|
5861
5864
|
}
|
|
5862
5865
|
};
|
|
5863
5866
|
|
|
5864
|
-
|
|
5865
|
-
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
|
|
5869
|
-
|
|
5870
|
-
|
|
5867
|
+
const migrateFiles = async (db, files) => {
|
|
5868
|
+
const qb = db.$qb;
|
|
5869
|
+
await qb.ensureTransaction(async () => {
|
|
5870
|
+
const adapter = qb.internal.transactionStorage.getStore()?.adapter;
|
|
5871
|
+
for (const load of files) {
|
|
5872
|
+
clearChanges();
|
|
5873
|
+
const changes = await getChanges({ load });
|
|
5874
|
+
const config = changes[0]?.config;
|
|
5875
|
+
await runMigration(adapter, true, changes, config);
|
|
5876
|
+
}
|
|
5877
|
+
});
|
|
5878
|
+
};
|
|
5879
|
+
|
|
5871
5880
|
exports.astToMigration = astToMigration;
|
|
5872
|
-
exports.changeCache = changeCache;
|
|
5873
|
-
exports.changeEnumValues = changeEnumValues;
|
|
5874
|
-
exports.clearChanges = clearChanges;
|
|
5875
|
-
exports.cmpRawSql = cmpRawSql;
|
|
5876
|
-
exports.colors = colors;
|
|
5877
|
-
exports.columnToSql = columnToSql;
|
|
5878
|
-
exports.columnTypeToSql = columnTypeToSql;
|
|
5879
|
-
exports.commentsToQuery = commentsToQuery;
|
|
5880
5881
|
exports.concatSchemaAndName = concatSchemaAndName;
|
|
5881
|
-
exports.constraintToSql = constraintToSql;
|
|
5882
|
-
exports.createDb = createDb;
|
|
5883
5882
|
exports.createMigrationInterface = createMigrationInterface;
|
|
5884
5883
|
exports.dbColumnToAst = dbColumnToAst;
|
|
5885
|
-
exports.deleteMigratedVersion = deleteMigratedVersion;
|
|
5886
|
-
exports.dropDb = dropDb;
|
|
5887
5884
|
exports.encodeColumnDefault = encodeColumnDefault;
|
|
5888
|
-
exports.
|
|
5889
|
-
exports.exhaustive = exhaustive;
|
|
5890
|
-
exports.generateTimeStamp = generateTimeStamp;
|
|
5891
|
-
exports.getColumnName = getColumnName;
|
|
5885
|
+
exports.fullMigrate = fullMigrate;
|
|
5892
5886
|
exports.getConstraintName = getConstraintName;
|
|
5893
|
-
exports.getCurrentChanges = getCurrentChanges;
|
|
5894
|
-
exports.getDatabaseAndUserFromOptions = getDatabaseAndUserFromOptions;
|
|
5895
5887
|
exports.getDbStructureTableData = getDbStructureTableData;
|
|
5896
5888
|
exports.getDbTableColumnsChecks = getDbTableColumnsChecks;
|
|
5897
5889
|
exports.getExcludeName = getExcludeName;
|
|
5898
|
-
exports.getFirstWordAndRest = getFirstWordAndRest;
|
|
5899
|
-
exports.getForeignKeyTable = getForeignKeyTable;
|
|
5900
5890
|
exports.getIndexName = getIndexName;
|
|
5901
|
-
exports.getMigratedVersionsMap = getMigratedVersionsMap;
|
|
5902
5891
|
exports.getSchemaAndTableFromName = getSchemaAndTableFromName;
|
|
5903
|
-
exports.getTextAfterFrom = getTextAfterFrom;
|
|
5904
|
-
exports.getTextAfterTo = getTextAfterTo;
|
|
5905
|
-
exports.identityToSql = identityToSql;
|
|
5906
|
-
exports.indexesToQuery = indexesToQuery;
|
|
5907
5892
|
exports.instantiateDbColumn = instantiateDbColumn;
|
|
5908
|
-
exports.interpolateSqlValues = interpolateSqlValues;
|
|
5909
5893
|
exports.introspectDbSchema = introspectDbSchema;
|
|
5910
|
-
exports.joinColumns = joinColumns;
|
|
5911
|
-
exports.joinWords = joinWords;
|
|
5912
|
-
exports.makeDbStructureColumnsShape = makeDbStructureColumnsShape;
|
|
5913
5894
|
exports.makeDomainsMap = makeDomainsMap;
|
|
5914
5895
|
exports.makeFileVersion = makeFileVersion;
|
|
5915
|
-
exports.makePopulateEnumQuery = makePopulateEnumQuery;
|
|
5916
5896
|
exports.makeStructureToAstCtx = makeStructureToAstCtx;
|
|
5917
|
-
exports.
|
|
5918
|
-
exports.migrateOrRollback = migrateOrRollback;
|
|
5897
|
+
exports.migrateFiles = migrateFiles;
|
|
5919
5898
|
exports.migrationConfigDefaults = migrationConfigDefaults;
|
|
5920
|
-
exports.nameColumnChecks = nameColumnChecks;
|
|
5921
|
-
exports.newMigration = newMigration;
|
|
5922
|
-
exports.pluralize = pluralize;
|
|
5923
|
-
exports.primaryKeyToSql = primaryKeyToSql;
|
|
5924
|
-
exports.processRakeDbConfig = processRakeDbConfig;
|
|
5925
|
-
exports.promptConfirm = promptConfirm;
|
|
5926
5899
|
exports.promptSelect = promptSelect;
|
|
5927
|
-
exports.promptText = promptText;
|
|
5928
|
-
exports.pushChange = pushChange;
|
|
5929
|
-
exports.queryLock = queryLock;
|
|
5930
|
-
exports.quoteCustomType = quoteCustomType;
|
|
5931
|
-
exports.quoteNameFromString = quoteNameFromString;
|
|
5932
|
-
exports.quoteSchemaTable = quoteSchemaTable;
|
|
5933
|
-
exports.quoteTable = quoteTable;
|
|
5934
|
-
exports.quoteWithSchema = quoteWithSchema;
|
|
5935
5900
|
exports.rakeDb = rakeDb;
|
|
5936
|
-
exports.rakeDbAliases = rakeDbAliases;
|
|
5937
5901
|
exports.rakeDbCommands = rakeDbCommands;
|
|
5938
|
-
exports.redo = redo;
|
|
5939
|
-
exports.referencesToSql = referencesToSql;
|
|
5940
|
-
exports.renameType = renameType;
|
|
5941
|
-
exports.resetDb = resetDb;
|
|
5942
|
-
exports.rollback = rollback;
|
|
5943
5902
|
exports.saveMigratedVersion = saveMigratedVersion;
|
|
5944
5903
|
exports.structureToAst = structureToAst;
|
|
5945
5904
|
exports.tableToAst = tableToAst;
|
|
5946
|
-
exports.transaction = transaction;
|
|
5947
|
-
exports.versionToString = versionToString;
|
|
5948
5905
|
exports.writeMigrationFile = writeMigrationFile;
|
|
5949
5906
|
//# sourceMappingURL=index.js.map
|