mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 14:27:23 +00:00
PIT: add backend logic for deep PIT and rollback
This commit is contained in:
@@ -488,6 +488,51 @@ export const secretFolderDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
const findFoldersByRootAndIds = async ({ rootId, folderIds }: { rootId: string; folderIds: string[] }, tx?: Knex) => {
|
||||
try {
|
||||
// First, get all descendant folders of rootId
|
||||
const descendants = await (tx || db.replicaNode())
|
||||
.withRecursive("descendants", (qb) =>
|
||||
qb
|
||||
.select(
|
||||
selectAllTableCols(TableName.SecretFolder),
|
||||
db.raw("0 as depth"),
|
||||
db.raw(`'/' as path`),
|
||||
db.ref(`${TableName.Environment}.slug`).as("environment")
|
||||
)
|
||||
.from(TableName.SecretFolder)
|
||||
.join(TableName.Environment, `${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`)
|
||||
.where(`${TableName.SecretFolder}.id`, rootId)
|
||||
.union((un) => {
|
||||
void un
|
||||
.select(
|
||||
selectAllTableCols(TableName.SecretFolder),
|
||||
db.raw("descendants.depth + 1 as depth"),
|
||||
db.raw(
|
||||
`CONCAT(
|
||||
CASE WHEN descendants.path = '/' THEN '' ELSE descendants.path END,
|
||||
CASE WHEN ${TableName.SecretFolder}."parentId" is NULL THEN '' ELSE CONCAT('/', secret_folders.name) END
|
||||
)`
|
||||
),
|
||||
db.ref("descendants.environment")
|
||||
)
|
||||
.from(TableName.SecretFolder)
|
||||
.where(`${TableName.SecretFolder}.isReserved`, false)
|
||||
.join("descendants", `${TableName.SecretFolder}.parentId`, "descendants.id");
|
||||
})
|
||||
)
|
||||
.select<(TSecretFolders & { path: string; depth: number; environment: string })[]>("*")
|
||||
.from("descendants")
|
||||
.whereIn(`id`, folderIds)
|
||||
.orderBy("depth")
|
||||
.orderBy(`name`);
|
||||
|
||||
return descendants;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "FindFoldersByRootAndIds" });
|
||||
}
|
||||
};
|
||||
|
||||
const findByParentId = async (parentId: string, tx?: Knex) => {
|
||||
try {
|
||||
const folders = await (tx || db.replicaNode())(TableName.SecretFolder)
|
||||
@@ -499,6 +544,17 @@ export const secretFolderDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
const findByEnvId = async (envId: string, tx?: Knex) => {
|
||||
try {
|
||||
const folders = await (tx || db.replicaNode())(TableName.SecretFolder)
|
||||
.where({ envId })
|
||||
.select(selectAllTableCols(TableName.SecretFolder));
|
||||
return folders;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "findByEnvId" });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...secretFolderOrm,
|
||||
update,
|
||||
@@ -511,6 +567,8 @@ export const secretFolderDALFactory = (db: TDbClient) => {
|
||||
findByProjectId,
|
||||
findByMultiEnv,
|
||||
findByEnvsDeep,
|
||||
findByParentId
|
||||
findByParentId,
|
||||
findByEnvId,
|
||||
findFoldersByRootAndIds
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user