diff --git a/backend/src/db/migrations/20240925100349_managed-secret-sharing.ts b/backend/src/db/migrations/20240925100349_managed-secret-sharing.ts index 56784d314..f64d7f858 100644 --- a/backend/src/db/migrations/20240925100349_managed-secret-sharing.ts +++ b/backend/src/db/migrations/20240925100349_managed-secret-sharing.ts @@ -4,27 +4,40 @@ import { TableName } from "../schemas"; export async function up(knex: Knex): Promise { if (await knex.schema.hasTable(TableName.SecretSharing)) { + const hasEncryptedSecret = await knex.schema.hasColumn(TableName.SecretSharing, "encryptedSecret"); + const hasIdentifier = await knex.schema.hasColumn(TableName.SecretSharing, "identifier"); + await knex.schema.alterTable(TableName.SecretSharing, (t) => { t.string("iv").nullable().alter(); t.string("tag").nullable().alter(); t.string("encryptedValue").nullable().alter(); - t.binary("encryptedSecret").nullable(); + if (!hasEncryptedSecret) { + t.binary("encryptedSecret").nullable(); + } t.string("hashedHex").nullable().alter(); - t.string("identifier", 64).nullable(); - t.unique("identifier"); - t.index("identifier"); + if (!hasIdentifier) { + t.string("identifier", 64).nullable(); + t.unique("identifier"); + t.index("identifier"); + } }); } } export async function down(knex: Knex): Promise { + const hasEncryptedSecret = await knex.schema.hasColumn(TableName.SecretSharing, "encryptedSecret"); + const hasIdentifier = await knex.schema.hasColumn(TableName.SecretSharing, "identifier"); if (await knex.schema.hasTable(TableName.SecretSharing)) { await knex.schema.alterTable(TableName.SecretSharing, (t) => { - t.dropColumn("encryptedSecret"); + if (hasEncryptedSecret) { + t.dropColumn("encryptedSecret"); + } - t.dropColumn("identifier"); + if (hasIdentifier) { + t.dropColumn("identifier"); + } }); } } diff --git a/backend/src/db/migrations/20241003220151_kms-key-cmek-alterations.ts b/backend/src/db/migrations/20241003220151_kms-key-cmek-alterations.ts index 6b701eea4..bdad443c9 100644 --- a/backend/src/db/migrations/20241003220151_kms-key-cmek-alterations.ts +++ b/backend/src/db/migrations/20241003220151_kms-key-cmek-alterations.ts @@ -7,15 +7,18 @@ export async function up(knex: Knex): Promise { if (await knex.schema.hasTable(TableName.KmsKey)) { const hasOrgId = await knex.schema.hasColumn(TableName.KmsKey, "orgId"); const hasSlug = await knex.schema.hasColumn(TableName.KmsKey, "slug"); + const hasProjectId = await knex.schema.hasColumn(TableName.KmsKey, "projectId"); // drop constraint if exists (won't exist if rolled back, see below) await dropConstraintIfExists(TableName.KmsKey, "kms_keys_orgid_slug_unique", knex); // projectId for CMEK functionality await knex.schema.alterTable(TableName.KmsKey, (table) => { - table.string("projectId").nullable().references("id").inTable(TableName.Project).onDelete("CASCADE"); + if (!hasProjectId) { + table.string("projectId").nullable().references("id").inTable(TableName.Project).onDelete("CASCADE"); + } - if (hasOrgId) { + if (hasOrgId && hasSlug) { table.unique(["orgId", "projectId", "slug"]); } @@ -30,6 +33,7 @@ export async function down(knex: Knex): Promise { if (await knex.schema.hasTable(TableName.KmsKey)) { const hasOrgId = await knex.schema.hasColumn(TableName.KmsKey, "orgId"); const hasName = await knex.schema.hasColumn(TableName.KmsKey, "name"); + const hasProjectId = await knex.schema.hasColumn(TableName.KmsKey, "projectId"); // remove projectId for CMEK functionality await knex.schema.alterTable(TableName.KmsKey, (table) => { @@ -40,7 +44,9 @@ export async function down(knex: Knex): Promise { if (hasOrgId) { table.dropUnique(["orgId", "projectId", "slug"]); } - table.dropColumn("projectId"); + if (hasProjectId) { + table.dropColumn("projectId"); + } }); } }