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 93fe3e3c98b..6acfbd00117 100644 --- a/public/app/features/dashboard/state/DashboardMigrator.test.ts +++ b/public/app/features/dashboard/state/DashboardMigrator.test.ts @@ -1992,6 +1992,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 589c814557b..d1becd26b2c 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;