rake-db 2.10.25 → 2.10.27
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 +3 -1
- package/dist/index.js +61 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -33
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { getCallerFilePath, singleQuote, toSnakeCase, isRawSQL, toArray, snakeCa
|
|
|
3
3
|
import path, { join } from 'path';
|
|
4
4
|
import { readdir, mkdir, writeFile, stat, readFile } from 'fs/promises';
|
|
5
5
|
import prompts from 'prompts';
|
|
6
|
-
import
|
|
6
|
+
import { pathToFileURL } from 'url';
|
|
7
7
|
|
|
8
8
|
var __defProp$6 = Object.defineProperty;
|
|
9
9
|
var __defProps$5 = Object.defineProperties;
|
|
@@ -45,8 +45,9 @@ const migrationConfigDefaults = {
|
|
|
45
45
|
const processRakeDbConfig = (config) => {
|
|
46
46
|
var _a;
|
|
47
47
|
const result = __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
|
|
48
|
-
if (!result.recurrentPath)
|
|
48
|
+
if (!result.recurrentPath) {
|
|
49
49
|
result.recurrentPath = path.join(result.migrationsPath, "recurrent");
|
|
50
|
+
}
|
|
50
51
|
if (config.appCodeUpdater && (!("baseTable" in config) || !config.baseTable)) {
|
|
51
52
|
throw new Error(
|
|
52
53
|
"`baseTable` option is required in `rakeDb` for `appCodeUpdater`"
|
|
@@ -62,16 +63,22 @@ const processRakeDbConfig = (config) => {
|
|
|
62
63
|
"Failed to determine path to db script. Please set basePath option of rakeDb"
|
|
63
64
|
);
|
|
64
65
|
}
|
|
66
|
+
const ext = path.extname(filePath);
|
|
67
|
+
if (ext !== ".ts" && ext !== ".js" && ext !== ".mjs") {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Add a .ts suffix to the "${path.basename(filePath)}" when calling it`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
65
72
|
result.basePath = path.dirname(filePath);
|
|
66
73
|
result.dbScript = path.basename(filePath);
|
|
67
74
|
}
|
|
68
|
-
if (!path.isAbsolute(result.migrationsPath)) {
|
|
75
|
+
if ("migrationsPath" in result && !path.isAbsolute(result.migrationsPath)) {
|
|
69
76
|
result.migrationsPath = path.resolve(
|
|
70
77
|
result.basePath,
|
|
71
78
|
result.migrationsPath
|
|
72
79
|
);
|
|
73
80
|
}
|
|
74
|
-
if (!path.isAbsolute(result.recurrentPath)) {
|
|
81
|
+
if ("recurrentPath" in result && !path.isAbsolute(result.recurrentPath)) {
|
|
75
82
|
result.recurrentPath = path.resolve(result.basePath, result.recurrentPath);
|
|
76
83
|
}
|
|
77
84
|
if ("baseTable" in config) {
|
|
@@ -202,35 +209,63 @@ const getTextAfterTo = (input) => {
|
|
|
202
209
|
const getTextAfterFrom = (input) => {
|
|
203
210
|
return getTextAfterRegExp(input, /(From|-from|_from)[A-Z-_]/, 4);
|
|
204
211
|
};
|
|
205
|
-
const
|
|
206
|
-
|
|
212
|
+
const getMigrations = async (config, up) => {
|
|
213
|
+
if ("migrations" in config) {
|
|
214
|
+
const result = [];
|
|
215
|
+
const { migrations, basePath } = config;
|
|
216
|
+
for (const key in migrations) {
|
|
217
|
+
result.push({
|
|
218
|
+
path: path.resolve(basePath, key),
|
|
219
|
+
version: getVersion(path.basename(key)),
|
|
220
|
+
change: migrations[key]
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
const { migrationsPath, import: imp } = config;
|
|
207
226
|
let files;
|
|
208
227
|
try {
|
|
209
228
|
files = await readdir(migrationsPath);
|
|
210
229
|
} catch (_) {
|
|
211
230
|
return [];
|
|
212
231
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
232
|
+
files = files.filter((file) => path.basename(file).includes("."));
|
|
233
|
+
files = (up ? sortAsc : sortDesc)(files);
|
|
234
|
+
return files.map((file) => {
|
|
235
|
+
checkExt(file);
|
|
236
|
+
const filePath = path.resolve(migrationsPath, file);
|
|
237
|
+
return {
|
|
238
|
+
path: filePath,
|
|
239
|
+
version: getVersion(file),
|
|
240
|
+
async change() {
|
|
241
|
+
try {
|
|
242
|
+
await imp(filePath);
|
|
243
|
+
} catch (err) {
|
|
244
|
+
if (err.code !== "ERR_UNSUPPORTED_ESM_URL_SCHEME")
|
|
245
|
+
throw err;
|
|
246
|
+
await imp(pathToFileURL(filePath).pathname);
|
|
247
|
+
}
|
|
226
248
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
version: timestampMatch[1]
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
);
|
|
249
|
+
};
|
|
250
|
+
});
|
|
233
251
|
};
|
|
252
|
+
function checkExt(filePath) {
|
|
253
|
+
const ext = path.extname(filePath);
|
|
254
|
+
if (ext !== ".ts" && ext !== ".js" && ext !== ".mjs") {
|
|
255
|
+
throw new Error(
|
|
256
|
+
`Only .ts and .js files are supported for migration, received: ${path}`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function getVersion(path2) {
|
|
261
|
+
const timestampMatch = path2.match(/^(\d{14})\D/);
|
|
262
|
+
if (!timestampMatch) {
|
|
263
|
+
throw new Error(
|
|
264
|
+
`Migration file name should start with 14 digit version, received ${path2}`
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return timestampMatch[1];
|
|
268
|
+
}
|
|
234
269
|
const sortAsc = (arr) => arr.sort();
|
|
235
270
|
const sortDesc = (arr) => arr.sort((a, b) => a > b ? -1 : 1);
|
|
236
271
|
const joinColumns = (columns) => {
|
|
@@ -2168,7 +2203,7 @@ const getDb = (adapter) => createDb$1({ adapter });
|
|
|
2168
2203
|
const migrateOrRollback = async (options, config, args, up) => {
|
|
2169
2204
|
var _a, _b, _c, _d, _e, _f;
|
|
2170
2205
|
config = __spreadValues$1({}, config);
|
|
2171
|
-
const files = await
|
|
2206
|
+
const files = await getMigrations(config, up);
|
|
2172
2207
|
let count = up ? Infinity : 1;
|
|
2173
2208
|
let argI = 0;
|
|
2174
2209
|
const num = args[0] === "all" ? Infinity : parseInt(args[0]);
|
|
@@ -2242,13 +2277,7 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
|
|
|
2242
2277
|
clearChanges();
|
|
2243
2278
|
let changes = changeCache[file.path];
|
|
2244
2279
|
if (!changes) {
|
|
2245
|
-
|
|
2246
|
-
await config.import(file.path);
|
|
2247
|
-
} catch (err) {
|
|
2248
|
-
if (err.code !== "ERR_UNSUPPORTED_ESM_URL_SCHEME")
|
|
2249
|
-
throw err;
|
|
2250
|
-
await config.import(url.pathToFileURL(file.path).pathname);
|
|
2251
|
-
}
|
|
2280
|
+
await file.change();
|
|
2252
2281
|
changes = getCurrentChanges();
|
|
2253
2282
|
changeCache[file.path] = changes;
|
|
2254
2283
|
}
|