Skip to content

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

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

import { TableName } from "../schemas";

export async function up(knex: Knex): Promise<void> {
  const hasTable = await knex.schema.hasTable(TableName.ResourceMetadata);

  if (hasTable) {
    const hasEncryptedValueColumn = await knex.schema.hasColumn(TableName.ResourceMetadata, "encryptedValue");
    const hasValueColumn = await knex.schema.hasColumn(TableName.ResourceMetadata, "value");

    await knex.schema.alterTable(TableName.ResourceMetadata, (t) => {
      if (!hasEncryptedValueColumn) t.binary("encryptedValue").nullable();
      if (hasValueColumn) t.string("value", 1020).nullable().alter();
      // if no encryptedValue column, means the column is created now, so we need to add the check constraint
      if (!hasEncryptedValueColumn) {
        t.check(
          `(value IS NOT NULL AND "encryptedValue" IS NULL) OR (value IS NULL AND "encryptedValue" IS NOT NULL)`,
          [],
          "chk_value_or_encrypted_value"
        );
      }
    });
  }

  const hasProjectTable = await knex.schema.hasTable(TableName.Project);
  if (hasProjectTable) {
    const hasEnforceEncryptedSecretManagerSecretMetadata = await knex.schema.hasColumn(
      TableName.Project,
      "enforceEncryptedSecretManagerSecretMetadata"
    );
    if (!hasEnforceEncryptedSecretManagerSecretMetadata) {
      await knex.schema.alterTable(TableName.Project, (t) => {
        t.boolean("enforceEncryptedSecretManagerSecretMetadata").nullable();
      });
    }
  }
}

export async function down(knex: Knex): Promise<void> {
  const hasTable = await knex.schema.hasTable(TableName.ResourceMetadata);

  if (hasTable) {
    const hasEncryptedValueColumn = await knex.schema.hasColumn(TableName.ResourceMetadata, "encryptedValue");
    await knex.schema.alterTable(TableName.ResourceMetadata, (t) => {
      if (hasEncryptedValueColumn) {
        t.dropChecks(["chk_value_or_encrypted_value"]);
        t.dropColumn("encryptedValue");
      }
    });
  }

  const hasProjectTable = await knex.schema.hasTable(TableName.Project);
  if (hasProjectTable) {
    const hasEnforceEncryptedSecretManagerSecretMetadata = await knex.schema.hasColumn(
      TableName.Project,
      "enforceEncryptedSecretManagerSecretMetadata"
    );
    if (hasEnforceEncryptedSecretManagerSecretMetadata) {
      await knex.schema.alterTable(TableName.Project, (t) => {
        t.dropColumn("enforceEncryptedSecretManagerSecretMetadata");
      });
    }
  }
}