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 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`"
@@ -83,16 +65,22 @@ const processRakeDbConfig = (config) => {
83
65
  "Failed to determine path to db script. Please set basePath option of rakeDb"
84
66
  );
85
67
  }
68
+ const ext = path.extname(filePath);
69
+ if (ext !== ".ts" && ext !== ".js" && ext !== ".mjs") {
70
+ throw new Error(
71
+ `Add a .ts suffix to the "${path.basename(filePath)}" when calling it`
72
+ );
73
+ }
86
74
  result.basePath = path.dirname(filePath);
87
75
  result.dbScript = path.basename(filePath);
88
76
  }
89
- if (!path.isAbsolute(result.migrationsPath)) {
77
+ if ("migrationsPath" in result && !path.isAbsolute(result.migrationsPath)) {
90
78
  result.migrationsPath = path.resolve(
91
79
  result.basePath,
92
80
  result.migrationsPath
93
81
  );
94
82
  }
95
- if (!path.isAbsolute(result.recurrentPath)) {
83
+ if ("recurrentPath" in result && !path.isAbsolute(result.recurrentPath)) {
96
84
  result.recurrentPath = path.resolve(result.basePath, result.recurrentPath);
97
85
  }
98
86
  if ("baseTable" in config) {
@@ -223,35 +211,63 @@ const getTextAfterTo = (input) => {
223
211
  const getTextAfterFrom = (input) => {
224
212
  return getTextAfterRegExp(input, /(From|-from|_from)[A-Z-_]/, 4);
225
213
  };
226
- const getMigrationFiles = async (config, up) => {
227
- const { migrationsPath } = config;
214
+ const getMigrations = async (config, up) => {
215
+ if ("migrations" in config) {
216
+ const result = [];
217
+ const { migrations, basePath } = config;
218
+ for (const key in migrations) {
219
+ result.push({
220
+ path: path.resolve(basePath, key),
221
+ version: getVersion(path.basename(key)),
222
+ change: migrations[key]
223
+ });
224
+ }
225
+ return result;
226
+ }
227
+ const { migrationsPath, import: imp } = config;
228
228
  let files;
229
229
  try {
230
230
  files = await promises.readdir(migrationsPath);
231
231
  } catch (_) {
232
232
  return [];
233
233
  }
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
- );
234
+ files = files.filter((file) => path.basename(file).includes("."));
235
+ files = (up ? sortAsc : sortDesc)(files);
236
+ return files.map((file) => {
237
+ checkExt(file);
238
+ const filePath = path.resolve(migrationsPath, file);
239
+ return {
240
+ path: filePath,
241
+ version: getVersion(file),
242
+ async change() {
243
+ try {
244
+ await imp(filePath);
245
+ } catch (err) {
246
+ if (err.code !== "ERR_UNSUPPORTED_ESM_URL_SCHEME")
247
+ throw err;
248
+ await imp(url.pathToFileURL(filePath).pathname);
249
+ }
247
250
  }
248
- return {
249
- path: path.resolve(migrationsPath, file),
250
- version: timestampMatch[1]
251
- };
252
- }
253
- );
251
+ };
252
+ });
254
253
  };
254
+ function checkExt(filePath) {
255
+ const ext = path.extname(filePath);
256
+ if (ext !== ".ts" && ext !== ".js" && ext !== ".mjs") {
257
+ throw new Error(
258
+ `Only .ts and .js files are supported for migration, received: ${path}`
259
+ );
260
+ }
261
+ }
262
+ function getVersion(path2) {
263
+ const timestampMatch = path2.match(/^(\d{14})\D/);
264
+ if (!timestampMatch) {
265
+ throw new Error(
266
+ `Migration file name should start with 14 digit version, received ${path2}`
267
+ );
268
+ }
269
+ return timestampMatch[1];
270
+ }
255
271
  const sortAsc = (arr) => arr.sort();
256
272
  const sortDesc = (arr) => arr.sort((a, b) => a > b ? -1 : 1);
257
273
  const joinColumns = (columns) => {
@@ -2189,7 +2205,7 @@ const getDb = (adapter) => pqb.createDb({ adapter });
2189
2205
  const migrateOrRollback = async (options, config, args, up) => {
2190
2206
  var _a, _b, _c, _d, _e, _f;
2191
2207
  config = __spreadValues$1({}, config);
2192
- const files = await getMigrationFiles(config, up);
2208
+ const files = await getMigrations(config, up);
2193
2209
  let count = up ? Infinity : 1;
2194
2210
  let argI = 0;
2195
2211
  const num = args[0] === "all" ? Infinity : parseInt(args[0]);
@@ -2263,13 +2279,7 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
2263
2279
  clearChanges();
2264
2280
  let changes = changeCache[file.path];
2265
2281
  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
- }
2282
+ await file.change();
2273
2283
  changes = getCurrentChanges();
2274
2284
  changeCache[file.path] = changes;
2275
2285
  }