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.d.ts CHANGED
@@ -282,6 +282,7 @@ type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
282
282
  basePath: string;
283
283
  dbScript: string;
284
284
  migrationsPath: string;
285
+ recurrentPath: string;
285
286
  migrationsTable: string;
286
287
  snakeCase: boolean;
287
288
  commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig<CT>, args: string[]) => Promise<void>>;
@@ -313,7 +314,7 @@ type AppCodeUpdater = {
313
314
  };
314
315
 
315
316
  declare const createDb: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => Promise<void>;
316
- declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
317
+ declare const dropDb: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => Promise<void>;
317
318
  declare const resetDb: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => Promise<void>;
318
319
 
319
320
  declare const writeMigrationFile: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(config: RakeDbConfig<CT>, version: string, name: string, content: (importPath: string, name: string) => string) => Promise<void>;
package/dist/index.js CHANGED
@@ -66,6 +66,8 @@ const migrationConfigDefaults = {
66
66
  const processRakeDbConfig = (config) => {
67
67
  var _a;
68
68
  const result = __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
69
+ if (!result.recurrentPath)
70
+ result.recurrentPath = path.join(result.migrationsPath, "recurrent");
69
71
  if (config.appCodeUpdater && (!("baseTable" in config) || !config.baseTable)) {
70
72
  throw new Error(
71
73
  "`baseTable` option is required in `rakeDb` for `appCodeUpdater`"
@@ -90,6 +92,9 @@ const processRakeDbConfig = (config) => {
90
92
  result.migrationsPath
91
93
  );
92
94
  }
95
+ if (!path.isAbsolute(result.recurrentPath)) {
96
+ result.recurrentPath = path.resolve(result.basePath, result.recurrentPath);
97
+ }
93
98
  if ("baseTable" in config) {
94
99
  const proto = (_a = config.baseTable) == null ? void 0 : _a.prototype;
95
100
  result.columnTypes = proto.columnTypes || pqb.columnTypes;
@@ -214,23 +219,25 @@ const getMigrationFiles = async (config, up) => {
214
219
  return [];
215
220
  }
216
221
  const sort = up ? sortAsc : sortDesc;
217
- return sort(files).map((file) => {
218
- if (!file.endsWith(".ts")) {
219
- throw new Error(
220
- `Only .ts files are supported for migration, received: ${file}`
221
- );
222
- }
223
- const timestampMatch = file.match(/^(\d{14})\D/);
224
- if (!timestampMatch) {
225
- throw new Error(
226
- `Migration file name should start with 14 digit version, received ${file}`
227
- );
222
+ return sort(files.filter((file) => path.basename(file).includes("."))).map(
223
+ (file) => {
224
+ if (!file.endsWith(".ts")) {
225
+ throw new Error(
226
+ `Only .ts files are supported for migration, received: ${file}`
227
+ );
228
+ }
229
+ const timestampMatch = file.match(/^(\d{14})\D/);
230
+ if (!timestampMatch) {
231
+ throw new Error(
232
+ `Migration file name should start with 14 digit version, received ${file}`
233
+ );
234
+ }
235
+ return {
236
+ path: path.resolve(migrationsPath, file),
237
+ version: timestampMatch[1]
238
+ };
228
239
  }
229
- return {
230
- path: path.resolve(migrationsPath, file),
231
- version: timestampMatch[1]
232
- };
233
- });
240
+ );
234
241
  };
235
242
  const sortAsc = (arr) => arr.sort();
236
243
  const sortDesc = (arr) => arr.sort((a, b) => a > b ? -1 : 1);
@@ -1846,9 +1853,9 @@ const createDb = async (arg, config) => {
1846
1853
  });
1847
1854
  }
1848
1855
  };
1849
- const dropDb = async (arg) => {
1856
+ const dropDb = async (arg, config) => {
1850
1857
  for (const options of orchidCore.toArray(arg)) {
1851
- await createOrDrop(options, options, migrationConfigDefaults, {
1858
+ await createOrDrop(options, options, config, {
1852
1859
  sql({ database }) {
1853
1860
  return `DROP DATABASE "${database}"`;
1854
1861
  },
@@ -1862,7 +1869,7 @@ const dropDb = async (arg) => {
1862
1869
  }
1863
1870
  };
1864
1871
  const resetDb = async (arg, config) => {
1865
- await dropDb(arg);
1872
+ await dropDb(arg, config);
1866
1873
  await createDb(arg, config);
1867
1874
  await migrate(arg, config);
1868
1875
  };
@@ -3058,6 +3065,52 @@ Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count >
3058
3065
  adapter.close();
3059
3066
  };
3060
3067
 
3068
+ const runRecurrentMigrations = async (options, config) => {
3069
+ var _a;
3070
+ let dbs;
3071
+ let files = 0;
3072
+ await readdirRecursive(config.recurrentPath, async (path) => {
3073
+ files++;
3074
+ dbs != null ? dbs : dbs = orchidCore.toArray(options).map(
3075
+ (opts) => pqb.createDb({ adapter: new pqb.Adapter(opts) })
3076
+ );
3077
+ const sql = await promises.readFile(path, "utf-8");
3078
+ await Promise.all(
3079
+ dbs.map(async (db) => {
3080
+ await db.adapter.arrays(sql);
3081
+ })
3082
+ );
3083
+ });
3084
+ if (dbs) {
3085
+ await Promise.all(dbs.map((db) => db.close()));
3086
+ if (files > 0) {
3087
+ (_a = config.logger) == null ? void 0 : _a.log(
3088
+ `Applied ${files} recurrent migration file${files > 1 ? "s" : ""}`
3089
+ );
3090
+ }
3091
+ }
3092
+ };
3093
+ const readdirRecursive = async (dirPath, cb) => {
3094
+ const list = await promises.readdir(dirPath).catch((err) => {
3095
+ if (err.code !== "ENOENT")
3096
+ throw err;
3097
+ return;
3098
+ });
3099
+ if (!list)
3100
+ return;
3101
+ await Promise.all(
3102
+ list.map(async (item) => {
3103
+ const path$1 = path.join(dirPath, item);
3104
+ const info = await promises.stat(path$1);
3105
+ if (info.isDirectory()) {
3106
+ await readdirRecursive(path$1, cb);
3107
+ } else if (info.isFile() && path$1.endsWith(".sql")) {
3108
+ await cb(path$1);
3109
+ }
3110
+ })
3111
+ );
3112
+ };
3113
+
3061
3114
  const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
3062
3115
  const config = processRakeDbConfig(partialConfig);
3063
3116
  const promise = runCommand(options, config, args);
@@ -3072,29 +3125,32 @@ const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
3072
3125
  };
3073
3126
  const runCommand = async (options, config, args = process.argv.slice(2)) => {
3074
3127
  var _a, _b, _c;
3075
- const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
3128
+ const arg = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
3076
3129
  try {
3077
- if (command === "create") {
3130
+ if (arg === "create") {
3078
3131
  await createDb(options, config);
3079
- } else if (command === "drop") {
3080
- await dropDb(options);
3081
- } else if (command === "reset") {
3132
+ } else if (arg === "drop") {
3133
+ await dropDb(options, config);
3134
+ } else if (arg === "reset") {
3082
3135
  await resetDb(options, config);
3083
- } else if (command === "migrate") {
3136
+ } else if (arg === "up" || arg === "migrate") {
3084
3137
  await migrate(options, config, args.slice(1));
3085
- } else if (command === "rollback") {
3138
+ } else if (arg === "down" || arg === "rollback") {
3086
3139
  await rollback(options, config, args.slice(1));
3087
- } else if (command === "redo") {
3140
+ } else if (arg === "redo") {
3088
3141
  await redo(options, config, args.slice(1));
3089
- } else if (command === "new") {
3142
+ } else if (arg === "new") {
3090
3143
  await generate(config, args.slice(1));
3091
- } else if (command === "pull") {
3144
+ } else if (arg === "pull") {
3092
3145
  await pullDbStructure(orchidCore.toArray(options)[0], config);
3093
- } else if (config.commands[command]) {
3094
- await config.commands[command](orchidCore.toArray(options), config, args.slice(1));
3095
- } else {
3146
+ } else if (config.commands[arg]) {
3147
+ await config.commands[arg](orchidCore.toArray(options), config, args.slice(1));
3148
+ } else if (arg !== "rec" && arg !== "recurrent") {
3096
3149
  (_b = config.logger) == null ? void 0 : _b.log(help);
3097
3150
  }
3151
+ if (arg === "migrate" || arg === "rec" || arg === "recurrent" || arg === "redo") {
3152
+ await runRecurrentMigrations(options, config);
3153
+ }
3098
3154
  } catch (err) {
3099
3155
  if (err instanceof RakeDbError) {
3100
3156
  (_c = config.logger) == null ? void 0 : _c.error(err.message);
@@ -3113,9 +3169,11 @@ Commands:
3113
3169
  drop drop databases
3114
3170
  reset drop, create and migrate databases
3115
3171
  new create new migration file, see below
3116
- migrate migrate pending migrations
3117
- rollback rollback the last migrated
3118
- redo rollback and migrate
3172
+ migrate migrate pending migrations, run recurrent
3173
+ up migrate pending migrations, don't run recurrent
3174
+ rollback or down rollback the last migrated
3175
+ redo rollback and migrate, run recurrent
3176
+ rec or recurrent run recurrent migrations
3119
3177
  no or unknown command prints this message
3120
3178
 
3121
3179
  Migrate arguments: