From 2f58311eea1de21d517e3c6b599cc818b2444d11 Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:35:01 +0100 Subject: [PATCH] Alerting: Fix setting datasource uid, when datasource is string in old version (#96085) fix setting datasource uid, when datasource is string in old versions rules --- .../unified/components/rule-editor/QueryWrapper.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx index e7366b34f2e..62f2ea6159a 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx @@ -98,7 +98,18 @@ export const QueryWrapper = ({ // It's unclear as to why this happens, but we need better visibility on why this happens, // so we log when it does, and make the query model datasource UID match the datasource UID // We already elsewhere work under the assumption that the datasource settings are fetched from the datasourceUid property - queryWithDefaults.datasource.uid = query.datasourceUid; + + // This check is necessary for some few cases where the datasource might be an string instead of an object + // see: https://github.com/grafana/grafana/issues/96040 for more context + if (typeof queryWithDefaults.datasource === 'object' && Boolean(queryWithDefaults.datasource)) { + queryWithDefaults.datasource.uid = query.datasourceUid; + } else { + // if the datasource is a string, we need to convert it to an object, and populate the fields from the query model + queryWithDefaults.datasource = {}; + queryWithDefaults.datasource.uid = query.datasourceUid; + queryWithDefaults.datasource.type = query.model.datasource?.type; + queryWithDefaults.datasource.apiVersion = query.model.datasource?.apiVersion; + } } function SelectingDataSourceTooltip() {