mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 14:27:30 +00:00
update trigegr to only create if it doesn't exit
This commit is contained in:
@@ -13,9 +13,9 @@ export async function up(knex: Knex): Promise<void> {
|
|||||||
t.string("name").notNullable();
|
t.string("name").notNullable();
|
||||||
t.string("description").notNullable();
|
t.string("description").notNullable();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await createOnUpdateTrigger(knex, TableName.PkiCollection);
|
await createOnUpdateTrigger(knex, TableName.PkiCollection);
|
||||||
}
|
|
||||||
|
|
||||||
if (!(await knex.schema.hasTable(TableName.PkiCollectionItem))) {
|
if (!(await knex.schema.hasTable(TableName.PkiCollectionItem))) {
|
||||||
await knex.schema.createTable(TableName.PkiCollectionItem, (t) => {
|
await knex.schema.createTable(TableName.PkiCollectionItem, (t) => {
|
||||||
@@ -28,9 +28,9 @@ export async function up(knex: Knex): Promise<void> {
|
|||||||
t.uuid("certId").nullable();
|
t.uuid("certId").nullable();
|
||||||
t.foreign("certId").references("id").inTable(TableName.Certificate).onDelete("CASCADE");
|
t.foreign("certId").references("id").inTable(TableName.Certificate).onDelete("CASCADE");
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await createOnUpdateTrigger(knex, TableName.PkiCollectionItem);
|
await createOnUpdateTrigger(knex, TableName.PkiCollectionItem);
|
||||||
}
|
|
||||||
|
|
||||||
if (!(await knex.schema.hasTable(TableName.PkiAlert))) {
|
if (!(await knex.schema.hasTable(TableName.PkiAlert))) {
|
||||||
await knex.schema.createTable(TableName.PkiAlert, (t) => {
|
await knex.schema.createTable(TableName.PkiAlert, (t) => {
|
||||||
@@ -45,10 +45,10 @@ export async function up(knex: Knex): Promise<void> {
|
|||||||
t.string("recipientEmails").notNullable();
|
t.string("recipientEmails").notNullable();
|
||||||
t.unique(["name", "projectId"]);
|
t.unique(["name", "projectId"]);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await createOnUpdateTrigger(knex, TableName.PkiAlert);
|
await createOnUpdateTrigger(knex, TableName.PkiAlert);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
export async function down(knex: Knex): Promise<void> {
|
||||||
await knex.schema.dropTableIfExists(TableName.PkiAlert);
|
await knex.schema.dropTableIfExists(TableName.PkiAlert);
|
||||||
|
|||||||
+18
-2
@@ -2,6 +2,9 @@ import { Knex } from "knex";
|
|||||||
|
|
||||||
import { TableName } from "./schemas";
|
import { TableName } from "./schemas";
|
||||||
|
|
||||||
|
interface PgTriggerResult {
|
||||||
|
rows: Array<{ exists: boolean }>;
|
||||||
|
}
|
||||||
export const createJunctionTable = (knex: Knex, tableName: TableName, table1Name: TableName, table2Name: TableName) =>
|
export const createJunctionTable = (knex: Knex, tableName: TableName, table1Name: TableName, table2Name: TableName) =>
|
||||||
knex.schema.createTable(tableName, (table) => {
|
knex.schema.createTable(tableName, (table) => {
|
||||||
table.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid());
|
table.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid());
|
||||||
@@ -28,13 +31,26 @@ DROP FUNCTION IF EXISTS on_update_timestamp() CASCADE;
|
|||||||
|
|
||||||
// we would be using this to apply updatedAt where ever we wanta
|
// we would be using this to apply updatedAt where ever we wanta
|
||||||
// remember to set `timestamps(true,true,true)` before this on schema
|
// remember to set `timestamps(true,true,true)` before this on schema
|
||||||
export const createOnUpdateTrigger = (knex: Knex, tableName: string) =>
|
export const createOnUpdateTrigger = async (knex: Knex, tableName: string) => {
|
||||||
knex.raw(`
|
const triggerExists = await knex.raw<PgTriggerResult>(`
|
||||||
|
SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_trigger
|
||||||
|
WHERE tgname = '${tableName}_updatedAt'
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
if (!triggerExists?.rows?.[0]?.exists) {
|
||||||
|
return knex.raw(`
|
||||||
CREATE TRIGGER "${tableName}_updatedAt"
|
CREATE TRIGGER "${tableName}_updatedAt"
|
||||||
BEFORE UPDATE ON ${tableName}
|
BEFORE UPDATE ON ${tableName}
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
EXECUTE PROCEDURE on_update_timestamp();
|
EXECUTE PROCEDURE on_update_timestamp();
|
||||||
`);
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
export const dropOnUpdateTrigger = (knex: Knex, tableName: string) =>
|
export const dropOnUpdateTrigger = (knex: Knex, tableName: string) =>
|
||||||
knex.raw(`DROP TRIGGER IF EXISTS "${tableName}_updatedAt" ON ${tableName}`);
|
knex.raw(`DROP TRIGGER IF EXISTS "${tableName}_updatedAt" ON ${tableName}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user