-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: initial setup for migration of audit logs
- Loading branch information
1 parent
e6e1ed7
commit 86cb513
Showing
16 changed files
with
227 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DB_CONNECTION_URI= | ||
AUDIT_LOGS_DB_CONNECTION_URI= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// eslint-disable-next-line | ||
import "ts-node/register"; | ||
|
||
import dotenv from "dotenv"; | ||
import type { Knex } from "knex"; | ||
import path from "path"; | ||
|
||
// Update with your config settings. . | ||
dotenv.config({ | ||
path: path.join(__dirname, "../../../.env.migration") | ||
}); | ||
dotenv.config({ | ||
path: path.join(__dirname, "../../../.env") | ||
}); | ||
|
||
if (!process.env.AUDIT_LOGS_DB_CONNECTION_URI && !process.env.AUDIT_LOGS_DB_HOST) { | ||
console.info("Dedicated audit log database not found. No further migrations necessary"); | ||
process.exit(0); | ||
} | ||
|
||
export default { | ||
development: { | ||
client: "postgres", | ||
connection: { | ||
connectionString: process.env.AUDIT_LOGS_DB_CONNECTION_URI, | ||
host: process.env.AUDIT_LOGS_DB_HOST, | ||
port: process.env.AUDIT_LOGS_DB_PORT, | ||
user: process.env.AUDIT_LOGS_DB_USER, | ||
database: process.env.AUDIT_LOGS_DB_NAME, | ||
password: process.env.AUDIT_LOGS_DB_PASSWORD, | ||
ssl: process.env.AUDIT_LOGS_DB_ROOT_CERT | ||
? { | ||
rejectUnauthorized: true, | ||
ca: Buffer.from(process.env.AUDIT_LOGS_DB_ROOT_CERT, "base64").toString("ascii") | ||
} | ||
: false | ||
}, | ||
pool: { | ||
min: 2, | ||
max: 10 | ||
}, | ||
seeds: { | ||
directory: "./seeds" | ||
}, | ||
migrations: { | ||
tableName: "infisical_migrations" | ||
} | ||
}, | ||
production: { | ||
client: "postgres", | ||
connection: { | ||
connectionString: process.env.AUDIT_LOGS_DB_CONNECTION_URI, | ||
host: process.env.AUDIT_LOGS_DB_HOST, | ||
port: process.env.AUDIT_LOGS_DB_PORT, | ||
user: process.env.AUDIT_LOGS_DB_USER, | ||
database: process.env.AUDIT_LOGS_DB_NAME, | ||
password: process.env.AUDIT_LOGS_DB_PASSWORD, | ||
ssl: process.env.AUDIT_LOGS_DB_ROOT_CERT | ||
? { | ||
rejectUnauthorized: true, | ||
ca: Buffer.from(process.env.AUDIT_LOGS_DB_ROOT_CERT, "base64").toString("ascii") | ||
} | ||
: false | ||
}, | ||
pool: { | ||
min: 2, | ||
max: 10 | ||
}, | ||
migrations: { | ||
tableName: "infisical_migrations" | ||
} | ||
} | ||
} as Knex.Config; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type { TDbClient } from "./instance"; | ||
export { initDbConnection } from "./instance"; | ||
export { initAuditLogDbConnection, initDbConnection } from "./instance"; |
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
53 changes: 53 additions & 0 deletions
53
backend/src/db/migrations/20241002092243_audit-log-drop-fk.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,53 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "../schemas"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
const doesProjectIdExist = await knex.schema.hasColumn(TableName.AuditLog, "projectId"); | ||
const doesOrgIdExist = await knex.schema.hasColumn(TableName.AuditLog, "orgId"); | ||
const doesTableExist = await knex.schema.hasTable(TableName.AuditLog); | ||
|
||
const doesProjectNameExist = await knex.schema.hasColumn(TableName.AuditLog, "projectName"); | ||
|
||
if (doesTableExist) { | ||
await knex.schema.alterTable(TableName.AuditLog, (t) => { | ||
// remove existing FKs | ||
if (doesOrgIdExist) { | ||
t.dropForeign("orgId"); | ||
} | ||
|
||
if (doesProjectIdExist) { | ||
t.dropForeign("projectId"); | ||
} | ||
|
||
// add normalized fields necessary after FK removal | ||
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 doesTableExist = await knex.schema.hasTable(TableName.AuditLog); | ||
const doesProjectNameExist = await knex.schema.hasColumn(TableName.AuditLog, "projectName"); | ||
|
||
if (doesTableExist) { | ||
await knex.schema.alterTable(TableName.AuditLog, (t) => { | ||
// add back FKs | ||
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 fields | ||
if (doesProjectNameExist) { | ||
t.dropColumn("projectName"); | ||
} | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/db/migrations/20241002110531_add-audit-log-metadata-index.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,15 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "../schemas"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
if (await knex.schema.hasColumn(TableName.AuditLog, "actorMetadata")) { | ||
await knex.raw( | ||
`CREATE INDEX "audit_logs_actorMetadata_idx" ON ${TableName.AuditLog} USING gin("actorMetadata" jsonb_path_ops)` | ||
); | ||
} | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.raw(`DROP INDEX IF EXISTS "audit_logs_actorMetadata_idx"`); | ||
} |
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
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
Oops, something went wrong.