mirror of https://github.com/grafana/grafana
UpdateQueries: Fixes issues setting datasource on queries after changing type (#41702)
parent
99900cbdd8
commit
51ef770c2b
@ -0,0 +1,102 @@ |
||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend'; |
||||
import { updateQueries } from './updateQueries'; |
||||
|
||||
describe('updateQueries', () => { |
||||
it('Should update all queries except expression query when changing data source with same type', () => { |
||||
const updated = updateQueries( |
||||
{ |
||||
uid: 'new-uid', |
||||
type: 'same-type', |
||||
meta: {}, |
||||
} as any, |
||||
[ |
||||
{ |
||||
refId: 'A', |
||||
datasource: { |
||||
uid: 'old-uid', |
||||
type: 'same-type', |
||||
}, |
||||
}, |
||||
{ |
||||
refId: 'B', |
||||
datasource: ExpressionDatasourceRef, |
||||
}, |
||||
], |
||||
{ |
||||
uid: 'old-uid', |
||||
type: 'same-type', |
||||
} as any |
||||
); |
||||
|
||||
expect(updated[0].datasource).toEqual({ type: 'same-type', uid: 'new-uid' }); |
||||
expect(updated[1].datasource).toEqual(ExpressionDatasourceRef); |
||||
}); |
||||
|
||||
it('Should clear queries when changing type', () => { |
||||
const updated = updateQueries( |
||||
{ |
||||
uid: 'new-uid', |
||||
type: 'new-type', |
||||
meta: {}, |
||||
} as any, |
||||
[ |
||||
{ |
||||
refId: 'A', |
||||
datasource: { |
||||
uid: 'old-uid', |
||||
type: 'old-type', |
||||
}, |
||||
}, |
||||
{ |
||||
refId: 'B', |
||||
datasource: { |
||||
uid: 'old-uid', |
||||
type: 'old-type', |
||||
}, |
||||
}, |
||||
], |
||||
{ |
||||
uid: 'old-uid', |
||||
type: 'old-type', |
||||
} as any |
||||
); |
||||
|
||||
expect(updated.length).toEqual(1); |
||||
expect(updated[0].datasource).toEqual({ type: 'new-type', uid: 'new-uid' }); |
||||
}); |
||||
|
||||
it('Should preserve query data source when changing to mixed', () => { |
||||
const updated = updateQueries( |
||||
{ |
||||
uid: 'mixed', |
||||
type: 'mixed', |
||||
meta: { |
||||
mixed: true, |
||||
}, |
||||
} as any, |
||||
[ |
||||
{ |
||||
refId: 'A', |
||||
datasource: { |
||||
uid: 'old-uid', |
||||
type: 'old-type', |
||||
}, |
||||
}, |
||||
{ |
||||
refId: 'B', |
||||
datasource: { |
||||
uid: 'other-uid', |
||||
type: 'other-type', |
||||
}, |
||||
}, |
||||
], |
||||
{ |
||||
uid: 'old-uid', |
||||
type: 'old-type', |
||||
} as any |
||||
); |
||||
|
||||
expect(updated[0].datasource).toEqual({ type: 'old-type', uid: 'old-uid' }); |
||||
expect(updated[1].datasource).toEqual({ type: 'other-type', uid: 'other-uid' }); |
||||
}); |
||||
}); |
@ -0,0 +1,29 @@ |
||||
import { DataQuery, DataSourceInstanceSettings, getDataSourceRef } from '@grafana/data'; |
||||
import { isExpressionReference } from '@grafana/runtime/src/utils/DataSourceWithBackend'; |
||||
|
||||
export function updateQueries( |
||||
newSettings: DataSourceInstanceSettings, |
||||
queries: DataQuery[], |
||||
dsSettings?: DataSourceInstanceSettings |
||||
): DataQuery[] { |
||||
const datasource = getDataSourceRef(newSettings); |
||||
|
||||
// we are changing data source type
|
||||
if (dsSettings?.type !== newSettings.type) { |
||||
// If changing to mixed do nothing
|
||||
if (newSettings.meta.mixed) { |
||||
return queries; |
||||
} else { |
||||
// Changing to another datasource type clear queries
|
||||
return [{ refId: 'A', datasource }]; |
||||
} |
||||
} |
||||
|
||||
// Set data source on all queries except expression queries
|
||||
return queries.map((query) => { |
||||
if (!isExpressionReference(query.datasource)) { |
||||
query.datasource = datasource; |
||||
} |
||||
return query; |
||||
}); |
||||
} |
Loading…
Reference in new issue