rake-db 2.8.19 → 2.8.21

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.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { columnTypes, quote, getRaw, EnumColumn, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, referencesArgsToCode, constraintPropsToCode, TimestampTZColumn } from 'pqb';
2
2
  import { getCallerFilePath, singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, emptyObject, consumeColumnName, deepCompare, raw, pathToLog, emptyArray, getImportPath, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey, backtickQuote } from 'orchid-core';
3
- import path from 'path';
4
- import { readdir, mkdir, writeFile } from 'fs/promises';
3
+ import path, { join } from 'path';
4
+ import { readdir, mkdir, writeFile, stat, readFile } from 'fs/promises';
5
5
  import prompts from 'prompts';
6
6
  import * as url from 'url';
7
7
 
@@ -45,6 +45,8 @@ 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)
49
+ result.recurrentPath = path.join(result.migrationsPath, "recurrent");
48
50
  if (config.appCodeUpdater && (!("baseTable" in config) || !config.baseTable)) {
49
51
  throw new Error(
50
52
  "`baseTable` option is required in `rakeDb` for `appCodeUpdater`"
@@ -69,6 +71,9 @@ const processRakeDbConfig = (config) => {
69
71
  result.migrationsPath
70
72
  );
71
73
  }
74
+ if (!path.isAbsolute(result.recurrentPath)) {
75
+ result.recurrentPath = path.resolve(result.basePath, result.recurrentPath);
76
+ }
72
77
  if ("baseTable" in config) {
73
78
  const proto = (_a = config.baseTable) == null ? void 0 : _a.prototype;
74
79
  result.columnTypes = proto.columnTypes || columnTypes;
@@ -193,23 +198,25 @@ const getMigrationFiles = async (config, up) => {
193
198
  return [];
194
199
  }
195
200
  const sort = up ? sortAsc : sortDesc;
196
- return sort(files).map((file) => {
197
- if (!file.endsWith(".ts")) {
198
- throw new Error(
199
- `Only .ts files are supported for migration, received: ${file}`
200
- );
201
- }
202
- const timestampMatch = file.match(/^(\d{14})\D/);
203
- if (!timestampMatch) {
204
- throw new Error(
205
- `Migration file name should start with 14 digit version, received ${file}`
206
- );
201
+ return sort(files.filter((file) => path.basename(file).includes("."))).map(
202
+ (file) => {
203
+ if (!file.endsWith(".ts")) {
204
+ throw new Error(
205
+ `Only .ts files are supported for migration, received: ${file}`
206
+ );
207
+ }
208
+ const timestampMatch = file.match(/^(\d{14})\D/);
209
+ if (!timestampMatch) {
210
+ throw new Error(
211
+ `Migration file name should start with 14 digit version, received ${file}`
212
+ );
213
+ }
214
+ return {
215
+ path: path.resolve(migrationsPath, file),
216
+ version: timestampMatch[1]
217
+ };
207
218
  }
208
- return {
209
- path: path.resolve(migrationsPath, file),
210
- version: timestampMatch[1]
211
- };
212
- });
219
+ );
213
220
  };
214
221
  const sortAsc = (arr) => arr.sort();
215
222
  const sortDesc = (arr) => arr.sort((a, b) => a > b ? -1 : 1);
@@ -1825,9 +1832,9 @@ const createDb = async (arg, config) => {
1825
1832
  });
1826
1833
  }
1827
1834
  };
1828
- const dropDb = async (arg) => {
1835
+ const dropDb = async (arg, config) => {
1829
1836
  for (const options of toArray(arg)) {
1830
- await createOrDrop(options, options, migrationConfigDefaults, {
1837
+ await createOrDrop(options, options, config, {
1831
1838
  sql({ database }) {
1832
1839
  return `DROP DATABASE "${database}"`;
1833
1840
  },
@@ -1841,7 +1848,7 @@ const dropDb = async (arg) => {
1841
1848
  }
1842
1849
  };
1843
1850
  const resetDb = async (arg, config) => {
1844
- await dropDb(arg);
1851
+ await dropDb(arg, config);
1845
1852
  await createDb(arg, config);
1846
1853
  await migrate(arg, config);
1847
1854
  };
@@ -3037,6 +3044,52 @@ Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count >
3037
3044
  adapter.close();
3038
3045
  };
3039
3046
 
3047
+ const runRecurrentMigrations = async (options, config) => {
3048
+ var _a;
3049
+ let dbs;
3050
+ let files = 0;
3051
+ await readdirRecursive(config.recurrentPath, async (path) => {
3052
+ files++;
3053
+ dbs != null ? dbs : dbs = toArray(options).map(
3054
+ (opts) => createDb$1({ adapter: new Adapter(opts) })
3055
+ );
3056
+ const sql = await readFile(path, "utf-8");
3057
+ await Promise.all(
3058
+ dbs.map(async (db) => {
3059
+ await db.adapter.arrays(sql);
3060
+ })
3061
+ );
3062
+ });
3063
+ if (dbs) {
3064
+ await Promise.all(dbs.map((db) => db.close()));
3065
+ if (files > 0) {
3066
+ (_a = config.logger) == null ? void 0 : _a.log(
3067
+ `Applied ${files} recurrent migration file${files > 1 ? "s" : ""}`
3068
+ );
3069
+ }
3070
+ }
3071
+ };
3072
+ const readdirRecursive = async (dirPath, cb) => {
3073
+ const list = await readdir(dirPath).catch((err) => {
3074
+ if (err.code !== "ENOENT")
3075
+ throw err;
3076
+ return;
3077
+ });
3078
+ if (!list)
3079
+ return;
3080
+ await Promise.all(
3081
+ list.map(async (item) => {
3082
+ const path = join(dirPath, item);
3083
+ const info = await stat(path);
3084
+ if (info.isDirectory()) {
3085
+ await readdirRecursive(path, cb);
3086
+ } else if (info.isFile() && path.endsWith(".sql")) {
3087
+ await cb(path);
3088
+ }
3089
+ })
3090
+ );
3091
+ };
3092
+
3040
3093
  const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
3041
3094
  const config = processRakeDbConfig(partialConfig);
3042
3095
  const promise = runCommand(options, config, args);
@@ -3051,29 +3104,32 @@ const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
3051
3104
  };
3052
3105
  const runCommand = async (options, config, args = process.argv.slice(2)) => {
3053
3106
  var _a, _b, _c;
3054
- const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
3107
+ const arg = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
3055
3108
  try {
3056
- if (command === "create") {
3109
+ if (arg === "create") {
3057
3110
  await createDb(options, config);
3058
- } else if (command === "drop") {
3059
- await dropDb(options);
3060
- } else if (command === "reset") {
3111
+ } else if (arg === "drop") {
3112
+ await dropDb(options, config);
3113
+ } else if (arg === "reset") {
3061
3114
  await resetDb(options, config);
3062
- } else if (command === "migrate") {
3115
+ } else if (arg === "up" || arg === "migrate") {
3063
3116
  await migrate(options, config, args.slice(1));
3064
- } else if (command === "rollback") {
3117
+ } else if (arg === "down" || arg === "rollback") {
3065
3118
  await rollback(options, config, args.slice(1));
3066
- } else if (command === "redo") {
3119
+ } else if (arg === "redo") {
3067
3120
  await redo(options, config, args.slice(1));
3068
- } else if (command === "new") {
3121
+ } else if (arg === "new") {
3069
3122
  await generate(config, args.slice(1));
3070
- } else if (command === "pull") {
3123
+ } else if (arg === "pull") {
3071
3124
  await pullDbStructure(toArray(options)[0], config);
3072
- } else if (config.commands[command]) {
3073
- await config.commands[command](toArray(options), config, args.slice(1));
3074
- } else {
3125
+ } else if (config.commands[arg]) {
3126
+ await config.commands[arg](toArray(options), config, args.slice(1));
3127
+ } else if (arg !== "rec" && arg !== "recurrent") {
3075
3128
  (_b = config.logger) == null ? void 0 : _b.log(help);
3076
3129
  }
3130
+ if (arg === "migrate" || arg === "rec" || arg === "recurrent" || arg === "redo") {
3131
+ await runRecurrentMigrations(options, config);
3132
+ }
3077
3133
  } catch (err) {
3078
3134
  if (err instanceof RakeDbError) {
3079
3135
  (_c = config.logger) == null ? void 0 : _c.error(err.message);
@@ -3092,9 +3148,11 @@ Commands:
3092
3148
  drop drop databases
3093
3149
  reset drop, create and migrate databases
3094
3150
  new create new migration file, see below
3095
- migrate migrate pending migrations
3096
- rollback rollback the last migrated
3097
- redo rollback and migrate
3151
+ migrate migrate pending migrations, run recurrent
3152
+ up migrate pending migrations, don't run recurrent
3153
+ rollback or down rollback the last migrated
3154
+ redo rollback and migrate, run recurrent
3155
+ rec or recurrent run recurrent migrations
3098
3156
  no or unknown command prints this message
3099
3157
 
3100
3158
  Migrate arguments: