From 0f50fe2a5e8dd9c21ddf030dcb9ed0550c216d0e Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Fri, 17 Jun 2022 08:10:01 -0400 Subject: [PATCH] Expressions: Fixes dashboard schema migration issue that casued Expression datasource to be set on panel level (#50945) (#51009) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Expressions: Fixes dashboard schema migration issue that casued Expression datasource to be set on panel level * fixing logic * Updated (cherry picked from commit eb25d8df894ac7d0b938bf277d42b67db1541e47) Co-authored-by: Torkel Ödegaard --- .../DashExportModal/DashboardExporter.test.ts | 1 + .../dashboard/state/DashboardMigrator.test.ts | 32 +++++++++++++++++++ .../dashboard/state/DashboardMigrator.ts | 10 +++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/public/app/features/dashboard/components/DashExportModal/DashboardExporter.test.ts b/public/app/features/dashboard/components/DashExportModal/DashboardExporter.test.ts index 5c344340666..e844c8c5175 100644 --- a/public/app/features/dashboard/components/DashExportModal/DashboardExporter.test.ts +++ b/public/app/features/dashboard/components/DashExportModal/DashboardExporter.test.ts @@ -345,6 +345,7 @@ describe('given dashboard with repeated panels', () => { expect(element.model).toEqual({ id: 17, datasource: { type: 'other2', uid: '$ds' }, + targets: [{ refId: 'A', datasource: { type: 'other2', uid: '$ds' } }], type: 'graph', }); }); diff --git a/public/app/features/dashboard/state/DashboardMigrator.test.ts b/public/app/features/dashboard/state/DashboardMigrator.test.ts index a2269ab56df..2f0dc793de0 100644 --- a/public/app/features/dashboard/state/DashboardMigrator.test.ts +++ b/public/app/features/dashboard/state/DashboardMigrator.test.ts @@ -1993,6 +1993,38 @@ describe('DashboardModel', () => { expect(model.panels[0].targets[0].datasource).toEqual({ type: 'prometheus', uid: 'prom2-uid' }); }); }); + + describe('when migrating default (null) datasource with panel with expressions queries', () => { + let model: DashboardModel; + + beforeEach(() => { + model = new DashboardModel({ + panels: [ + { + id: 2, + targets: [ + { + refId: 'A', + }, + { + refId: 'B', + datasource: '__expr__', + }, + ], + }, + ], + schemaVersion: 30, + }); + }); + + it('should update panel datasource props to default datasource', () => { + expect(model.panels[0].datasource).toEqual({ type: 'prometheus', uid: 'prom2-uid' }); + }); + + it('should update target datasource props to default data source', () => { + expect(model.panels[0].targets[0].datasource).toEqual({ type: 'prometheus', uid: 'prom2-uid' }); + }); + }); }); function createRow(options: any, panelDescriptions: any[]) { diff --git a/public/app/features/dashboard/state/DashboardMigrator.ts b/public/app/features/dashboard/state/DashboardMigrator.ts index 79b747773e9..212f8d273e4 100644 --- a/public/app/features/dashboard/state/DashboardMigrator.ts +++ b/public/app/features/dashboard/state/DashboardMigrator.ts @@ -761,15 +761,15 @@ export class DashboardMigrator { } for (const target of panel.targets) { - if (target.datasource && panelDataSourceWasDefault) { + if (target.datasource == null || target.datasource.uid == null) { + target.datasource = { ...panel.datasource }; + } + + if (panelDataSourceWasDefault && target.datasource.uid !== '__expr__') { // We can have situations when default ds changed and the panel level data source is different from the queries // In this case we use the query level data source as source for truth panel.datasource = target.datasource as DataSourceRef; } - - if (target.datasource === null) { - target.datasource = getDataSourceRef(defaultDs); - } } } return panel;