Skip to content

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

add missing index

Thiago Araujo da Silva3b82489a05d3
diff --git a/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.ts b/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.ts
new file mode 100644
index 0000000000..4bde8ac819
--- /dev/null
+++ b/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.ts
@@ -0,0 +1,36 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+const RESOURCE_LOOKUP_INDEX = "idx_alerts_resource_type_resource_id";
+const MIGRATION_TIMEOUT = 60 * 60 * 1000;
+const MIGRATION_LOCK_TIMEOUT = 30 * 1000;
+
+export async function up(knex: Knex): Promise<void> {
+ const stmtResult = await knex.raw("SHOW statement_timeout");
+ const originalStatementTimeout = stmtResult.rows[0].statement_timeout;
+ const lockResult = await knex.raw("SHOW lock_timeout");
+ const originalLockTimeout = lockResult.rows[0].lock_timeout;
+
+ try {
+ await knex.raw(`SET statement_timeout = ${MIGRATION_TIMEOUT}`);
+ await knex.raw(`SET lock_timeout = ${MIGRATION_LOCK_TIMEOUT}`);
+
+ if (await knex.schema.hasColumn(TableName.Alert, "resourceId")) {
+ await knex.raw(`
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS "${RESOURCE_LOOKUP_INDEX}"
+ ON ${TableName.Alert} ("resourceType", "resourceId")
+ `);
+ }
+ } finally {
+ await knex.raw(`SET statement_timeout = '${originalStatementTimeout}'`);
+ await knex.raw(`SET lock_timeout = '${originalLockTimeout}'`);
+ }
+}
+
+export async function down(knex: Knex): Promise<void> {
+ await knex.raw(`DROP INDEX CONCURRENTLY IF EXISTS "${RESOURCE_LOOKUP_INDEX}"`);
+}
+
+const config = { transaction: false };
+export { config };