SQL Datasources: Move database setting to jsonData (#58649)

* Datasource settings: Add deprecation notice for database field

* SQL Datasources: Migrate from settings.database to settings.jsonData.database

* Check jsonData first

* Remove comment from docs
This commit is contained in:
Zoltán Bedi 2022-12-13 09:56:52 +01:00 committed by GitHub
parent ac19b920f1
commit d65899de7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 79 additions and 20 deletions

View File

@ -160,33 +160,36 @@ datasources:
- name: gdev-mysql
type: mysql
url: localhost:3306
database: grafana
user: grafana
jsonData:
database: grafana
secureJsonData:
password: password
- name: gdev-mysql-ds-tests
type: mysql
url: localhost:3306
database: grafana_ds_tests
user: grafana
jsonData:
database: grafana_ds_tests
secureJsonData:
password: password
- name: gdev-mssql
type: mssql
url: localhost:1433
database: grafana
user: grafana
jsonData:
database: grafana
secureJsonData:
password: Password!
- name: gdev-mssql-tls
type: mssql
url: localhost:1434
database: grafana
user: grafana
jsonData:
database: grafana
encrypt: "true"
tlsSkipVerify: true
secureJsonData:
@ -195,30 +198,31 @@ datasources:
- name: gdev-mssql-ds-tests
type: mssql
url: localhost:1433
database: grafanatest
user: grafana
jsonData:
database: grafanatest
secureJsonData:
password: Password!
- name: gdev-postgres
type: postgres
url: localhost:5432
database: grafana
user: grafana
secureJsonData:
password: password
jsonData:
sslmode: "disable"
database: grafana
- name: gdev-postgres-ds-tests
type: postgres
url: localhost:5432
database: grafanadstest
user: grafanatest
secureJsonData:
password: grafanatest
jsonData:
sslmode: "disable"
database: grafanadstest
- name: gdev-cloudwatch
type: cloudwatch

View File

@ -116,9 +116,9 @@ datasources:
- name: MSSQL
type: mssql
url: localhost:1433
database: grafana
user: grafana
jsonData:
database: grafana
maxOpenConns: 0 # Grafana v5.4+
maxIdleConns: 2 # Grafana v5.4+
connMaxLifetime: 14400 # Grafana v5.4+

View File

@ -91,9 +91,9 @@ datasources:
- name: MySQL
type: mysql
url: localhost:3306
database: grafana
user: grafana
jsonData:
database: grafana
maxOpenConns: 0 # Grafana v5.4+
maxIdleConns: 2 # Grafana v5.4+
connMaxLifetime: 14400 # Grafana v5.4+

View File

@ -125,11 +125,11 @@ datasources:
- name: Postgres
type: postgres
url: localhost:5432
database: grafana
user: grafana
secureJsonData:
password: 'Password!'
jsonData:
database: grafana
sslmode: 'disable' # disable/require/verify-ca/verify-full
maxOpenConns: 0 # Grafana v5.4+
maxIdleConns: 2 # Grafana v5.4+

View File

@ -333,6 +333,8 @@ func (ss *SqlStore) UpdateDataSource(ctx context.Context, cmd *datasources.Updat
sess.UseBool("basic_auth")
sess.UseBool("with_credentials")
sess.UseBool("read_only")
// Make sure database field is zeroed out if empty. We want to migrate away from this field.
sess.MustCols("database")
// Make sure password are zeroed out if empty. We do this as we want to migrate passwords from
// plain text fields to SecureJsonData.
sess.MustCols("password")

View File

@ -66,11 +66,17 @@ func newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFactoryFunc {
if err != nil {
return nil, fmt.Errorf("error reading settings: %w", err)
}
database := jsonData.Database
if database == "" {
database = settings.Database
}
dsInfo := sqleng.DataSourceInfo{
JsonData: jsonData,
URL: settings.URL,
User: settings.User,
Database: settings.Database,
Database: database,
ID: settings.ID,
Updated: settings.Updated,
UID: settings.UID,

View File

@ -60,11 +60,17 @@ func newInstanceSettings(cfg *setting.Cfg, httpClientProvider httpclient.Provide
if err != nil {
return nil, fmt.Errorf("error reading settings: %w", err)
}
database := jsonData.Database
if database == "" {
database = settings.Database
}
dsInfo := sqleng.DataSourceInfo{
JsonData: jsonData,
URL: settings.URL,
User: settings.User,
Database: settings.Database,
Database: database,
ID: settings.ID,
Updated: settings.Updated,
UID: settings.UID,

View File

@ -66,11 +66,17 @@ func (s *Service) newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFacto
if err != nil {
return nil, fmt.Errorf("error reading settings: %w", err)
}
database := jsonData.Database
if database == "" {
database = settings.Database
}
dsInfo := sqleng.DataSourceInfo{
JsonData: jsonData,
URL: settings.URL,
User: settings.User,
Database: settings.Database,
Database: database,
ID: settings.ID,
Updated: settings.Updated,
UID: settings.UID,

View File

@ -66,6 +66,7 @@ type JsonData struct {
Encrypt string `json:"encrypt"`
Servername string `json:"servername"`
TimeInterval string `json:"timeInterval"`
Database string `json:"database"`
}
type DataSourceInfo struct {

View File

@ -0,0 +1,25 @@
import { useEffect } from 'react';
import { DataSourceJsonData, DataSourcePluginOptionsEditorProps } from '@grafana/data';
import { logDebug } from '@grafana/runtime';
import { SQLOptions } from '../../types';
/**
* Moves the database field from the options object to jsonData.database and empties the database field.
*/
export function useMigrateDatabaseField<T extends DataSourceJsonData = SQLOptions, S = {}>({
onOptionsChange,
options,
}: DataSourcePluginOptionsEditorProps<T, S>) {
useEffect(() => {
if (options.database) {
logDebug(`Migrating from options.database with value ${options.database} for ${options.name}`);
onOptionsChange({
...options,
database: '',
jsonData: { ...options.jsonData, database: options.database },
});
}
}, [onOptionsChange, options]);
}

View File

@ -24,6 +24,7 @@ import {
} from '@grafana/ui';
import { NumberInput } from 'app/core/components/OptionsUI/NumberInput';
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
import { useMigrateDatabaseField } from 'app/features/plugins/sql/components/configuration/useMigrateDatabaseField';
import { MSSQLAuthenticationType, MSSQLEncryptOptions, MssqlOptions } from '../types';
@ -32,6 +33,8 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
const styles = useStyles2(getStyles);
const jsonData = options.jsonData;
useMigrateDatabaseField(props);
const onResetPassword = () => {
updateDatasourcePluginResetOption(props, 'password');
};
@ -99,9 +102,9 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
<Input
width={longWidth}
name="database"
value={options.database || ''}
value={jsonData.database || ''}
placeholder="database name"
onChange={onDSOptionChanged('database')}
onChange={onUpdateDatasourceJsonDataOption(props, 'database')}
></Input>
</InlineField>
<InlineField

View File

@ -10,6 +10,7 @@ import {
import { Alert, FieldSet, InlineField, InlineFieldRow, InlineSwitch, Input, Link, SecretInput } from '@grafana/ui';
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
import { TLSSecretsConfig } from 'app/features/plugins/sql/components/configuration/TLSSecretsConfig';
import { useMigrateDatabaseField } from 'app/features/plugins/sql/components/configuration/useMigrateDatabaseField';
import { MySQLOptions } from '../types';
@ -17,6 +18,8 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<My
const { options, onOptionsChange } = props;
const jsonData = options.jsonData;
useMigrateDatabaseField(props);
const onResetPassword = () => {
updateDatasourcePluginResetOption(props, 'password');
};
@ -54,9 +57,9 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<My
<Input
width={longWidth}
name="database"
value={options.database || ''}
value={jsonData.database || ''}
placeholder="database name"
onChange={onDSOptionChanged('database')}
onChange={onUpdateDatasourceJsonDataOption(props, 'database')}
></Input>
</InlineField>
<InlineFieldRow>

View File

@ -21,6 +21,7 @@ import {
} from '@grafana/ui';
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
import { TLSSecretsConfig } from 'app/features/plugins/sql/components/configuration/TLSSecretsConfig';
import { useMigrateDatabaseField } from 'app/features/plugins/sql/components/configuration/useMigrateDatabaseField';
import { PostgresOptions, PostgresTLSMethods, PostgresTLSModes, SecureJsonData } from '../types';
@ -47,6 +48,8 @@ export const PostgresConfigEditor = (props: DataSourcePluginOptionsEditorProps<P
useAutoDetectFeatures({ props, setVersionOptions });
useMigrateDatabaseField(props);
const { options, onOptionsChange } = props;
const jsonData = options.jsonData;
@ -103,9 +106,9 @@ export const PostgresConfigEditor = (props: DataSourcePluginOptionsEditorProps<P
<Input
width={40}
name="database"
value={options.database || ''}
value={jsonData.database || ''}
placeholder="database name"
onChange={onDSOptionChanged('database')}
onChange={onUpdateDatasourceJsonDataOption(props, 'database')}
></Input>
</InlineField>
<InlineFieldRow>

View File

@ -76,7 +76,7 @@ export function useAutoDetectFeatures({ props, setVersionOptions }: Options) {
function isValidConfig(options: DataSourceSettings<PostgresOptions, SecureJsonData>) {
return (
options.url &&
options.database &&
options.jsonData.database &&
options.user &&
(options.secureJsonData?.password || options.secureJsonFields?.password) &&
(options.jsonData.sslmode === PostgresTLSModes.disable ||