rake-db 2.14.4 → 2.14.5

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
@@ -2205,9 +2205,9 @@ const removeMigratedVersion = async (db, version, config) => {
2205
2205
  };
2206
2206
  class NoMigrationsTableError extends Error {
2207
2207
  }
2208
- const getMigratedVersionsMap = async (db, config) => {
2208
+ const getMigratedVersionsMap = async (adapter, config) => {
2209
2209
  try {
2210
- const result = await db.arrays(
2210
+ const result = await adapter.arrays(
2211
2211
  `SELECT * FROM ${quoteWithSchema({ name: config.migrationsTable })}`
2212
2212
  );
2213
2213
  return Object.fromEntries(result.rows.map((row) => [row[0], true]));
@@ -3749,6 +3749,96 @@ const readdirRecursive = async (dirPath, cb) => {
3749
3749
  );
3750
3750
  };
3751
3751
 
3752
+ const listMigrationsStatuses = async (options, config, args) => {
3753
+ var _a, _b;
3754
+ const adapters = options.map((opts) => new Adapter(opts));
3755
+ const [migrations, ...migrated] = await Promise.all([
3756
+ getMigrations(config, true),
3757
+ ...adapters.map((adapter) => getMigratedVersionsMap(adapter, config))
3758
+ ]);
3759
+ const map = {};
3760
+ let maxVersionLength = 12;
3761
+ let maxNameLength = 4;
3762
+ for (let i = 0; i < options.length; i++) {
3763
+ const list = migrated[i];
3764
+ const key = Object.entries(list).map(([version, up]) => `${version}${up ? "t" : "f"}`).join("");
3765
+ const database = options[i].database || new URL(options[i].databaseURL).pathname.slice(1);
3766
+ if (map[key]) {
3767
+ map[key].databases.push(database);
3768
+ continue;
3769
+ }
3770
+ map[key] = {
3771
+ databases: [database],
3772
+ migrations: migrations.map((item) => {
3773
+ if (item.version.length > maxVersionLength) {
3774
+ maxVersionLength = item.version.length;
3775
+ }
3776
+ const name = path.parse(item.path).name.slice(item.version.length + 1).replace(
3777
+ /([a-z])([A-Z])/g,
3778
+ (_, a, b) => `${a} ${b.toLocaleLowerCase()}`
3779
+ ).replace(/[-_](.)/g, (_, char) => ` ${char.toLocaleLowerCase()}`).replace(/^\w/, (match) => match.toLocaleUpperCase());
3780
+ if (name.length > maxNameLength) {
3781
+ maxNameLength = name.length;
3782
+ }
3783
+ return {
3784
+ up: list[item.version],
3785
+ version: item.version,
3786
+ name,
3787
+ url: pathToFileURL(item.path)
3788
+ };
3789
+ })
3790
+ };
3791
+ }
3792
+ const showUrl = args.includes("p") || args.includes("path");
3793
+ const colors = typeof config.log === "object" ? (_a = config.log.colors) != null ? _a : true : true;
3794
+ const yellow = colors ? (s) => `\x1B[33m${s}\x1B[0m` : (s) => s;
3795
+ const green = colors ? (s) => `\x1B[32m${s}\x1B[0m` : (s) => s;
3796
+ const red = colors ? (s) => `\x1B[31m${s}\x1B[0m` : (s) => s;
3797
+ const blue = colors ? (s) => `\x1B[34m${s}\x1B[0m` : (s) => s;
3798
+ const log = Object.values(map).map(({ databases, migrations: migrations2 }) => {
3799
+ let log2 = ` ${yellow("Database:")} ${databases.join(", ")}`;
3800
+ if (migrations2.length === 0) {
3801
+ return log2 + `
3802
+
3803
+ No migrations available`;
3804
+ }
3805
+ const lineSeparator = yellow(
3806
+ makeChars(14 + maxVersionLength + maxNameLength, "-")
3807
+ );
3808
+ const columnSeparator = yellow("|");
3809
+ log2 += "\n\n " + yellow(
3810
+ `Status | Migration ID${makeChars(
3811
+ maxVersionLength - 12,
3812
+ " "
3813
+ )} | Name
3814
+ ${lineSeparator}`
3815
+ );
3816
+ for (const migration of migrations2) {
3817
+ log2 += `
3818
+ ${migration.up ? ` ${green("Up")} ` : red("Down")} ${columnSeparator} ${blue(migration.version)}${makeChars(
3819
+ maxVersionLength - migration.version.length,
3820
+ " "
3821
+ )} ${columnSeparator} ${migration.name}`;
3822
+ if (showUrl) {
3823
+ log2 += `
3824
+ ${migration.url}
3825
+ `;
3826
+ }
3827
+ }
3828
+ return log2 += showUrl ? lineSeparator : `
3829
+ ${lineSeparator}`;
3830
+ }).join("\n\n");
3831
+ ((_b = config.logger) != null ? _b : console).log(log);
3832
+ await Promise.all(adapters.map((adapter) => adapter.close()));
3833
+ };
3834
+ const makeChars = (count, char) => {
3835
+ let chars = "";
3836
+ for (let i = 0; i < count; i++) {
3837
+ chars += char;
3838
+ }
3839
+ return chars;
3840
+ };
3841
+
3752
3842
  var __defProp = Object.defineProperty;
3753
3843
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3754
3844
  var __hasOwnProp = Object.prototype.hasOwnProperty;
@@ -3815,6 +3905,8 @@ const runCommand = async (options, config, args = process.argv.slice(2)) => {
3815
3905
  await generate(config, args.slice(1));
3816
3906
  } else if (arg === "pull") {
3817
3907
  await pullDbStructure(toArray(options)[0], config);
3908
+ } else if (arg === "status" || arg === "s") {
3909
+ await listMigrationsStatuses(toArray(options), config, args.slice(1));
3818
3910
  } else if (config.commands[arg]) {
3819
3911
  await config.commands[arg](toArray(options), config, args.slice(1));
3820
3912
  } else if (arg !== "rec" && arg !== "recurrent") {
@@ -3839,6 +3931,8 @@ Commands:
3839
3931
  up migrate pending migrations, don't run recurrent
3840
3932
  rollback or down rollback the last migrated
3841
3933
  redo rollback and migrate, run recurrent
3934
+ status or s list migrations statuses
3935
+ status path or s p list migrations statuses and paths to files
3842
3936
  rec or recurrent run recurrent migrations
3843
3937
  no or unknown command prints this message
3844
3938