From 8badd8fd5591a4f20e03547cd360aedb7ddc2f92 Mon Sep 17 00:00:00 2001 From: apif <48519412+apif@users.noreply.github.com> Date: Sat, 26 Oct 2019 00:29:28 +0200 Subject: [PATCH] fix: MSSQL - assetFolder getHierarchy query (#1131) --- server/models/assetFolders.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/server/models/assetFolders.js b/server/models/assetFolders.js index c15cfba4..965eb40c 100644 --- a/server/models/assetFolders.js +++ b/server/models/assetFolders.js @@ -40,11 +40,21 @@ module.exports = class AssetFolder extends Model { * @param {Number} folderId Id of the folder */ static async getHierarchy (folderId) { - const hier = await WIKI.models.knex.withRecursive('ancestors', qb => { - qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).union(sqb => { - sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id') - }) - }).select('*').from('ancestors') + let hier + if (WIKI.config.db.type === 'mssql') { + hier = await WIKI.models.knex.with('ancestors', qb => { + qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).unionAll(sqb => { + sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id') + }) + }).select('*').from('ancestors') + } + else { + hier = await WIKI.models.knex.withRecursive('ancestors', qb => { + qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).union(sqb => { + sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id') + }) + }).select('*').from('ancestors') + } // The ancestors are from children to grandparents, must reverse for correct path order. return _.reverse(hier) }