From a721dd42b310b92a86b426efc7f782b643595fd7 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 28 Jun 2021 10:12:46 -0400 Subject: [PATCH] Explore: Fix import of queries between SQL data sources (#36210) * Remove datasource property when importing same ds type queries * Refactor imported queries * Update public/app/features/explore/state/query.ts Co-authored-by: Giordano Ricci Co-authored-by: Giordano Ricci --- .../app/features/explore/state/datasource.ts | 3 +- .../app/features/explore/state/query.test.ts | 37 +++++++++++++++++++ public/app/features/explore/state/query.ts | 6 +-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/public/app/features/explore/state/datasource.ts b/public/app/features/explore/state/datasource.ts index e6256e8d100..342e61fe47d 100644 --- a/public/app/features/explore/state/datasource.ts +++ b/public/app/features/explore/state/datasource.ts @@ -51,9 +51,8 @@ export function changeDatasource( }) ); - const queries = getState().explore[exploreId]!.queries; - if (options?.importQueries) { + const queries = getState().explore[exploreId]!.queries; await dispatch(importQueries(exploreId, queries, currentDataSourceInstance, instance)); } diff --git a/public/app/features/explore/state/query.test.ts b/public/app/features/explore/state/query.test.ts index d3370872a51..518a0db0d73 100644 --- a/public/app/features/explore/state/query.test.ts +++ b/public/app/features/explore/state/query.test.ts @@ -6,6 +6,7 @@ import { cancelQueriesAction, queryReducer, removeQueryRowAction, + importQueries, runQueries, scanStartAction, scanStopAction, @@ -22,6 +23,9 @@ import { PanelData, DataFrame, LoadingState, + DataSourceApi, + DataSourceJsonData, + DataQuery, } from '@grafana/data'; import { thunkTester } from 'test/core/thunk/thunkTester'; import { makeExplorePaneState } from './utils'; @@ -124,6 +128,39 @@ describe('running queries', () => { }); }); +describe('importing queries', () => { + describe('when importing queries between the same type of data source', () => { + it('remove datasource property from all of the queries', async () => { + const store = configureStore({ + ...(defaultInitialState as any), + explore: { + [ExploreId.left]: { + ...defaultInitialState.explore[ExploreId.left], + datasourceInstance: { name: 'testDs', type: 'postgres' }, + }, + }, + }); + + await store.dispatch( + importQueries( + ExploreId.left, + [ + { datasource: 'postgres1', refId: 'refId_A' }, + { datasource: 'postgres1', refId: 'refId_B' }, + ], + { name: 'Postgres1', type: 'postgres' } as DataSourceApi, + { name: 'Postgres2', type: 'postgres' } as DataSourceApi + ) + ); + + expect(store.getState().explore[ExploreId.left].queries[0]).toHaveProperty('refId', 'refId_A'); + expect(store.getState().explore[ExploreId.left].queries[1]).toHaveProperty('refId', 'refId_B'); + expect(store.getState().explore[ExploreId.left].queries[0]).not.toHaveProperty('datasource'); + expect(store.getState().explore[ExploreId.left].queries[1]).not.toHaveProperty('datasource'); + }); + }); +}); + describe('reducer', () => { describe('scanning', () => { it('should start scanning', () => { diff --git a/public/app/features/explore/state/query.ts b/public/app/features/explore/state/query.ts index 79ebde5bc71..50dab0f612a 100644 --- a/public/app/features/explore/state/query.ts +++ b/public/app/features/explore/state/query.ts @@ -264,8 +264,8 @@ export const importQueries = ( let importedQueries = queries; // Check if queries can be imported from previously selected datasource if (sourceDataSource.meta?.id === targetDataSource.meta?.id) { - // Keep same queries if same type of datasource - importedQueries = [...queries]; + // Keep same queries if same type of datasource, but delete datasource query property to prevent mismatch of new and old data source instance + importedQueries = queries.map(({ datasource, ...query }) => query); } else if (targetDataSource.importQueries) { // Datasource-specific importers importedQueries = await targetDataSource.importQueries(queries, sourceDataSource); @@ -340,7 +340,7 @@ export const runQueries = (exploreId: ExploreId, options?: { replaceUrl?: boolea dispatch(queryStreamUpdatedAction({ exploreId, response: data })); }); - // If we don't have resuls saved in cache, run new queries + // If we don't have results saved in cache, run new queries } else { if (!hasNonEmptyQuery(queries)) { dispatch(clearQueriesAction({ exploreId }));