-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1687d66
commit 98cca70
Showing
6 changed files
with
78 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
backend/src/db/migrations/20241007052025_make-audit-log-independent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "../schemas"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
if (await knex.schema.hasTable(TableName.AuditLog)) { | ||
const doesProjectIdExist = await knex.schema.hasColumn(TableName.AuditLog, "projectId"); | ||
const doesOrgIdExist = await knex.schema.hasColumn(TableName.AuditLog, "orgId"); | ||
const doesProjectNameExist = await knex.schema.hasColumn(TableName.AuditLog, "projectName"); | ||
|
||
await knex.schema.alterTable(TableName.AuditLog, (t) => { | ||
if (doesOrgIdExist) { | ||
t.dropForeign("orgId"); | ||
} | ||
|
||
if (doesProjectIdExist) { | ||
t.dropForeign("projectId"); | ||
} | ||
|
||
// add normalized field | ||
if (!doesProjectNameExist) { | ||
t.string("projectName"); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
const doesProjectIdExist = await knex.schema.hasColumn(TableName.AuditLog, "projectId"); | ||
const doesOrgIdExist = await knex.schema.hasColumn(TableName.AuditLog, "orgId"); | ||
const doesProjectNameExist = await knex.schema.hasColumn(TableName.AuditLog, "projectName"); | ||
|
||
if (await knex.schema.hasTable(TableName.AuditLog)) { | ||
await knex.schema.alterTable(TableName.AuditLog, (t) => { | ||
if (doesOrgIdExist) { | ||
t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE"); | ||
} | ||
if (doesProjectIdExist) { | ||
t.foreign("projectId").references("id").inTable(TableName.Project).onDelete("CASCADE"); | ||
} | ||
|
||
// remove normalized field | ||
if (doesProjectNameExist) { | ||
t.dropColumn("projectName"); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters