hunk.code_window:             if (queryRunner) await (queryRunner as QueryRunner).release()
               if (dataSource) await dataSource.destroy()
               PlatformTools.logCmdErr("Error during query execution:", err)
               process.exit(1)
           }
       }
  
hunk.labels: keep keep keep replace keep keep keep
hunk.file_path: src/commands/QueryCommand.ts
hunk.type: replace
file: import { DataSource } from "../data-source/DataSource"
  import * as yargs from "yargs"
  import { PlatformTools } from "../platform/PlatformTools"
  import path from "path"
  import process from "process"
  import { CommandUtils } from "./CommandUtils"
  
  /**
   * Reverts last migration command.
   */
  export class MigrationRevertCommand implements yargs.CommandModule {
      command = "migration:revert"
      describe = "Reverts last executed migration."
  
      builder(args: yargs.Argv) {
          return args
              .option("dataSource", {
                  alias: "d",
                  describe:
                      "Path to the file where your DataSource instance is defined.",
                  demandOption: true,
              })
              .option("transaction", {
                  alias: "t",
                  default: "default",
                  describe:
                      "Indicates if transaction should be used or not for migration revert. Enabled by default.",
              })
      }
  
      async handler(args: yargs.Arguments) {
          let dataSource: DataSource | undefined = undefined
          try {
              dataSource = await CommandUtils.loadDataSource(
                  path.resolve(process.cwd(), args.dataSource as string),
              )
              dataSource.setOptions({
                  subscribers: [],
                  synchronize: false,
                  migrationsRun: false,
                  dropSchema: false,
                  logging: ["query", "error", "schema"],
              })
              await dataSource.initialize()
  
              const options = {
                  transaction:
                      dataSource.options.migrationsTransactionMode ??
                      ("all" as "all" | "none" | "each"),
              }
  
              switch (args.t) {
                  case "all":
                      options.transaction = "all"
                      break
                  case "none":
                  case "false":
                      options.transaction = "none"
                      break
                  case "each":
                      options.transaction = "each"
                      break
                  default:
                  // noop
              }
  
              await dataSource.undoLastMigration(options)
              await dataSource.destroy()
          } catch (err) {
              if (dataSource) await dataSource.destroy()
  
              PlatformTools.logCmdErr("Error during migration revert:", err)
              process.exit(1)
          }
      }
  }
  