rake-db 2.10.25 → 2.10.26

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
@@ -968,21 +968,23 @@ type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
968
968
  basePath: string;
969
969
  dbScript: string;
970
970
  migrationsPath: string;
971
+ migrations?: ModuleExportsRecord;
971
972
  recurrentPath: string;
972
973
  migrationsTable: string;
973
974
  snakeCase: boolean;
974
975
  language?: string;
975
976
  commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig<CT>, args: string[]) => Promise<void>>;
976
- import(path: string): Promise<unknown>;
977
977
  noPrimaryKey?: NoPrimaryKeyOption;
978
978
  baseTable?: BaseTable<CT>;
979
979
  appCodeUpdater?: AppCodeUpdater;
980
980
  useCodeUpdater?: boolean;
981
+ import(path: string): Promise<unknown>;
981
982
  beforeMigrate?(db: Db): Promise<void>;
982
983
  afterMigrate?(db: Db): Promise<void>;
983
984
  beforeRollback?(db: Db): Promise<void>;
984
985
  afterRollback?(db: Db): Promise<void>;
985
986
  } & QueryLogOptions;
987
+ type ModuleExportsRecord = Record<string, () => Promise<unknown>>;
986
988
  type AppCodeUpdaterParams = {
987
989
  options: AdapterOptions;
988
990
  basePath: string;
package/dist/index.js CHANGED
@@ -7,25 +7,6 @@ var promises = require('fs/promises');
7
7
  var prompts = require('prompts');
8
8
  var url = require('url');
9
9
 
10
- function _interopNamespaceDefault(e) {
11
- var n = Object.create(null);
12
- if (e) {
13
- Object.keys(e).forEach(function (k) {
14
- if (k !== 'default') {
15
- var d = Object.getOwnPropertyDescriptor(e, k);
16
- Object.defineProperty(n, k, d.get ? d : {
17
- enumerable: true,
18
- get: function () { return e[k]; }
19
- });
20
- }
21
- });
22
- }
23
- n.default = e;
24
- return Object.freeze(n);
25
- }
26
-
27
- var url__namespace = /*#__PURE__*/_interopNamespaceDefault(url);
28
-
29
10
  var __defProp$6 = Object.defineProperty;
30
11
  var __defProps$5 = Object.defineProperties;
31
12
  var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
@@ -66,8 +47,9 @@ const migrationConfigDefaults = {
66
47
  const processRakeDbConfig = (config) => {
67
48
  var _a;
68
49
  const result = __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
69
- if (!result.recurrentPath)
50
+ if (!result.recurrentPath) {
70
51
  result.recurrentPath = path.join(result.migrationsPath, "recurrent");
52
+ }
71
53
  if (config.appCodeUpdater && (!("baseTable" in config) || !config.baseTable)) {
72
54
  throw new Error(
73
55
  "`baseTable` option is required in `rakeDb` for `appCodeUpdater`"
@@ -86,13 +68,13 @@ const processRakeDbConfig = (config) => {
86
68
  result.basePath = path.dirname(filePath);
87
69
  result.dbScript = path.basename(filePath);
88
70
  }
89
- if (!path.isAbsolute(result.migrationsPath)) {
71
+ if ("migrationsPath" in result && !path.isAbsolute(result.migrationsPath)) {
90
72
  result.migrationsPath = path.resolve(
91
73
  result.basePath,
92
74
  result.migrationsPath
93
75
  );
94
76
  }
95
- if (!path.isAbsolute(result.recurrentPath)) {
77
+ if ("recurrentPath" in result && !path.isAbsolute(result.recurrentPath)) {
96
78
  result.recurrentPath = path.resolve(result.basePath, result.recurrentPath);
97
79
  }
98
80
  if ("baseTable" in config) {
@@ -223,35 +205,63 @@ const getTextAfterTo = (input) => {
223
205
  const getTextAfterFrom = (input) => {
224
206
  return getTextAfterRegExp(input, /(From|-from|_from)[A-Z-_]/, 4);
225
207
  };
226
- const getMigrationFiles = async (config, up) => {
227
- const { migrationsPath } = config;
208
+ const getMigrations = async (config, up) => {
209
+ if ("migrations" in config) {
210
+ const result = [];
211
+ const { migrations, basePath } = config;
212
+ for (const key in migrations) {
213
+ result.push({
214
+ path: path.resolve(basePath, key),
215
+ version: getVersion(path.basename(key)),
216
+ change: migrations[key]
217
+ });
218
+ }
219
+ return result;
220
+ }
221
+ const { migrationsPath, import: imp } = config;
228
222
  let files;
229
223
  try {
230
224
  files = await promises.readdir(migrationsPath);
231
225
  } catch (_) {
232
226
  return [];
233
227
  }
234
- const sort = up ? sortAsc : sortDesc;
235
- return sort(files.filter((file) => path.basename(file).includes("."))).map(
236
- (file) => {
237
- if (!file.endsWith(".ts")) {
238
- throw new Error(
239
- `Only .ts files are supported for migration, received: ${file}`
240
- );
241
- }
242
- const timestampMatch = file.match(/^(\d{14})\D/);
243
- if (!timestampMatch) {
244
- throw new Error(
245
- `Migration file name should start with 14 digit version, received ${file}`
246
- );
228
+ files = files.filter((file) => path.basename(file).includes("."));
229
+ files = (up ? sortAsc : sortDesc)(files);
230
+ return files.map((file) => {
231
+ checkExt(file);
232
+ const filePath = path.resolve(migrationsPath, file);
233
+ return {
234
+ path: filePath,
235
+ version: getVersion(file),
236
+ async change() {
237
+ try {
238
+ await imp(filePath);
239
+ } catch (err) {
240
+ if (err.code !== "ERR_UNSUPPORTED_ESM_URL_SCHEME")
241
+ throw err;
242
+ await imp(url.pathToFileURL(filePath).pathname);
243
+ }
247
244
  }
248
- return {
249
- path: path.resolve(migrationsPath, file),
250
- version: timestampMatch[1]
251
- };
252
- }
253
- );
245
+ };
246
+ });
254
247
  };
248
+ function checkExt(path2) {
249
+ const ext = path2.slice(-3);
250
+ if (ext !== ".ts" && ext !== ".js") {
251
+ throw new Error(
252
+ `Only .ts and .js files are supported for migration, received: ${path2}`
253
+ );
254
+ }
255
+ }
256
+ function getVersion(path2) {
257
+ const timestampMatch = path2.match(/^(\d{14})\D/);
258
+ if (!timestampMatch) {
259
+ throw new Error(
260
+ `Migration file name should start with 14 digit version, received ${path2}`
261
+ );
262
+ }
263
+ return timestampMatch[1];
264
+ }
255
265
  const sortAsc = (arr) => arr.sort();
256
266
  const sortDesc = (arr) => arr.sort((a, b) => a > b ? -1 : 1);
257
267
  const joinColumns = (columns) => {
@@ -2189,7 +2199,7 @@ const getDb = (adapter) => pqb.createDb({ adapter });
2189
2199
  const migrateOrRollback = async (options, config, args, up) => {
2190
2200
  var _a, _b, _c, _d, _e, _f;
2191
2201
  config = __spreadValues$1({}, config);
2192
- const files = await getMigrationFiles(config, up);
2202
+ const files = await getMigrations(config, up);
2193
2203
  let count = up ? Infinity : 1;
2194
2204
  let argI = 0;
2195
2205
  const num = args[0] === "all" ? Infinity : parseInt(args[0]);
@@ -2263,13 +2273,7 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
2263
2273
  clearChanges();
2264
2274
  let changes = changeCache[file.path];
2265
2275
  if (!changes) {
2266
- try {
2267
- await config.import(file.path);
2268
- } catch (err) {
2269
- if (err.code !== "ERR_UNSUPPORTED_ESM_URL_SCHEME")
2270
- throw err;
2271
- await config.import(url__namespace.pathToFileURL(file.path).pathname);
2272
- }
2276
+ await file.change();
2273
2277
  changes = getCurrentChanges();
2274
2278
  changeCache[file.path] = changes;
2275
2279
  }