"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var chunk_K2NG52LU_exports = {};
__export(chunk_K2NG52LU_exports, {
  MigrateDiff: () => MigrateDiff,
  init_MigrateDiff: () => init_MigrateDiff
});
module.exports = __toCommonJS(chunk_K2NG52LU_exports);
var import_chunk_BOP7PHOB = require("./chunk-BOP7PHOB.js");
var import_chunk_F7KSWWYN = require("./chunk-F7KSWWYN.js");
var import_chunk_FVJ3R4NJ = require("./chunk-FVJ3R4NJ.js");
var import_debug = __toESM(require("@prisma/debug"));
var import_internals = require("@prisma/internals");
var import_path = __toESM(require("path"));
var debug, helpOptions, _MigrateDiff, MigrateDiff;
var init_MigrateDiff = (0, import_chunk_FVJ3R4NJ.__esm)({
  "src/commands/MigrateDiff.ts"() {
    (0, import_chunk_F7KSWWYN.init_colors)();
    (0, import_chunk_BOP7PHOB.init_Migrate)();
    debug = (0, import_debug.default)("prisma:migrate:diff");
    helpOptions = (0, import_internals.format)(
      `${(0, import_chunk_F7KSWWYN.bold)("Usage")}

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

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

  -h, --help               Display this help message

${(0, import_chunk_F7KSWWYN.italic)("From and To inputs (1 `--from-...` and 1 `--to-...` must be provided):")}
  --from-url               A datasource URL
  --to-url

  --from-empty             Flag to assume from or to is an empty datamodel
  --to-empty

  --from-schema-datamodel  Path to a Prisma schema file, uses the ${(0, import_chunk_F7KSWWYN.italic)("datamodel")} for the diff
  --to-schema-datamodel

  --from-schema-datasource Path to a Prisma schema file, uses the ${(0, import_chunk_F7KSWWYN.italic)("datasource url")} for the diff
  --to-schema-datasource

  --from-migrations        Path to the Prisma Migrate migrations directory
  --to-migrations

${(0, import_chunk_F7KSWWYN.italic)("Shadow database (only required if using --from-migrations or --to-migrations):")}
  --shadow-database-url    URL for the shadow database

${(0, import_chunk_F7KSWWYN.bold)("Flags")}

  --script                 Render a SQL script to stdout instead of the default human readable summary (not supported on MongoDB)
  --exit-code              Change the exit code behavior to signal if the diff is empty or not (Empty: 0, Error: 1, Not empty: 2). Default behavior is Success: 0, Error: 1.`
    );
    _MigrateDiff = class _MigrateDiff2 {
      static new() {
        return new _MigrateDiff2();
      }
      async parse(argv) {
        const args = (0, import_internals.arg)(
          argv,
          {
            "--help": Boolean,
            "-h": "--help",
            // From
            "--from-empty": Boolean,
            "--from-schema-datasource": String,
            "--from-schema-datamodel": String,
            "--from-url": String,
            "--from-migrations": String,
            // To
            "--to-empty": Boolean,
            "--to-schema-datasource": String,
            "--to-schema-datamodel": String,
            "--to-url": String,
            "--to-migrations": String,
            // Others
            "--shadow-database-url": String,
            "--script": Boolean,
            "--exit-code": Boolean,
            "--telemetry-information": String
          },
          false
        );
        if ((0, import_internals.isError)(args)) {
          return this.help(args.message);
        }
        await (0, import_internals.checkUnsupportedDataProxy)("migrate diff", args, false);
        if (args["--help"]) {
          return this.help();
        }
        const numberOfFromParameterProvided = Number(Boolean(args["--from-empty"])) + Number(Boolean(args["--from-schema-datasource"])) + Number(Boolean(args["--from-schema-datamodel"])) + Number(Boolean(args["--from-url"])) + Number(Boolean(args["--from-migrations"]));
        const numberOfToParameterProvided = Number(Boolean(args["--to-empty"])) + Number(Boolean(args["--to-schema-datasource"])) + Number(Boolean(args["--to-schema-datamodel"])) + Number(Boolean(args["--to-url"])) + Number(Boolean(args["--to-migrations"]));
        if (numberOfFromParameterProvided !== 1 || numberOfToParameterProvided !== 1) {
          const errorMessages = [];
          if (numberOfFromParameterProvided !== 1) {
            errorMessages.push(`${numberOfFromParameterProvided} \`--from-...\` parameter(s) provided. 1 must be provided.`);
          }
          if (numberOfToParameterProvided !== 1) {
            errorMessages.push(`${numberOfToParameterProvided} \`--to-...\` parameter(s) provided. 1 must be provided.`);
          }
          return this.help(`${errorMessages.join("\n")}`);
        }
        let from;
        if (args["--from-empty"]) {
          from = {
            tag: "empty"
          };
        } else if (args["--from-schema-datasource"]) {
          (0, import_internals.loadEnvFile)({ schemaPath: args["--from-schema-datasource"], printMessage: false });
          from = {
            tag: "schemaDatasource",
            schema: import_path.default.resolve(args["--from-schema-datasource"])
          };
        } else if (args["--from-schema-datamodel"]) {
          from = {
            tag: "schemaDatamodel",
            schema: import_path.default.resolve(args["--from-schema-datamodel"])
          };
        } else if (args["--from-url"]) {
          from = {
            tag: "url",
            url: args["--from-url"]
          };
        } else if (args["--from-migrations"]) {
          from = {
            tag: "migrations",
            path: import_path.default.resolve(args["--from-migrations"])
          };
        }
        let to;
        if (args["--to-empty"]) {
          to = {
            tag: "empty"
          };
        } else if (args["--to-schema-datasource"]) {
          (0, import_internals.loadEnvFile)({ schemaPath: args["--to-schema-datasource"], printMessage: false });
          to = {
            tag: "schemaDatasource",
            schema: import_path.default.resolve(args["--to-schema-datasource"])
          };
        } else if (args["--to-schema-datamodel"]) {
          to = {
            tag: "schemaDatamodel",
            schema: import_path.default.resolve(args["--to-schema-datamodel"])
          };
        } else if (args["--to-url"]) {
          to = {
            tag: "url",
            url: args["--to-url"]
          };
        } else if (args["--to-migrations"]) {
          to = {
            tag: "migrations",
            path: import_path.default.resolve(args["--to-migrations"])
          };
        }
        const migrate = new import_chunk_BOP7PHOB.Migrate();
        let result;
        try {
          result = await migrate.engine.migrateDiff({
            from,
            to,
            script: args["--script"] || false,
            // default is false
            shadowDatabaseUrl: args["--shadow-database-url"],
            exitCode: args["--exit-code"]
          });
        } finally {
          migrate.stop();
        }
        debug({ migrateDiffOutput: result });
        if (args["--exit-code"] && result.exitCode) {
          process.exit(result.exitCode);
        }
        return ``;
      }
      help(error) {
        if (error) {
          throw new import_internals.HelpError(`
${error}

${helpOptions}`);
        }
        return _MigrateDiff2.help;
      }
    };
    _MigrateDiff.help = (0, import_internals.format)(`
${process.platform === "win32" ? "" : "\u{1F50D} "}Compares the database schema from two arbitrary sources, and outputs the differences either as a human-readable summary (by default) or an executable script.

${(0, import_chunk_F7KSWWYN.green)(`prisma migrate diff`)} is a read-only command that does not write to your datasource(s).
${(0, import_chunk_F7KSWWYN.green)(`prisma db execute`)} can be used to execute its ${(0, import_chunk_F7KSWWYN.green)(`--script`)} output.

The command takes a source ${(0, import_chunk_F7KSWWYN.green)(`--from-...`)} and a destination ${(0, import_chunk_F7KSWWYN.green)(`--to-...`)}.
The source and destination must use the same provider,
e.g. a diff using 2 different providers like PostgreSQL and SQLite is not supported.

It compares the source with the destination to generate a diff. 
The diff can be interpreted as generating a migration that brings the source schema (from) to the shape of the destination schema (to).
The default output is a human readable diff, it can be rendered as SQL using \`--script\` on SQL databases.

See the documentation for more information ${(0, import_internals.link)("https://pris.ly/d/migrate-diff")}

${helpOptions}
${(0, import_chunk_F7KSWWYN.bold)("Examples")}
 
  From database to database as summary
    e.g. compare two live databases
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate diff \\
    --from-url "$DATABASE_URL" \\
    --to-url "postgresql://login:password@localhost:5432/db2"
  
  From a live database to a Prisma datamodel
    e.g. roll forward after a migration failed in the middle
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate diff \\
    --shadow-database-url "$SHADOW_DB" \\
    --from-url "$PROD_DB" \\
    --to-schema-datamodel=next_datamodel.prisma \\
    --script
  
  From a live database to a datamodel 
    e.g. roll backward after a migration failed in the middle
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate diff \\
    --shadow-database-url "$SHADOW_DB" \\
    --from-url "$PROD_DB" \\
    --to-schema-datamodel=previous_datamodel.prisma \\
    --script
  
  From a Prisma Migrate \`migrations\` directory to another database
    e.g. generate a migration for a hotfix already applied on production
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate diff \\
    --shadow-database-url "$SHADOW_DB" \\
    --from-migrations ./migrations \\
    --to-url "$PROD_DB" \\
    --script

  Execute the --script output with \`prisma db execute\` using bash pipe \`|\`
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate diff \\
    --from-[...] \\
    --to-[...] \\
    --script | prisma db execute --stdin --url="$DATABASE_URL"

  Detect if both sources are in sync, it will exit with exit code 2 if changes are detected
  ${(0, import_chunk_F7KSWWYN.dim)("$")} prisma migrate diff \\
    --exit-code \\
    --from-[...] \\
    --to-[...]
`);
    MigrateDiff = _MigrateDiff;
  }
});
