Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Datasources: Reinstate SQL data source behavior around database selection when default configured databases already exist #65659

Merged
merged 138 commits into from Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 135 commits
Commits
Show all changes
138 commits
Select commit Hold shift + click to select a range
195f522
badlm0mma/sql_datasource_update/ initial notes
baldm0mma Mar 30, 2023
600d540
baldm0mma/sql_datasource_update/ solution
baldm0mma Mar 30, 2023
21dc37d
baldm0mma/sql_datasource_update/ clean datasetSelector
baldm0mma Mar 30, 2023
5af644c
baldm0mma/sql_datasource_update/ clean up queryEditor.tsx
baldm0mma Mar 30, 2023
e48c102
baldm0mma/sql_datasource_update/ clewan up queryHeader.tsx
baldm0mma Mar 30, 2023
cefbf57
baldm0mma/sql_datasource_update/ clean up tableSelector.tsx
baldm0mma Mar 30, 2023
53fd198
baldm0mma/sql_datasource_update/ clean up mysqlDatasource.ts
baldm0mma Mar 30, 2023
a6dbd87
baldm0mma/sql_datasource_update/ clean up configurationEditor.tsx
baldm0mma Mar 30, 2023
3b10e07
baldm0mma/sql_datasource_update/ rem conlog from queryEditor.tsx
baldm0mma Mar 30, 2023
26ac769
baldm0mma/sql_datasource_update/ rem conlog from queryEditor.tsx
baldm0mma Mar 30, 2023
3ba3a57
baldm0mma/sql_datasource_update/ remove conlog in tableSelector.tsx
baldm0mma Apr 3, 2023
d4395dd
baldm0mma/sql_datasource_update/ rem conlog in sqlDatasource.ts
baldm0mma Apr 3, 2023
105a6f5
baldm0mma/sql_datasource_update/ update deafult database value in sql…
baldm0mma Apr 3, 2023
1e74578
baldm0mma/sql_datasource_update/ update logic to accomidate no preconfig
baldm0mma Apr 3, 2023
0c559e1
baldm0mma/sql_datasource_update/ update props
baldm0mma Apr 3, 2023
31b6cd1
baldm0mma/sql_datasource_update/ update prop names
baldm0mma Apr 3, 2023
91f22b7
baldm0mma/sql_datasource_update/ update prop names in tableSelector
baldm0mma Apr 3, 2023
719a474
baldm0mma/sql_datasource_update/ update annos in datasetSelector
baldm0mma Apr 3, 2023
3be7f51
baldm0mma/sql_datasource_update/ update naming
baldm0mma Apr 3, 2023
0ffd950
baldm0mma/sql_datasource_update/ update to standard langauae
baldm0mma Apr 3, 2023
fdc7805
baldm0mma/sql_datasource_update/ update prop names
baldm0mma Apr 3, 2023
73b37d2
baldm0mma/sql_datasource_update/ update annos in datasetSelector
baldm0mma Apr 3, 2023
f137d75
baldm0mma/sql_datasource_update/ remove unused import in tableSelecto…
baldm0mma Apr 3, 2023
ec1c5c6
baldm0mma/sql_datasource_update/ remove addDefaultdataset
baldm0mma Apr 3, 2023
ad3dc0b
baldm0mma/sql_datasource_update/ reset query when needed
baldm0mma Apr 3, 2023
2483152
baldm0mma/sql_datasource_update/ update asymc return val
baldm0mma Apr 3, 2023
ee6f90c
baldm0mma/sql_datasource_update/ remove psql query editor
baldm0mma Apr 4, 2023
46d2e53
baldm0mma/sql_datasource_update/ remove con logs in defaults.ts
baldm0mma Apr 4, 2023
3212b46
baldm0mma/sql_datasource_update/ revert postgres changes
baldm0mma Apr 4, 2023
d2bb165
baldm0mma/sql_datasource_update/ update postgres naming
baldm0mma Apr 4, 2023
b98e7c8
baldm0mma/sql_datasource_update/ add altert
baldm0mma Apr 4, 2023
a349325
baldm0mma/sql_datasource_update/ update annos and add alerts and aler…
baldm0mma Apr 4, 2023
2b33ac1
baldm0mma/sql_datasource_update/ update postgres nomenclature
baldm0mma Apr 4, 2023
2d76d3e
baldm0mma/sql_datasource_update/ update annos and remove con logs
baldm0mma Apr 4, 2023
0d5ca8e
baldm0mma/sql_datasource_update/ update nomenclature
baldm0mma Apr 4, 2023
4e13ea8
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma Apr 4, 2023
51fd0b3
baldm0mma/sql_datasource_update/ drone fix
baldm0mma Apr 4, 2023
47e8f3a
baldm0mma/sql_datasource_update/ export and format
baldm0mma Apr 6, 2023
3804612
baldm0mma/sql_datasource_update/ update docs for mysql datasource wit…
baldm0mma Apr 6, 2023
9c1f88d
baldm0mma/sql_datasource_update/ update mssql docs
baldm0mma Apr 6, 2023
29b54d7
baldm0mma/sql_datasource_update/ update postgres docs and alert logic
baldm0mma Apr 6, 2023
12c8a6c
baldm0mma/sql_datasource_update/ add tests
baldm0mma Apr 6, 2023
e908f28
baldm0mma/sql_datasource_update/ update docs
baldm0mma Apr 6, 2023
b3e9c73
baldm0mma/sql_datasource_update/ update test names
baldm0mma Apr 6, 2023
3efd313
baldm0mma/sql_datasource_update/ update alert language
baldm0mma Apr 6, 2023
fc873e1
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma Apr 7, 2023
92433f3
baldm0mma/sql_datasource_update/ correct spelling
baldm0mma Apr 7, 2023
f3ccc5c
baldm0mma/sql_datasource_update/ update postgres query builder image
baldm0mma Apr 7, 2023
fe69a45
baldm0mma/sql_datasource_update/ update annos
baldm0mma Apr 7, 2023
e710934
baldm0mma/sql_datasource_update/ update tests
baldm0mma Apr 10, 2023
6b1d125
baldm0mma/sql_datasource_update/ docs updated
baldm0mma Apr 11, 2023
d5bb438
baldm0mma/sql_datsource_update/ refactor determinePlaceholder
baldm0mma Apr 11, 2023
579a961
Update public/app/features/plugins/sql/components/QueryEditor.tsx
baldm0mma Apr 11, 2023
372c760
Update public/app/features/plugins/sql/components/QueryEditor.tsx
baldm0mma Apr 11, 2023
402baf2
baldm0mma/sql_datasource_update/ remove superfluous cleanup data from…
baldm0mma Apr 11, 2023
f3d2c8d
baldm0mma/sql_datasource_update/ update spelling
baldm0mma Apr 11, 2023
2758370
Merge branch 'baldm0mma/sql_datasource_update' of github.com:grafana/…
baldm0mma Apr 11, 2023
a366a93
Update public/app/features/plugins/sql/components/SqlComponents.test.tsx
baldm0mma Apr 11, 2023
085854d
baldm0mma/sql_datasource_update/ add logic to prevent db call for tab…
baldm0mma Apr 11, 2023
655ffc1
baldm0mma/sql_datasource_update/ update alert tests
baldm0mma Apr 11, 2023
aa5c503
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma Apr 11, 2023
c7fc10d
baldm0mma/sql_datasource_update/ update button text
baldm0mma Apr 11, 2023
f50b821
baldm0mma/sql_datasource_update/ update tests to preferred standard
baldm0mma Apr 13, 2023
dcb5634
baldm0mma/sql_datasource_update/add feature flag
baldm0mma Apr 13, 2023
d689b8a
baldm0mma/sql_datasource_update/ update mssql docs
baldm0mma Apr 13, 2023
b9e3171
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma Apr 14, 2023
210ea92
baldm0mma/sql_datasource_update/ add feature flag to registry
baldm0mma Apr 14, 2023
42035bf
baldm0mma/sql_datasource_update/ adjust table and dataset dropdown logic
baldm0mma Apr 14, 2023
5982fa9
baldm0mma/sql_datasource_update/ update testing to deal with feature …
baldm0mma Apr 18, 2023
5d6fa63
baldm0mma/sql_datasource_update/ update wioth cascadeDisable
baldm0mma Apr 18, 2023
cbd3dbc
baldm0mma/sql_datasource_update/ update naming
baldm0mma Apr 19, 2023
16fcc58
baldm0mma/sql_datasource_update/ update tests to reflect enabled feat…
baldm0mma Apr 20, 2023
91261aa
baldm0mma/sql_datasource_update/ update annotations
baldm0mma Apr 20, 2023
23c9613
baldm0mma/sql_datasource_update/ update annos in queryEd
baldm0mma Apr 20, 2023
fd6d26e
baldm0mma/sql_datasource_update/ update test names
baldm0mma Apr 21, 2023
58a9c64
baldm0mma/sql_datasource_update/ update anno issues
baldm0mma Apr 24, 2023
9d25155
baldm0mma/slq_datasource_update/ add query to sqlDatasource with error
baldm0mma Apr 25, 2023
ff68fbd
baldm0mma/sql_datasource_update/ update docs language
baldm0mma Apr 25, 2023
a9a7067
baldm0mma/sql_datasource_update/ remove notes to self
baldm0mma Apr 25, 2023
500142a
baldm0mma/sql_datasource_update/ add QueryEditorFeatureFlag.utils.ts
baldm0mma Apr 26, 2023
32ce8ab
baldm0mma/sql_datasource_update/ update database into json
baldm0mma May 3, 2023
fd41776
baldm0mma/sql_datasource_update/ found file
baldm0mma May 8, 2023
ef3a5bf
Update docs/sources/datasources/mssql/query-editor/index.md
baldm0mma May 8, 2023
ef90b21
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 8, 2023
6569fe8
baldm0mma/sql_datasource_update/ update feature flag and toggles
baldm0mma May 9, 2023
bad3655
baldm0mma/sql_datasource_update/ add hasConfigIssue, update annos, re…
baldm0mma May 9, 2023
e0a712f
baldm0mma/sql_datasource_update/ update warning language
baldm0mma May 9, 2023
7d83b3b
baldm0mma/sql_datasource_update/ update button content
baldm0mma May 9, 2023
d429a39
baldm0mma/sql_datasource_update/ update jsonData logic in frontendset…
baldm0mma May 9, 2023
66f49e0
baldm0mma/sql_datasource_update/ update jsonData logic
baldm0mma May 9, 2023
a25f873
baldm0mma/sql_datasource_update/ update annos in frontendsettings.go
baldm0mma May 9, 2023
415c4fd
baldm0mma/sql_datasource_update/ update sql editor docs
baldm0mma May 9, 2023
b5523a0
baldm0mma/sql_datasource_update/ update mysql docs
baldm0mma May 9, 2023
d9758c6
baldm0mma/sql_datasource_update/ update postgres docs
baldm0mma May 9, 2023
2488e89
baldm0mma/sql_datasource_update/ remove unused code in datasetSelecto…
baldm0mma May 9, 2023
5845828
baldm0mma/sql_datasource_update/ update syntax conventions
baldm0mma May 9, 2023
89926ce
baldm0mma/sql_datasource_update/ add logs
baldm0mma May 9, 2023
44f8a04
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 9, 2023
86d6d96
baldm0mma/sql_datasource_update/ remove unused code
baldm0mma May 9, 2023
d5e1569
baldm0mma/sql_datasource_update/ remove conlogs
baldm0mma May 9, 2023
49a13ea
baldm0mma/sql_datasource_update/ update tests
baldm0mma May 9, 2023
d3fd153
baldm0mma/sql_datasource_update/ add second error to query
baldm0mma May 9, 2023
48504e4
baldm0mma/sql_datasource_update/ run make gen-cue
baldm0mma May 10, 2023
0e14411
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 10, 2023
155b6ac
baldm0mma/sql_database_update/ lint registry
baldm0mma May 10, 2023
ddcc00b
baldm0mma/sql_datasource_update/ update registry
baldm0mma May 11, 2023
4d6ca72
baldm0mma/sql_datasource_update/ upate datasource logic
baldm0mma May 11, 2023
58f82cd
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 11, 2023
f7c22ec
baldm0mma/sql_datasource_update/ add logs
baldm0mma May 11, 2023
f4f3531
baldm0mma/sql_datasource_update/ add comms to self
baldm0mma May 11, 2023
abe2198
baldm0mma/sql_datasource_update/ comment out false pos tests, and add…
baldm0mma May 11, 2023
c30cb5a
baldm0mma/sql_database_update/ update query error to only test for co…
baldm0mma May 12, 2023
6f46372
baldm0mma/sql_datasource_update/ update annos in frontendsettings.go
baldm0mma May 15, 2023
72e50ce
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 15, 2023
80470d9
baldm0mma/sql_datasource_update/ update error logic to datasource
baldm0mma May 16, 2023
fc7b36d
baldm0mma/sql_datasource_update/ remove alerts from query editor
baldm0mma May 17, 2023
99bf5fd
baldm0mma/sql_datasource_update/ remove unused imports
baldm0mma May 17, 2023
e564932
baldm0mma/sql_datasource_update/ update tests
baldm0mma May 17, 2023
d458bff
baldm0mma/sql_datasource_update/ remove comments
baldm0mma May 17, 2023
89bdaeb
baldm0mma/sql_datasource_update/ remove logs in queryGroup.tsx
baldm0mma May 17, 2023
4479201
baldm0mma/sql_datasource_update/ remove outdated annotation in datase…
baldm0mma May 24, 2023
2fea108
baldm0mma/sql_datasource_update/ remove superfluous test and update t…
baldm0mma May 24, 2023
b47b087
baldm0mma/sql_datasource_update/ remove feature flag
baldm0mma May 24, 2023
1f9e1a2
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 24, 2023
96570eb
baldm0mma/sql_datasource_update/ add back feature flag
baldm0mma May 24, 2023
2ddaf92
baldm0mma/sql_datasource_update/ update to enums
baldm0mma May 26, 2023
ae61741
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 26, 2023
8ebc086
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 26, 2023
65332d2
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma May 30, 2023
dd83ff3
baldm0mma/sql_datasource_update/ update panel caps
baldm0mma May 30, 2023
cba5859
baldm0mma/sql_datasource_update/ update dataset selector to default u…
baldm0mma May 30, 2023
23485a6
baldm0mma/sql_datasource_update/ move onChange into conditional
baldm0mma May 30, 2023
4f11a90
baldm0mma/sql_datasource_update/ add logic for previous datasets choice
baldm0mma May 30, 2023
63a03c9
baldm0mma/sql_datasource_update/ add back previous logic for assignin…
baldm0mma May 30, 2023
d344236
baldm0mma/sql_datasource_update/ update useEffect dep array
baldm0mma May 30, 2023
f314305
baldm0mma/sql_datasource_update/ remove feature toggle
baldm0mma Jun 6, 2023
7e87a35
Merge branch 'main' into baldm0mma/sql_datasource_update
baldm0mma Jun 6, 2023
26ee0f6
baldm0mma/sql_datasource_update/ add feature toggle
baldm0mma Jun 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/sources/datasources/mssql/query-editor/index.md
Expand Up @@ -87,12 +87,12 @@ Code mode supports autocompletion of tables, columns, SQL keywords, standard SQL

In **Builder mode**, you can build queries using a visual interface.

### Select a dataset and table
### Dataset and table selection

In the **Dataset** dropdown, select the MS SQL database to query.
In the **Dataset** dropdown, select the MSSQL database to query. Grafana populates the dropdown with all databases that the user can access.
Once you select a database, Grafana populates the dropdown with all available tables.

Grafana populates the dropdown with the databases that the configured user can access.
When you select a dataset, Grafana populates the **Table** dropdown with available tables.
**Note:** If a default database has been configured through the Data Source Configuration page (or through a provisioning configuration file), the user will only be able to use that single preconfigured database for querying.

### Select columns and aggregation functions (SELECT)

Expand Down
2 changes: 2 additions & 0 deletions docs/sources/datasources/mysql/_index.md
Expand Up @@ -186,6 +186,8 @@ If your table or database name contains a reserved word or a [not permitted char
In the dataset dropdown, choose the MySQL database to query. The dropdown is be populated with the databases that the user has access to.
When the dataset is selected, the table dropdown is populated with the tables that are available.

**Note:** If a default database has been configured through the Data Source Configuration page (or through a provisioning configuration file), the user will only be able to use that single preconfigured database for querying.

### Columns and Aggregation functions (SELECT)

Using the dropdown, select a column to include in the data. You can also specify an optional aggregation function.
Expand Down
8 changes: 4 additions & 4 deletions docs/sources/datasources/postgres/_index.md
Expand Up @@ -88,18 +88,18 @@ Make sure the user does not get any unwanted privileges from the public role.

## Query builder

{{< figure src="/static/img/docs/v92/postgresql_query_builder.png" class="docs-image--no-shadow" caption="PostgreSQL query builder" >}}
{{< figure src="/static/img/docs/screenshot-postgres-query-editor.png" class="docs-image--no-shadow" caption="PostgreSQL query builder" >}}

The PostgreSQL query builder is available when editing a panel using a PostgreSQL data source. The built query can be run by pressing the `Run query` button in the top right corner of the editor.

### Format

The response from PostgreSQL can be formatted as either a table or as a time series. To use the time series format one of the columns must be named `time`.

### Dataset and Table selection
### Dataset and table selection

In the dataset dropdown, choose the PostgreSQL database to query. The dropdown is be populated with the databases that the user has access to.
When the dataset is selected, the table dropdown is populated with the tables that are available.
The dataset dropdown will be populated with the configured database to which the user has access.
The table dropdown is populated with the tables that are available within that database.

### Columns and Aggregation functions (SELECT)

Expand Down
Expand Up @@ -65,6 +65,7 @@ Some stable features are enabled by default. You can disable a stable feature by
| `faroDatasourceSelector` | Enable the data source selector within the Frontend Apps section of the Frontend Observability |
| `enableDatagridEditing` | Enables the edit functionality in the datagrid panel |
| `dataSourcePageHeader` | Apply new pageHeader UI in data source edit page |
| `sqlDatasourceDatabaseSelection` | Enables previous SQL data source dataset dropdown behavior |

## Alpha feature toggles

Expand Down
1 change: 1 addition & 0 deletions packages/grafana-data/src/types/featureToggles.gen.ts
Expand Up @@ -98,4 +98,5 @@ export interface FeatureToggles {
enableDatagridEditing?: boolean;
dataSourcePageHeader?: boolean;
extraThemes?: boolean;
sqlDatasourceDatabaseSelection?: boolean;
}
10 changes: 10 additions & 0 deletions pkg/api/frontendsettings.go
Expand Up @@ -372,6 +372,16 @@ func (hs *HTTPServer) getFSDataSources(c *contextmodel.ReqContext, availablePlug
}
}

// Update `jsonData.database` for outdated provisioned SQL datasources created WITHOUT the `jsonData` object in their configuration.
// In these cases, the `Database` value is defined (if at all) on the root level of the provisioning config object.
// This is done for easier warning/error checking on the front end.
if (ds.Type == datasources.DS_MSSQL) || (ds.Type == datasources.DS_MYSQL) || (ds.Type == datasources.DS_POSTGRES) {
baldm0mma marked this conversation as resolved.
Show resolved Hide resolved
// Only update if the value isn't already assigned.
if dsDTO.JSONData["database"] == nil || dsDTO.JSONData["database"] == "" {
dsDTO.JSONData["database"] = ds.Database
}
}

if (ds.Type == datasources.DS_INFLUXDB) || (ds.Type == datasources.DS_ES) {
dsDTO.Database = ds.Database
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/services/featuremgmt/registry.go
Expand Up @@ -541,5 +541,12 @@ var (
State: FeatureStateAlpha,
Owner: grafanaUserEssentialsSquad,
},
{
Name: "sqlDatasourceDatabaseSelection",
Description: "Enables previous SQL data source dataset dropdown behavior",
FrontendOnly: true,
State: FeatureStateBeta,
Owner: grafanaBiSquad,
},
}
)
1 change: 1 addition & 0 deletions pkg/services/featuremgmt/toggles_gen.csv
Expand Up @@ -79,3 +79,4 @@ faroDatasourceSelector,beta,@grafana/app-o11y,false,false,false,true
enableDatagridEditing,beta,@grafana/grafana-bi-squad,false,false,false,true
dataSourcePageHeader,beta,@grafana/enterprise-datasources,false,false,false,true
extraThemes,alpha,@grafana/user-essentials,false,false,false,true
sqlDatasourceDatabaseSelection,beta,@grafana/grafana-bi-squad,false,false,false,true
4 changes: 4 additions & 0 deletions pkg/services/featuremgmt/toggles_gen.go
Expand Up @@ -326,4 +326,8 @@ const (
// FlagExtraThemes
// Enables extra themes
FlagExtraThemes = "extraThemes"

// FlagSqlDatasourceDatabaseSelection
// Enables previous SQL data source dataset dropdown behavior
FlagSqlDatasourceDatabaseSelection = "sqlDatasourceDatabaseSelection"
)
73 changes: 52 additions & 21 deletions public/app/features/plugins/sql/components/DatasetSelector.tsx
Expand Up @@ -6,47 +6,78 @@ import { Select } from '@grafana/ui';

import { DB, ResourceSelectorProps, toOption } from '../types';

interface DatasetSelectorProps extends ResourceSelectorProps {
import { isSqlDatasourceDatabaseSelectionFeatureFlagEnabled } from './QueryEditorFeatureFlag.utils';

export interface DatasetSelectorProps extends ResourceSelectorProps {
db: DB;
value: string | null;
applyDefault?: boolean;
disabled?: boolean;
dataset: string | undefined;
preconfiguredDataset: string;
isPostgresInstance: boolean | undefined;
onChange: (v: SelectableValue) => void;
}

export const DatasetSelector = ({ db, value, onChange, disabled, className, applyDefault }: DatasetSelectorProps) => {
export const DatasetSelector = ({
dataset,
db,
isPostgresInstance,
onChange,
preconfiguredDataset,
}: DatasetSelectorProps) => {
/*
The behavior of this component - for MSSQL and MySQL datasources - is based on whether the user chose to create a datasource
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work with the explanations!

with or without a default database (preconfiguredDataset). If the user configured a default database, this selector
should only allow that single preconfigured database option to be selected. If the user chose to NOT assign/configure a default database,
then the user should be able to use this component to choose between multiple databases available to the datasource.
*/
// `hasPreconfigCondition` is true if either 1) the sql datasource has a preconfigured default database,
// OR if 2) the datasource is Postgres. In either case the only option available to the user is the preconfigured database.
const hasPreconfigCondition = !!preconfiguredDataset || isPostgresInstance;

const state = useAsync(async () => {
if (isSqlDatasourceDatabaseSelectionFeatureFlagEnabled()) {
// If a default database is already configured for a MSSQL or MySQL data source, OR the data source is Postgres, no need to fetch other databases.
if (hasPreconfigCondition) {
// Set the current database to the preconfigured database.
onChange(toOption(preconfiguredDataset));
return [toOption(preconfiguredDataset)];
}
}

// If there is no preconfigured database, but there is a selected dataset, set the current database to the selected dataset.
if (dataset) {
onChange(toOption(dataset));
}

// Otherwise, fetch all databases available to the datasource.
const datasets = await db.datasets();
return datasets.map(toOption);
}, []);

useEffect(() => {
if (!applyDefault) {
return;
}
// Set default dataset when values are fetched
if (!value) {
if (state.value && state.value[0]) {
onChange(state.value[0]);
}
} else {
if (state.value && state.value.find((v) => v.value === value) === undefined) {
// if value is set and newly fetched values does not contain selected value
if (state.value.length > 0) {
if (!isSqlDatasourceDatabaseSelectionFeatureFlagEnabled()) {
// Set default dataset when values are fetched
if (!dataset) {
if (state.value && state.value[0]) {
onChange(state.value[0]);
}
} else {
if (state.value && state.value.find((v) => v.value === dataset) === undefined) {
// if value is set and newly fetched values does not contain selected value
if (state.value.length > 0) {
onChange(state.value[0]);
}
}
}
}
}, [state.value, value, applyDefault, onChange]);
}, [state.value, onChange, dataset]);

return (
<Select
className={className}
aria-label="Dataset selector"
value={value}
value={dataset}
options={state.value}
onChange={onChange}
disabled={disabled}
disabled={state.loading}
isLoading={state.loading}
menuShouldPortal={true}
/>
Expand Down
19 changes: 15 additions & 4 deletions public/app/features/plugins/sql/components/QueryEditor.tsx
Expand Up @@ -13,13 +13,23 @@ import { QueryHeader, QueryHeaderProps } from './QueryHeader';
import { RawEditor } from './query-editor-raw/RawEditor';
import { VisualEditor } from './visual-query-builder/VisualEditor';

interface Props extends QueryEditorProps<SqlDatasource, SQLQuery, SQLOptions> {
queryHeaderProps?: Pick<QueryHeaderProps, 'isDatasetSelectorHidden'>;
interface SqlQueryEditorProps extends QueryEditorProps<SqlDatasource, SQLQuery, SQLOptions> {
queryHeaderProps?: Pick<QueryHeaderProps, 'isPostgresInstance'>;
}

export function SqlQueryEditor({ datasource, query, onChange, onRunQuery, range, queryHeaderProps }: Props) {
export function SqlQueryEditor({
datasource,
query,
onChange,
onRunQuery,
range,
queryHeaderProps,
}: SqlQueryEditorProps) {
const [isQueryRunnable, setIsQueryRunnable] = useState(true);
const db = datasource.getDB();

const { preconfiguredDatabase } = datasource;
const isPostgresInstance = !!queryHeaderProps?.isPostgresInstance;
const { loading, error } = useAsync(async () => {
return () => {
if (datasource.getDB(datasource.id).init !== undefined) {
Expand Down Expand Up @@ -80,13 +90,14 @@ export function SqlQueryEditor({ datasource, query, onChange, onRunQuery, range,
<>
<QueryHeader
db={db}
preconfiguredDataset={preconfiguredDatabase}
onChange={onQueryHeaderChange}
onRunQuery={onRunQuery}
onQueryRowChange={setQueryRowFilter}
queryRowFilter={queryRowFilter}
query={queryWithDefaults}
isQueryRunnable={isQueryRunnable}
{...queryHeaderProps}
isPostgresInstance={isPostgresInstance}
/>

<Space v={0.5} />
Expand Down
@@ -0,0 +1,5 @@
import { config } from '@grafana/runtime';

export const isSqlDatasourceDatabaseSelectionFeatureFlagEnabled = () => {
return !!config.featureToggles.sqlDatasourceDatabaseSelection;
};
45 changes: 29 additions & 16 deletions public/app/features/plugins/sql/components/QueryHeader.tsx
Expand Up @@ -10,17 +10,19 @@ import { SQLQuery, QueryFormat, QueryRowFilter, QUERY_FORMAT_OPTIONS, DB } from

import { ConfirmModal } from './ConfirmModal';
import { DatasetSelector } from './DatasetSelector';
import { isSqlDatasourceDatabaseSelectionFeatureFlagEnabled } from './QueryEditorFeatureFlag.utils';
import { TableSelector } from './TableSelector';

export interface QueryHeaderProps {
db: DB;
query: QueryWithDefaults;
isPostgresInstance?: boolean;
isQueryRunnable: boolean;
onChange: (query: SQLQuery) => void;
onRunQuery: () => void;
onQueryRowChange: (queryRowFilter: QueryRowFilter) => void;
onRunQuery: () => void;
preconfiguredDataset: string;
query: QueryWithDefaults;
queryRowFilter: QueryRowFilter;
isQueryRunnable: boolean;
isDatasetSelectorHidden?: boolean;
}

const editorModes = [
Expand All @@ -30,13 +32,14 @@ const editorModes = [

export function QueryHeader({
db,
query,
queryRowFilter,
isPostgresInstance,
isQueryRunnable,
onChange,
onRunQuery,
onQueryRowChange,
isQueryRunnable,
isDatasetSelectorHidden,
onRunQuery,
preconfiguredDataset,
query,
queryRowFilter,
}: QueryHeaderProps) {
const { editorMode } = query;
const [_, copyToClipboard] = useCopyToClipboard();
Expand Down Expand Up @@ -86,9 +89,20 @@ export function QueryHeader({
sql: undefined,
rawSql: '',
};

onChange(next);
};

const datasetDropdownIsAvailable = () => {
// If the feature flag is DISABLED, && the datasource is Postgres (`isPostgresInstance`),
// we want to hide the dropdown - as per previous behavior.
if (!isSqlDatasourceDatabaseSelectionFeatureFlagEnabled() && isPostgresInstance) {
return false;
}

return true;
};

return (
<>
<EditorHeader>
Expand Down Expand Up @@ -205,24 +219,23 @@ export function QueryHeader({
<>
<Space v={0.5} />
<EditorRow>
{isDatasetSelectorHidden ? null : (
{datasetDropdownIsAvailable() && (
<EditorField label="Dataset" width={25}>
<DatasetSelector
db={db}
value={query.dataset === undefined ? null : query.dataset}
dataset={query.dataset}
isPostgresInstance={isPostgresInstance}
preconfiguredDataset={preconfiguredDataset}
onChange={onDatasetChange}
/>
</EditorField>
)}

<EditorField label="Table" width={25}>
<TableSelector
db={db}
query={query}
value={query.table === undefined ? null : query.table}
dataset={query.dataset || preconfiguredDataset}
table={query.table}
onChange={onTableChange}
forceFetch={isDatasetSelectorHidden}
applyDefault
/>
</EditorField>
</EditorRow>
Expand Down