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