Expressions: Fixes dashboard schema migration issue that casued Expression datasource to be set on panel level (#50945)

* Expressions: Fixes dashboard schema migration issue that casued Expression datasource to be set on panel level

* fixing logic

* Updated
This commit is contained in:
Torkel Ödegaard 2022-06-17 08:57:11 +02:00 committed by GitHub
parent ccc587dc0f
commit eb25d8df89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 5 deletions

View File

@ -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',
});
});

View File

@ -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[]) {

View File

@ -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;