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 add_sort_to_workflows_exports = {};
__export(add_sort_to_workflows_exports, {
  default: () => AddSortToWorkflowsMigration
});
module.exports = __toCommonJS(add_sort_to_workflows_exports);
var import_server = require("@tego/server");
class AddSortToWorkflowsMigration extends import_server.Migration {
  constructor() {
    super(...arguments);
    this.on = "afterLoad";
    this.appVersion = "<1.4.0";
  }
  async up() {
    var _a, _b, _c, _d;
    const queryInterface = this.db.sequelize.getQueryInterface();
    const tableName = "workflows";
    const tables = await queryInterface.showAllTables();
    if (!tables.includes(tableName)) {
      this.app.logger.info(`[migration skipped] table ${tableName} does not exist`);
      return;
    }
    const tableDescription = await queryInterface.describeTable(tableName);
    if (tableDescription.sort) {
      this.app.logger.info(`[migration skipped] column sort already exists in ${tableName}`);
      return;
    }
    try {
      await queryInterface.addColumn(tableName, "sort", {
        type: import_server.DataTypes.BIGINT,
        allowNull: true,
        defaultValue: 0
      });
      await this.db.sequelize.query(`UPDATE ${this.db.utils.quoteTable(tableName)} SET sort = 0 WHERE sort IS NULL`);
      this.app.logger.info(`[workflows] added sort column successfully`);
      const Field = this.context.db.getRepository("fields");
      const sortFieldExists = await Field.count({
        filter: {
          name: "sort",
          collectionName: "workflows"
        }
      });
      if (!sortFieldExists) {
        await Field.create({
          values: {
            name: "sort",
            collectionName: "workflows",
            type: "bigInt",
            interface: "sort",
            uiSchema: {
              type: "number",
              title: '{{t("Sort")}}',
              "x-component": "InputNumber",
              "x-component-props": {
                stringMode: true,
                step: "1"
              },
              "x-validator": "integer"
            }
          }
        });
        this.app.logger.info(`[workflows] added sort field record to fields table`);
      }
    } catch (error) {
      if (((_a = error.message) == null ? void 0 : _a.includes("duplicate column name")) || ((_b = error.message) == null ? void 0 : _b.includes("already exists")) || ((_c = error.message) == null ? void 0 : _c.includes("column")) && ((_d = error.message) == null ? void 0 : _d.includes("exists"))) {
        this.app.logger.info(`[migration skipped] sort column may already exist: ${error.message}`);
        return;
      }
      throw error;
    }
  }
}
