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_initAt_exports = {};
__export(add_initAt_exports, {
  default: () => add_initAt_default
});
module.exports = __toCommonJS(add_initAt_exports);
var import_server = require("@tachybase/server");
var import_sequelize = require("sequelize");
var import_constants = require("../constants");
class add_initAt_default extends import_server.Migration {
  constructor() {
    super(...arguments);
    this.on = "afterLoad";
    // 'beforeLoad' or 'afterLoad'
    this.appVersion = "<0.22.38";
  }
  async up() {
    const result = await this.app.db.sequelize.query(
      `
      WITH min_created_at AS (
        SELECT key, MIN("createdAt") AS min_created_at
        FROM ${import_constants.COLLECTION_WORKFLOWS_NAME}
        GROUP BY key
      )
      UPDATE ${import_constants.COLLECTION_WORKFLOWS_NAME} AS wf
      SET "initAt" = min_created_at.min_created_at
      FROM min_created_at
      WHERE wf.key = min_created_at.key
    `,
      { type: import_sequelize.QueryTypes.UPDATE }
    );
    this.app.logger.info(`collection [${import_constants.COLLECTION_WORKFLOWS_NAME}] update ${result[1]} rows`);
  }
}
