"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_I2FA66RM_exports = {};
__export(chunk_I2FA66RM_exports, {
  MigrateResolve: () => MigrateResolve,
  init_MigrateResolve: () => init_MigrateResolve
});
module.exports = __toCommonJS(chunk_I2FA66RM_exports);
var import_chunk_BOP7PHOB = require("./chunk-BOP7PHOB.js");
var import_chunk_KCMQOXUX = require("./chunk-KCMQOXUX.js");
var import_chunk_FFLD75KX = require("./chunk-FFLD75KX.js");
var import_chunk_PZ4BRULM = require("./chunk-PZ4BRULM.js");
var import_chunk_F7KSWWYN = require("./chunk-F7KSWWYN.js");
var import_chunk_FVJ3R4NJ = require("./chunk-FVJ3R4NJ.js");
var import_internals = require("@prisma/internals");
var _MigrateResolve, MigrateResolve;
var init_MigrateResolve = (0, import_chunk_FVJ3R4NJ.__esm)({
  "src/commands/MigrateResolve.ts"() {
    (0, import_chunk_F7KSWWYN.init_colors)();
    (0, import_chunk_BOP7PHOB.init_Migrate)();
    (0, import_chunk_PZ4BRULM.init_ensureDatabaseExists)();
    (0, import_chunk_KCMQOXUX.init_getSchemaPathAndPrint)();
    (0, import_chunk_FFLD75KX.init_printDatasource)();
    _MigrateResolve = class _MigrateResolve2 {
      static new() {
        return new _MigrateResolve2();
      }
      async parse(argv) {
        const args = (0, import_internals.arg)(
          argv,
          {
            "--help": Boolean,
            "-h": "--help",
            "--applied": String,
            "--rolled-back": String,
            "--schema": String,
            "--telemetry-information": String
          },
          false
        );
        if ((0, import_internals.isError)(args)) {
          return this.help(args.message);
        }
        await (0, import_internals.checkUnsupportedDataProxy)("migrate resolve", args, true);
        if (args["--help"]) {
          return this.help();
        }
        (0, import_internals.loadEnvFile)({ schemaPath: args["--schema"], printMessage: true });
        const schemaPath = await (0, import_chunk_KCMQOXUX.getSchemaPathAndPrint)(args["--schema"]);
        (0, import_chunk_FFLD75KX.printDatasource)({ datasourceInfo: await (0, import_chunk_PZ4BRULM.getDatasourceInfo)({ schemaPath }) });
        if (!args["--applied"] && !args["--rolled-back"]) {
          throw new Error(
            `--applied or --rolled-back must be part of the command like:
${(0, import_chunk_F7KSWWYN.bold)((0, import_chunk_F7KSWWYN.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --applied 20201231000000_example")))}
${(0, import_chunk_F7KSWWYN.bold)((0, import_chunk_F7KSWWYN.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_F7KSWWYN.bold)(
                (0, import_chunk_F7KSWWYN.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --applied 20201231000000_example"))
              )}`
            );
          }
          await (0, import_chunk_PZ4BRULM.ensureCanConnectToDatabase)(schemaPath);
          const migrate = new import_chunk_BOP7PHOB.Migrate(schemaPath);
          try {
            await migrate.markMigrationApplied({
              migrationId: args["--applied"]
            });
          } finally {
            migrate.stop();
          }
          return `Migration ${args["--applied"]} marked as applied.`;
        } 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_F7KSWWYN.bold)(
                (0, import_chunk_F7KSWWYN.green)((0, import_internals.getCommandWithExecutor)("prisma migrate resolve --rolled-back 20201231000000_example"))
              )}`
            );
          }
          await (0, import_chunk_PZ4BRULM.ensureCanConnectToDatabase)(schemaPath);
          const migrate = new import_chunk_BOP7PHOB.Migrate(schemaPath);
          try {
            await migrate.markMigrationRolledBack({
              migrationId: args["--rolled-back"]
            });
          } finally {
            migrate.stop();
          }
          return `Migration ${args["--rolled-back"]} marked as rolled back.`;
        }
      }
      help(error) {
        if (error) {
          return new import_internals.HelpError(`
${(0, import_chunk_F7KSWWYN.bold)((0, import_chunk_F7KSWWYN.red)(`!`))} ${error}
${_MigrateResolve2.help}`);
        }
        return _MigrateResolve2.help;
      }
    };
    _MigrateResolve.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_F7KSWWYN.bold)("Usage")}

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

    -h, --help   Display this help message
      --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_F7KSWWYN.bold)("Examples")}

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

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

  Specify a schema
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate resolve --rolled-back 20201231000000_add_users_table --schema=./schema.prisma
`);
    MigrateResolve = _MigrateResolve;
  }
});
