rake-db 2.8.18 → 2.8.20

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;
@@ -1846,9 +1851,9 @@ const createDb = async (arg, config) => {
1846
1851
  });
1847
1852
  }
1848
1853
  };
1849
- const dropDb = async (arg) => {
1854
+ const dropDb = async (arg, config) => {
1850
1855
  for (const options of orchidCore.toArray(arg)) {
1851
- await createOrDrop(options, options, migrationConfigDefaults, {
1856
+ await createOrDrop(options, options, config, {
1852
1857
  sql({ database }) {
1853
1858
  return `DROP DATABASE "${database}"`;
1854
1859
  },
@@ -1862,7 +1867,7 @@ const dropDb = async (arg) => {
1862
1867
  }
1863
1868
  };
1864
1869
  const resetDb = async (arg, config) => {
1865
- await dropDb(arg);
1870
+ await dropDb(arg, config);
1866
1871
  await createDb(arg, config);
1867
1872
  await migrate(arg, config);
1868
1873
  };
@@ -3058,6 +3063,52 @@ Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count >
3058
3063
  adapter.close();
3059
3064
  };
3060
3065
 
3066
+ const runRecurrentMigrations = async (options, config) => {
3067
+ var _a;
3068
+ let dbs;
3069
+ let files = 0;
3070
+ await readdirRecursive(config.recurrentPath, async (path) => {
3071
+ files++;
3072
+ dbs != null ? dbs : dbs = orchidCore.toArray(options).map(
3073
+ (opts) => pqb.createDb({ adapter: new pqb.Adapter(opts) })
3074
+ );
3075
+ const sql = await promises.readFile(path, "utf-8");
3076
+ await Promise.all(
3077
+ dbs.map(async (db) => {
3078
+ await db.adapter.arrays(sql);
3079
+ })
3080
+ );
3081
+ });
3082
+ if (dbs) {
3083
+ await Promise.all(dbs.map((db) => db.close()));
3084
+ if (files > 0) {
3085
+ (_a = config.logger) == null ? void 0 : _a.log(
3086
+ `Applied ${files} recurrent migration file${files > 1 ? "s" : ""}`
3087
+ );
3088
+ }
3089
+ }
3090
+ };
3091
+ const readdirRecursive = async (dirPath, cb) => {
3092
+ const list = await promises.readdir(dirPath).catch((err) => {
3093
+ if (err.code !== "ENOENT")
3094
+ throw err;
3095
+ return;
3096
+ });
3097
+ if (!list)
3098
+ return;
3099
+ await Promise.all(
3100
+ list.map(async (item) => {
3101
+ const path$1 = path.join(dirPath, item);
3102
+ const info = await promises.stat(path$1);
3103
+ if (info.isDirectory()) {
3104
+ await readdirRecursive(path$1, cb);
3105
+ } else if (info.isFile() && path$1.endsWith(".sql")) {
3106
+ await cb(path$1);
3107
+ }
3108
+ })
3109
+ );
3110
+ };
3111
+
3061
3112
  const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
3062
3113
  const config = processRakeDbConfig(partialConfig);
3063
3114
  const promise = runCommand(options, config, args);
@@ -3072,29 +3123,32 @@ const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
3072
3123
  };
3073
3124
  const runCommand = async (options, config, args = process.argv.slice(2)) => {
3074
3125
  var _a, _b, _c;
3075
- const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
3126
+ const arg = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
3076
3127
  try {
3077
- if (command === "create") {
3128
+ if (arg === "create") {
3078
3129
  await createDb(options, config);
3079
- } else if (command === "drop") {
3080
- await dropDb(options);
3081
- } else if (command === "reset") {
3130
+ } else if (arg === "drop") {
3131
+ await dropDb(options, config);
3132
+ } else if (arg === "reset") {
3082
3133
  await resetDb(options, config);
3083
- } else if (command === "migrate") {
3134
+ } else if (arg === "up" || arg === "migrate") {
3084
3135
  await migrate(options, config, args.slice(1));
3085
- } else if (command === "rollback") {
3136
+ } else if (arg === "down" || arg === "rollback") {
3086
3137
  await rollback(options, config, args.slice(1));
3087
- } else if (command === "redo") {
3138
+ } else if (arg === "redo") {
3088
3139
  await redo(options, config, args.slice(1));
3089
- } else if (command === "new") {
3140
+ } else if (arg === "new") {
3090
3141
  await generate(config, args.slice(1));
3091
- } else if (command === "pull") {
3142
+ } else if (arg === "pull") {
3092
3143
  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 {
3144
+ } else if (config.commands[arg]) {
3145
+ await config.commands[arg](orchidCore.toArray(options), config, args.slice(1));
3146
+ } else if (arg !== "rec" && arg !== "recurrent") {
3096
3147
  (_b = config.logger) == null ? void 0 : _b.log(help);
3097
3148
  }
3149
+ if (arg === "migrate" || arg === "rec" || arg === "recurrent" || arg === "redo") {
3150
+ await runRecurrentMigrations(options, config);
3151
+ }
3098
3152
  } catch (err) {
3099
3153
  if (err instanceof RakeDbError) {
3100
3154
  (_c = config.logger) == null ? void 0 : _c.error(err.message);
@@ -3113,9 +3167,11 @@ Commands:
3113
3167
  drop drop databases
3114
3168
  reset drop, create and migrate databases
3115
3169
  new create new migration file, see below
3116
- migrate migrate pending migrations
3117
- rollback rollback the last migrated
3118
- redo rollback and migrate
3170
+ migrate migrate pending migrations, run recurrent
3171
+ up migrate pending migrations, don't run recurrent
3172
+ rollback or down rollback the last migrated
3173
+ redo rollback and migrate, run recurrent
3174
+ rec or recurrent run recurrent migrations
3119
3175
  no or unknown command prints this message
3120
3176
 
3121
3177
  Migrate arguments: