fix: Make git sync untracked work with sqlite.

The previous implementation of the sync untracked feature would attempt
to run multiple queries in parallel. The `knex` framework appears to use
a separate connection for each query by default (unless the queries are
tied together by a transaction). When using sqlite, there is only a
single connection available in the knex database pool, causing this
feature to deadlock (when the initial query can not return all results
immediatly).

In this change, we switch to doing all of these queries in a
transaction, which causes all quries to occur on a single connection.
pull/7822/head
Greg Darke 2 months ago
parent b49c00226c
commit ee97e248bc

@ -1,4 +1,5 @@
const Model = require('objection').Model
const knex = require('knex')
const _ = require('lodash')
/* global WIKI */
@ -60,9 +61,11 @@ module.exports = class AssetFolder extends Model {
/**
* Get full folder paths
*
* @param {knex.Transaction?} trx Currently running transaction (if any)
*/
static async getAllPaths () {
const all = await WIKI.models.assetFolders.query()
static async getAllPaths (trx) {
const all = await WIKI.models.assetFolders.query(trx)
let folders = {}
all.forEach(fld => {
_.set(folders, fld.id, fld.slug)

@ -471,16 +471,17 @@ module.exports = {
async syncUntracked() {
WIKI.logger.info(`(STORAGE/GIT) Adding all untracked content...`)
await WIKI.models.knex.transaction(async (trx) => {
// -> Pages
await pipeline(
WIKI.models.knex.column('id', 'path', 'localeCode', 'title', 'description', 'contentType', 'content', 'isPublished', 'updatedAt', 'createdAt', 'editorKey').select().from('pages').where({
trx.column('id', 'path', 'localeCode', 'title', 'description', 'contentType', 'content', 'isPublished', 'updatedAt', 'createdAt', 'editorKey').select().from('pages').where({
isPrivate: false
}).stream(),
new stream.Transform({
objectMode: true,
transform: async (page, enc, cb) => {
const pageObject = await WIKI.models.pages.query().findById(page.id)
page.tags = await pageObject.$relatedQuery('tags')
const pageObject = await WIKI.models.pages.query(trx).findById(page.id)
page.tags = await pageObject.$relatedQuery('tags', trx)
let fileName = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
if (this.config.alwaysNamespace || (WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode)) {
@ -496,10 +497,10 @@ module.exports = {
)
// -> Assets
const assetFolders = await WIKI.models.assetFolders.getAllPaths()
const assetFolders = await WIKI.models.assetFolders.getAllPaths(trx)
await pipeline(
WIKI.models.knex.column('filename', 'folderId', 'data').select().from('assets').join('assetData', 'assets.id', '=', 'assetData.id').stream(),
trx.column('filename', 'folderId', 'data').select().from('assets').join('assetData', 'assets.id', '=', 'assetData.id').stream(),
new stream.Transform({
objectMode: true,
transform: async (asset, enc, cb) => {
@ -511,6 +512,9 @@ module.exports = {
}
})
)
await trx.commit()
})
await this.git.commit(`docs: add all untracked content`)
WIKI.logger.info('(STORAGE/GIT) All content is now tracked.')

Loading…
Cancel
Save