"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var chunk_AL3XYISJ_exports = {};
__export(chunk_AL3XYISJ_exports, {
  MigrateResolve: () => MigrateResolve
});
module.exports = __toCommonJS(chunk_AL3XYISJ_exports);
var import_chunk_QOPTDDIH = require("./chunk-QOPTDDIH.js");
var import_chunk_BRPS327A = require("./chunk-BRPS327A.js");
var import_chunk_RGXJYLMD = require("./chunk-RGXJYLMD.js");
var import_chunk_SKRR5WT4 = require("./chunk-SKRR5WT4.js");
var import_internals = require("@prisma/internals");
var MigrateResolve = class _MigrateResolve {
  static new() {
    return new _MigrateResolve();
  }
  static help = (0, import_internals.format)(`
Resolve issues with database migrations in deployment databases:
- recover from failed migrations
- baseline databases when starting to use Prisma Migrate on existing databases
- reconcile hotfixes done manually on databases with your migration history

Run "prisma migrate status" to identify if you need to use resolve.

Read more about resolving migration history issues: ${(0, import_internals.link)("https://pris.ly/d/migrate-resolve")}

${(0, import_chunk_SKRR5WT4.bold)("Usage")}

  ${(0, import_chunk_SKRR5WT4.dim)("$")} prisma migrate resolve [options]

  The datasource URL configuration is read from the Prisma config file (e.g., ${(0, import_chunk_SKRR5WT4.italic)("prisma.config.ts")}).

${(0, import_chunk_SKRR5WT4.bold)("Options")}

    -h, --help   Display this help message
      --config   Custom path to your Prisma config file
      --schema   Custom path to your Prisma schema
     --applied   Record a specific migration as applied
 --rolled-back   Record a specific migration as rolled back

${(0, import_chunk_SKRR5WT4.bold)("Examples")}

  Update migrations table, recording a specific migration as applied
  ${(0, import_chunk_SKRR5WT4.dim)("$")} prisma migrate resolve --applied 20201231000000_add_users_table

  Update migrations table, recording a specific migration as rolled back
  ${(0, import_chunk_SKRR5WT4.dim)("$")} prisma migrate resolve --rolled-back 20201231000000_add_users_table

  Specify a schema
  ${(0, import_chunk_SKRR5WT4.dim)("$")} prisma migrate resolve --rolled-back 20201231000000_add_users_table --schema=./schema.prisma
`);
  async parse(argv, config, baseDir) {
    const args = (0, import_internals.arg)(
      argv,
      {
        "--help": Boolean,
        "-h": "--help",
        "--applied": String,
        "--rolled-back": String,
        "--schema": String,
        "--config": String,
        "--telemetry-information": String
      },
      false
    );
    if ((0, import_internals.isError)(args)) {
      return this.help(args.message);
    }
    if (args["--help"]) {
      return this.help();
    }
    const schemaContext = await (0, import_internals.loadSchemaContext)({
      schemaPath: (0, import_internals.createSchemaPathInput)({
        schemaPathFromArgs: args["--schema"],
        schemaPathFromConfig: config.schema,
        baseDir
      })
    });
    const { migrationsDirPath } = (0, import_internals.inferDirectoryConfig)(schemaContext, config);
    const cmd = "migrate resolve";
    const validatedConfig = (0, import_internals.validatePrismaConfigWithDatasource)({ config, cmd });
    (0, import_internals.checkUnsupportedDataProxy)({ cmd, validatedConfig });
    (0, import_chunk_QOPTDDIH.printDatasource)({ datasourceInfo: (0, import_chunk_BRPS327A.parseDatasourceInfo)(schemaContext.primaryDatasource, validatedConfig) });
    if (!args["--applied"] && !args["--rolled-back"]) {
      throw new Error(
        `--applied or --rolled-back must be part of the command like:
${(0, import_chunk_SKRR5WT4.bold)((0, import_chunk_SKRR5WT4.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --applied 20201231000000_example")))}
${(0, import_chunk_SKRR5WT4.bold)((0, import_chunk_SKRR5WT4.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --rolled-back 20201231000000_example")))}`
      );
    } else if (args["--applied"] && args["--rolled-back"]) {
      throw new Error("Pass either --applied or --rolled-back, not both.");
    }
    if (args["--applied"]) {
      if (typeof args["--applied"] !== "string" || args["--applied"].length === 0) {
        throw new Error(
          `--applied value must be a string like ${(0, import_chunk_SKRR5WT4.bold)(
            (0, import_chunk_SKRR5WT4.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --applied 20201231000000_example"))
          )}`
        );
      }
      await (0, import_chunk_BRPS327A.ensureCanConnectToDatabase)(baseDir, validatedConfig);
      const migrate = await import_chunk_RGXJYLMD.Migrate.setup({
        schemaEngineConfig: config,
        baseDir,
        migrationsDirPath,
        schemaContext,
        extensions: config["extensions"]
      });
      try {
        await migrate.markMigrationApplied({
          migrationId: args["--applied"]
        });
      } finally {
        await migrate.stop();
      }
      process.stdout.write(`
Migration ${args["--applied"]} marked as applied.
`);
      return ``;
    } else {
      if (typeof args["--rolled-back"] !== "string" || args["--rolled-back"].length === 0) {
        throw new Error(
          `--rolled-back value must be a string like ${(0, import_chunk_SKRR5WT4.bold)(
            (0, import_chunk_SKRR5WT4.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --rolled-back 20201231000000_example"))
          )}`
        );
      }
      await (0, import_chunk_BRPS327A.ensureCanConnectToDatabase)(baseDir, validatedConfig);
      const migrate = await import_chunk_RGXJYLMD.Migrate.setup({
        schemaEngineConfig: config,
        baseDir,
        migrationsDirPath,
        schemaContext,
        extensions: config["extensions"]
      });
      try {
        await migrate.markMigrationRolledBack({
          migrationId: args["--rolled-back"]
        });
      } finally {
        await migrate.stop();
      }
      process.stdout.write(`
Migration ${args["--rolled-back"]} marked as rolled back.
`);
      return ``;
    }
  }
  help(error) {
    if (error) {
      return new import_internals.HelpError(`
${(0, import_chunk_SKRR5WT4.bold)((0, import_chunk_SKRR5WT4.red)(`!`))} ${error}
${_MigrateResolve.help}`);
    }
    return _MigrateResolve.help;
  }
};
