Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

2.0 KB
Raw Blame
import { Knex } from "knex";

import { TableName } from "../schemas";
import { createOnUpdateTrigger, dropOnUpdateTrigger } from "../utils";

export async function up(knex: Knex): Promise<void> {
  if (!(await knex.schema.hasTable(TableName.CertificateCleanupConfig))) {
    await knex.schema.createTable(TableName.CertificateCleanupConfig, (tb) => {
      tb.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid());
      tb.string("projectId").notNullable().unique();
      tb.foreign("projectId").references("id").inTable(TableName.Project).onDelete("CASCADE");
      tb.boolean("isEnabled").notNullable().defaultTo(false);
      tb.integer("postExpiryRetentionDays").notNullable().defaultTo(3);
      tb.boolean("skipCertsWithActiveSyncs").notNullable().defaultTo(true);
      tb.string("lastRunStatus");
      tb.datetime("lastRunAt");
      tb.integer("lastRunCertsDeleted").notNullable().defaultTo(0);
      tb.text("lastRunMessage");
      tb.timestamps(true, true, true);
    });

    await createOnUpdateTrigger(knex, TableName.CertificateCleanupConfig);
  }

  const indexExists = await knex.raw(`SELECT 1 FROM pg_indexes WHERE tablename = ? AND indexname = ?`, [
    TableName.Certificate,
    "idx_certificates_not_after"
  ]);
  if (indexExists.rows.length === 0) {
    await knex.schema.alterTable(TableName.Certificate, (tb) => {
      tb.index(["notAfter"], "idx_certificates_not_after");
    });
  }
}

export async function down(knex: Knex): Promise<void> {
  await dropOnUpdateTrigger(knex, TableName.CertificateCleanupConfig);
  await knex.schema.dropTableIfExists(TableName.CertificateCleanupConfig);

  if (await knex.schema.hasTable(TableName.Certificate)) {
    const indexExists = await knex.raw(`SELECT 1 FROM pg_indexes WHERE tablename = ? AND indexname = ?`, [
      TableName.Certificate,
      "idx_certificates_not_after"
    ]);
    if (indexExists.rows.length > 0) {
      await knex.schema.alterTable(TableName.Certificate, (tb) => {
        tb.dropIndex(["notAfter"], "idx_certificates_not_after");
      });
    }
  }
}