"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 setupCockroach_exports = {};
__export(setupCockroach_exports, {
  setupCockroach: () => setupCockroach,
  tearDownCockroach: () => tearDownCockroach
});
module.exports = __toCommonJS(setupCockroach_exports);
var import_chunk_ZFYCWOZ3 = require("../chunk-ZFYCWOZ3.js");
var import_chunk_FVJ3R4NJ = require("../chunk-FVJ3R4NJ.js");
var import_internals = require("@prisma/internals");
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_pg = (0, import_chunk_FVJ3R4NJ.__toESM)((0, import_chunk_ZFYCWOZ3.require_lib)());
async function setupCockroach(options) {
  const { connectionString } = options;
  const { dirname } = options;
  const credentials = (0, import_internals.uriToCredentials)(connectionString);
  const dbDefault = new import_pg.Client({
    connectionString: connectionString.replace(credentials.database, "cockroachdb")
  });
  await dbDefault.connect();
  await dbDefault.query(`DROP DATABASE IF EXISTS "${credentials.database}-shadowdb";`);
  await dbDefault.query(`CREATE DATABASE "${credentials.database}-shadowdb";`);
  await dbDefault.query(`DROP DATABASE IF EXISTS "${credentials.database}";`);
  await dbDefault.query(`CREATE DATABASE "${credentials.database}";`);
  await dbDefault.end();
  if (dirname !== "") {
    const db = new import_pg.Client({
      connectionString
    });
    await db.connect();
    await db.query(import_fs.default.readFileSync(import_path.default.join(dirname, "setup.sql"), "utf-8"));
    await db.end();
  }
}
async function tearDownCockroach(options) {
  const { connectionString } = options;
  const credentials = (0, import_internals.uriToCredentials)(connectionString);
  const credentialsClone = { ...credentials };
  credentialsClone.database = "cockroachdb";
  credentialsClone.schema = "";
  const connectionStringCopy = (0, import_internals.credentialsToUri)(credentialsClone);
  const db = new import_pg.Client({
    connectionString: connectionStringCopy
  });
  await db.connect();
  await db.query(`
    DROP DATABASE IF EXISTS "${credentials.database}";
  `);
  await db.end();
}
