feat: rebased and added back missing idempotence in some migration files

This commit is contained in:
=
2024-10-18 00:33:35 +05:30
parent c60d957269
commit aeaabe2c27
2 changed files with 28 additions and 9 deletions
@@ -4,27 +4,40 @@ import { TableName } from "../schemas";
export async function up(knex: Knex): Promise<void> { export async function up(knex: Knex): Promise<void> {
if (await knex.schema.hasTable(TableName.SecretSharing)) { 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) => { await knex.schema.alterTable(TableName.SecretSharing, (t) => {
t.string("iv").nullable().alter(); t.string("iv").nullable().alter();
t.string("tag").nullable().alter(); t.string("tag").nullable().alter();
t.string("encryptedValue").nullable().alter(); t.string("encryptedValue").nullable().alter();
if (!hasEncryptedSecret) {
t.binary("encryptedSecret").nullable(); t.binary("encryptedSecret").nullable();
}
t.string("hashedHex").nullable().alter(); t.string("hashedHex").nullable().alter();
if (!hasIdentifier) {
t.string("identifier", 64).nullable(); t.string("identifier", 64).nullable();
t.unique("identifier"); t.unique("identifier");
t.index("identifier"); t.index("identifier");
}
}); });
} }
} }
export async function down(knex: Knex): Promise<void> { export async function down(knex: Knex): Promise<void> {
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)) { if (await knex.schema.hasTable(TableName.SecretSharing)) {
await knex.schema.alterTable(TableName.SecretSharing, (t) => { await knex.schema.alterTable(TableName.SecretSharing, (t) => {
if (hasEncryptedSecret) {
t.dropColumn("encryptedSecret"); t.dropColumn("encryptedSecret");
}
if (hasIdentifier) {
t.dropColumn("identifier"); t.dropColumn("identifier");
}
}); });
} }
} }
@@ -7,15 +7,18 @@ export async function up(knex: Knex): Promise<void> {
if (await knex.schema.hasTable(TableName.KmsKey)) { if (await knex.schema.hasTable(TableName.KmsKey)) {
const hasOrgId = await knex.schema.hasColumn(TableName.KmsKey, "orgId"); const hasOrgId = await knex.schema.hasColumn(TableName.KmsKey, "orgId");
const hasSlug = await knex.schema.hasColumn(TableName.KmsKey, "slug"); 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) // drop constraint if exists (won't exist if rolled back, see below)
await dropConstraintIfExists(TableName.KmsKey, "kms_keys_orgid_slug_unique", knex); await dropConstraintIfExists(TableName.KmsKey, "kms_keys_orgid_slug_unique", knex);
// projectId for CMEK functionality // projectId for CMEK functionality
await knex.schema.alterTable(TableName.KmsKey, (table) => { await knex.schema.alterTable(TableName.KmsKey, (table) => {
if (!hasProjectId) {
table.string("projectId").nullable().references("id").inTable(TableName.Project).onDelete("CASCADE"); table.string("projectId").nullable().references("id").inTable(TableName.Project).onDelete("CASCADE");
}
if (hasOrgId) { if (hasOrgId && hasSlug) {
table.unique(["orgId", "projectId", "slug"]); table.unique(["orgId", "projectId", "slug"]);
} }
@@ -30,6 +33,7 @@ export async function down(knex: Knex): Promise<void> {
if (await knex.schema.hasTable(TableName.KmsKey)) { if (await knex.schema.hasTable(TableName.KmsKey)) {
const hasOrgId = await knex.schema.hasColumn(TableName.KmsKey, "orgId"); const hasOrgId = await knex.schema.hasColumn(TableName.KmsKey, "orgId");
const hasName = await knex.schema.hasColumn(TableName.KmsKey, "name"); const hasName = await knex.schema.hasColumn(TableName.KmsKey, "name");
const hasProjectId = await knex.schema.hasColumn(TableName.KmsKey, "projectId");
// remove projectId for CMEK functionality // remove projectId for CMEK functionality
await knex.schema.alterTable(TableName.KmsKey, (table) => { await knex.schema.alterTable(TableName.KmsKey, (table) => {
@@ -40,7 +44,9 @@ export async function down(knex: Knex): Promise<void> {
if (hasOrgId) { if (hasOrgId) {
table.dropUnique(["orgId", "projectId", "slug"]); table.dropUnique(["orgId", "projectId", "slug"]);
} }
if (hasProjectId) {
table.dropColumn("projectId"); table.dropColumn("projectId");
}
}); });
} }
} }