From 19b9e593d6deed6679f5322e9724975b32290638 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 16 Jun 2022 09:21:01 +0100 Subject: [PATCH] Fix: Handle datasource not exists for query --- .../src/migrations/functions/backfill/app.ts | 1 + .../functions/backfill/app/queries.ts | 21 +++++++++++++++---- .../migrations/functions/backfill/global.ts | 4 +++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/server/src/migrations/functions/backfill/app.ts b/packages/server/src/migrations/functions/backfill/app.ts index 0f53e4d0ad..476344e7b0 100644 --- a/packages/server/src/migrations/functions/backfill/app.ts +++ b/packages/server/src/migrations/functions/backfill/app.ts @@ -20,6 +20,7 @@ const handleError = (e: any, errors?: any) => { } return } + console.trace(e) throw e } diff --git a/packages/server/src/migrations/functions/backfill/app/queries.ts b/packages/server/src/migrations/functions/backfill/app/queries.ts index e07dec8c06..b2ea31c19c 100644 --- a/packages/server/src/migrations/functions/backfill/app/queries.ts +++ b/packages/server/src/migrations/functions/backfill/app/queries.ts @@ -22,10 +22,23 @@ export const backfill = async (appDb: any, timestamp: string | number) => { const queries: Query[] = await getQueries(appDb) for (const query of queries) { - const datasource: Datasource = await getDatasource( - appDb, - query.datasourceId - ) + let datasource: Datasource + + try { + datasource = await getDatasource(appDb, query.datasourceId) + } catch (e: any) { + // handle known bug where a datasource has been deleted + // and the query has not + if (e.status === 404) { + datasource = { + _id: query.datasourceId, + source: "unknown", + } + } else { + throw e + } + } + await events.query.created(datasource, query, timestamp) } diff --git a/packages/server/src/migrations/functions/backfill/global.ts b/packages/server/src/migrations/functions/backfill/global.ts index d61e89fcc9..5dd812d6ea 100644 --- a/packages/server/src/migrations/functions/backfill/global.ts +++ b/packages/server/src/migrations/functions/backfill/global.ts @@ -105,7 +105,9 @@ export const run = async (db: any) => { if (!allUsers || allUsers.length === 0) { // first time startup - we don't need to backfill anything // tenant will be identified when admin user is created - await events.installation.firstStartup() + if (env.SELF_HOSTED) { + await events.installation.firstStartup() + } return }