From f0b76784440d065a5d25589bd2726c840cee31d3 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Sun, 21 Nov 2021 00:19:10 +0100 Subject: [PATCH] Fixed upsert failing on empty insert array due breaking Knex API change. --- seeds/00_tags.js | 6 +++--- src/utils/upsert.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/seeds/00_tags.js b/seeds/00_tags.js index 0729e6d0..0f3446d2 100644 --- a/seeds/00_tags.js +++ b/seeds/00_tags.js @@ -2021,13 +2021,13 @@ const aliases = [ }, ]; -exports.seed = knex => Promise.resolve() +exports.seed = (knex) => Promise.resolve() .then(async () => upsert('tags_groups', groups, 'slug', knex)) .then(async () => { const groupEntries = await knex('tags_groups').select('*'); const groupsMap = groupEntries.reduce((acc, { id, slug }) => ({ ...acc, [slug]: id }), {}); - const tagsWithGroups = tags.map(tag => ({ + const tagsWithGroups = tags.map((tag) => ({ name: tag.name, slug: tag.slug || slugify(tag.name), description: tag.description, @@ -2042,7 +2042,7 @@ exports.seed = knex => Promise.resolve() const tagEntries = await knex('tags').select('*').where({ alias_for: null }); const tagsMap = tagEntries.reduce((acc, { id, slug }) => ({ ...acc, [slug]: id }), {}); - const tagAliases = aliases.map(alias => ({ + const tagAliases = aliases.map((alias) => ({ name: alias.name, alias_for: tagsMap[alias.for], secondary: !!alias.secondary, diff --git a/src/utils/upsert.js b/src/utils/upsert.js index a78297fa..88f53297 100644 --- a/src/utils/upsert.js +++ b/src/utils/upsert.js @@ -33,8 +33,8 @@ async function upsert(table, items, identifier = ['id'], _knex) { logger.debug(`${table}: Updating ${update.length}`); const [inserted, updated] = await Promise.all([ - knex(table).returning('*').insert(insert), - knex.transaction(async (trx) => Promise.all(update.map((item) => { + insert.length > 0 ? knex(table).returning('*').insert(insert) : [], + update.length > 0 ? knex.transaction(async (trx) => Promise.all(update.map((item) => { const clause = identifiers.reduce((acc, identifierX) => ({ ...acc, [identifierX]: item[identifierX] }), {}); return trx @@ -42,7 +42,7 @@ async function upsert(table, items, identifier = ['id'], _knex) { .update(item) .into(table) .returning('*'); - }))), + }))) : [], ]); return {