mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 17:22:09 +08:00
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:
@ -160,33 +160,36 @@ datasources:
|
|||||||
- name: gdev-mysql
|
- name: gdev-mysql
|
||||||
type: mysql
|
type: mysql
|
||||||
url: localhost:3306
|
url: localhost:3306
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
|
jsonData:
|
||||||
|
database: grafana
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: password
|
password: password
|
||||||
|
|
||||||
- name: gdev-mysql-ds-tests
|
- name: gdev-mysql-ds-tests
|
||||||
type: mysql
|
type: mysql
|
||||||
url: localhost:3306
|
url: localhost:3306
|
||||||
database: grafana_ds_tests
|
|
||||||
user: grafana
|
user: grafana
|
||||||
|
jsonData:
|
||||||
|
database: grafana_ds_tests
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: password
|
password: password
|
||||||
|
|
||||||
- name: gdev-mssql
|
- name: gdev-mssql
|
||||||
type: mssql
|
type: mssql
|
||||||
url: localhost:1433
|
url: localhost:1433
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
|
jsonData:
|
||||||
|
database: grafana
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: Password!
|
password: Password!
|
||||||
|
|
||||||
- name: gdev-mssql-tls
|
- name: gdev-mssql-tls
|
||||||
type: mssql
|
type: mssql
|
||||||
url: localhost:1434
|
url: localhost:1434
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
jsonData:
|
jsonData:
|
||||||
|
database: grafana
|
||||||
encrypt: "true"
|
encrypt: "true"
|
||||||
tlsSkipVerify: true
|
tlsSkipVerify: true
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
@ -195,30 +198,31 @@ datasources:
|
|||||||
- name: gdev-mssql-ds-tests
|
- name: gdev-mssql-ds-tests
|
||||||
type: mssql
|
type: mssql
|
||||||
url: localhost:1433
|
url: localhost:1433
|
||||||
database: grafanatest
|
|
||||||
user: grafana
|
user: grafana
|
||||||
|
jsonData:
|
||||||
|
database: grafanatest
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: Password!
|
password: Password!
|
||||||
|
|
||||||
- name: gdev-postgres
|
- name: gdev-postgres
|
||||||
type: postgres
|
type: postgres
|
||||||
url: localhost:5432
|
url: localhost:5432
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: password
|
password: password
|
||||||
jsonData:
|
jsonData:
|
||||||
sslmode: "disable"
|
sslmode: "disable"
|
||||||
|
database: grafana
|
||||||
|
|
||||||
- name: gdev-postgres-ds-tests
|
- name: gdev-postgres-ds-tests
|
||||||
type: postgres
|
type: postgres
|
||||||
url: localhost:5432
|
url: localhost:5432
|
||||||
database: grafanadstest
|
|
||||||
user: grafanatest
|
user: grafanatest
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: grafanatest
|
password: grafanatest
|
||||||
jsonData:
|
jsonData:
|
||||||
sslmode: "disable"
|
sslmode: "disable"
|
||||||
|
database: grafanadstest
|
||||||
|
|
||||||
- name: gdev-cloudwatch
|
- name: gdev-cloudwatch
|
||||||
type: cloudwatch
|
type: cloudwatch
|
||||||
|
@ -116,9 +116,9 @@ datasources:
|
|||||||
- name: MSSQL
|
- name: MSSQL
|
||||||
type: mssql
|
type: mssql
|
||||||
url: localhost:1433
|
url: localhost:1433
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
jsonData:
|
jsonData:
|
||||||
|
database: grafana
|
||||||
maxOpenConns: 0 # Grafana v5.4+
|
maxOpenConns: 0 # Grafana v5.4+
|
||||||
maxIdleConns: 2 # Grafana v5.4+
|
maxIdleConns: 2 # Grafana v5.4+
|
||||||
connMaxLifetime: 14400 # Grafana v5.4+
|
connMaxLifetime: 14400 # Grafana v5.4+
|
||||||
|
@ -91,9 +91,9 @@ datasources:
|
|||||||
- name: MySQL
|
- name: MySQL
|
||||||
type: mysql
|
type: mysql
|
||||||
url: localhost:3306
|
url: localhost:3306
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
jsonData:
|
jsonData:
|
||||||
|
database: grafana
|
||||||
maxOpenConns: 0 # Grafana v5.4+
|
maxOpenConns: 0 # Grafana v5.4+
|
||||||
maxIdleConns: 2 # Grafana v5.4+
|
maxIdleConns: 2 # Grafana v5.4+
|
||||||
connMaxLifetime: 14400 # Grafana v5.4+
|
connMaxLifetime: 14400 # Grafana v5.4+
|
||||||
|
@ -125,11 +125,11 @@ datasources:
|
|||||||
- name: Postgres
|
- name: Postgres
|
||||||
type: postgres
|
type: postgres
|
||||||
url: localhost:5432
|
url: localhost:5432
|
||||||
database: grafana
|
|
||||||
user: grafana
|
user: grafana
|
||||||
secureJsonData:
|
secureJsonData:
|
||||||
password: 'Password!'
|
password: 'Password!'
|
||||||
jsonData:
|
jsonData:
|
||||||
|
database: grafana
|
||||||
sslmode: 'disable' # disable/require/verify-ca/verify-full
|
sslmode: 'disable' # disable/require/verify-ca/verify-full
|
||||||
maxOpenConns: 0 # Grafana v5.4+
|
maxOpenConns: 0 # Grafana v5.4+
|
||||||
maxIdleConns: 2 # Grafana v5.4+
|
maxIdleConns: 2 # Grafana v5.4+
|
||||||
|
@ -333,6 +333,8 @@ func (ss *SqlStore) UpdateDataSource(ctx context.Context, cmd *datasources.Updat
|
|||||||
sess.UseBool("basic_auth")
|
sess.UseBool("basic_auth")
|
||||||
sess.UseBool("with_credentials")
|
sess.UseBool("with_credentials")
|
||||||
sess.UseBool("read_only")
|
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
|
// Make sure password are zeroed out if empty. We do this as we want to migrate passwords from
|
||||||
// plain text fields to SecureJsonData.
|
// plain text fields to SecureJsonData.
|
||||||
sess.MustCols("password")
|
sess.MustCols("password")
|
||||||
|
@ -66,11 +66,17 @@ func newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFactoryFunc {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
database := jsonData.Database
|
||||||
|
if database == "" {
|
||||||
|
database = settings.Database
|
||||||
|
}
|
||||||
|
|
||||||
dsInfo := sqleng.DataSourceInfo{
|
dsInfo := sqleng.DataSourceInfo{
|
||||||
JsonData: jsonData,
|
JsonData: jsonData,
|
||||||
URL: settings.URL,
|
URL: settings.URL,
|
||||||
User: settings.User,
|
User: settings.User,
|
||||||
Database: settings.Database,
|
Database: database,
|
||||||
ID: settings.ID,
|
ID: settings.ID,
|
||||||
Updated: settings.Updated,
|
Updated: settings.Updated,
|
||||||
UID: settings.UID,
|
UID: settings.UID,
|
||||||
|
@ -60,11 +60,17 @@ func newInstanceSettings(cfg *setting.Cfg, httpClientProvider httpclient.Provide
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
database := jsonData.Database
|
||||||
|
if database == "" {
|
||||||
|
database = settings.Database
|
||||||
|
}
|
||||||
|
|
||||||
dsInfo := sqleng.DataSourceInfo{
|
dsInfo := sqleng.DataSourceInfo{
|
||||||
JsonData: jsonData,
|
JsonData: jsonData,
|
||||||
URL: settings.URL,
|
URL: settings.URL,
|
||||||
User: settings.User,
|
User: settings.User,
|
||||||
Database: settings.Database,
|
Database: database,
|
||||||
ID: settings.ID,
|
ID: settings.ID,
|
||||||
Updated: settings.Updated,
|
Updated: settings.Updated,
|
||||||
UID: settings.UID,
|
UID: settings.UID,
|
||||||
|
@ -66,11 +66,17 @@ func (s *Service) newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFacto
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
database := jsonData.Database
|
||||||
|
if database == "" {
|
||||||
|
database = settings.Database
|
||||||
|
}
|
||||||
|
|
||||||
dsInfo := sqleng.DataSourceInfo{
|
dsInfo := sqleng.DataSourceInfo{
|
||||||
JsonData: jsonData,
|
JsonData: jsonData,
|
||||||
URL: settings.URL,
|
URL: settings.URL,
|
||||||
User: settings.User,
|
User: settings.User,
|
||||||
Database: settings.Database,
|
Database: database,
|
||||||
ID: settings.ID,
|
ID: settings.ID,
|
||||||
Updated: settings.Updated,
|
Updated: settings.Updated,
|
||||||
UID: settings.UID,
|
UID: settings.UID,
|
||||||
|
@ -66,6 +66,7 @@ type JsonData struct {
|
|||||||
Encrypt string `json:"encrypt"`
|
Encrypt string `json:"encrypt"`
|
||||||
Servername string `json:"servername"`
|
Servername string `json:"servername"`
|
||||||
TimeInterval string `json:"timeInterval"`
|
TimeInterval string `json:"timeInterval"`
|
||||||
|
Database string `json:"database"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DataSourceInfo struct {
|
type DataSourceInfo struct {
|
||||||
|
@ -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]);
|
||||||
|
}
|
@ -24,6 +24,7 @@ import {
|
|||||||
} from '@grafana/ui';
|
} from '@grafana/ui';
|
||||||
import { NumberInput } from 'app/core/components/OptionsUI/NumberInput';
|
import { NumberInput } from 'app/core/components/OptionsUI/NumberInput';
|
||||||
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
|
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';
|
import { MSSQLAuthenticationType, MSSQLEncryptOptions, MssqlOptions } from '../types';
|
||||||
|
|
||||||
@ -32,6 +33,8 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
|
|||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
const jsonData = options.jsonData;
|
const jsonData = options.jsonData;
|
||||||
|
|
||||||
|
useMigrateDatabaseField(props);
|
||||||
|
|
||||||
const onResetPassword = () => {
|
const onResetPassword = () => {
|
||||||
updateDatasourcePluginResetOption(props, 'password');
|
updateDatasourcePluginResetOption(props, 'password');
|
||||||
};
|
};
|
||||||
@ -99,9 +102,9 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
|
|||||||
<Input
|
<Input
|
||||||
width={longWidth}
|
width={longWidth}
|
||||||
name="database"
|
name="database"
|
||||||
value={options.database || ''}
|
value={jsonData.database || ''}
|
||||||
placeholder="database name"
|
placeholder="database name"
|
||||||
onChange={onDSOptionChanged('database')}
|
onChange={onUpdateDatasourceJsonDataOption(props, 'database')}
|
||||||
></Input>
|
></Input>
|
||||||
</InlineField>
|
</InlineField>
|
||||||
<InlineField
|
<InlineField
|
||||||
|
@ -10,6 +10,7 @@ import {
|
|||||||
import { Alert, FieldSet, InlineField, InlineFieldRow, InlineSwitch, Input, Link, SecretInput } from '@grafana/ui';
|
import { Alert, FieldSet, InlineField, InlineFieldRow, InlineSwitch, Input, Link, SecretInput } from '@grafana/ui';
|
||||||
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
|
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
|
||||||
import { TLSSecretsConfig } from 'app/features/plugins/sql/components/configuration/TLSSecretsConfig';
|
import { TLSSecretsConfig } from 'app/features/plugins/sql/components/configuration/TLSSecretsConfig';
|
||||||
|
import { useMigrateDatabaseField } from 'app/features/plugins/sql/components/configuration/useMigrateDatabaseField';
|
||||||
|
|
||||||
import { MySQLOptions } from '../types';
|
import { MySQLOptions } from '../types';
|
||||||
|
|
||||||
@ -17,6 +18,8 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<My
|
|||||||
const { options, onOptionsChange } = props;
|
const { options, onOptionsChange } = props;
|
||||||
const jsonData = options.jsonData;
|
const jsonData = options.jsonData;
|
||||||
|
|
||||||
|
useMigrateDatabaseField(props);
|
||||||
|
|
||||||
const onResetPassword = () => {
|
const onResetPassword = () => {
|
||||||
updateDatasourcePluginResetOption(props, 'password');
|
updateDatasourcePluginResetOption(props, 'password');
|
||||||
};
|
};
|
||||||
@ -54,9 +57,9 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<My
|
|||||||
<Input
|
<Input
|
||||||
width={longWidth}
|
width={longWidth}
|
||||||
name="database"
|
name="database"
|
||||||
value={options.database || ''}
|
value={jsonData.database || ''}
|
||||||
placeholder="database name"
|
placeholder="database name"
|
||||||
onChange={onDSOptionChanged('database')}
|
onChange={onUpdateDatasourceJsonDataOption(props, 'database')}
|
||||||
></Input>
|
></Input>
|
||||||
</InlineField>
|
</InlineField>
|
||||||
<InlineFieldRow>
|
<InlineFieldRow>
|
||||||
|
@ -21,6 +21,7 @@ import {
|
|||||||
} from '@grafana/ui';
|
} from '@grafana/ui';
|
||||||
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
|
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
|
||||||
import { TLSSecretsConfig } from 'app/features/plugins/sql/components/configuration/TLSSecretsConfig';
|
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';
|
import { PostgresOptions, PostgresTLSMethods, PostgresTLSModes, SecureJsonData } from '../types';
|
||||||
|
|
||||||
@ -47,6 +48,8 @@ export const PostgresConfigEditor = (props: DataSourcePluginOptionsEditorProps<P
|
|||||||
|
|
||||||
useAutoDetectFeatures({ props, setVersionOptions });
|
useAutoDetectFeatures({ props, setVersionOptions });
|
||||||
|
|
||||||
|
useMigrateDatabaseField(props);
|
||||||
|
|
||||||
const { options, onOptionsChange } = props;
|
const { options, onOptionsChange } = props;
|
||||||
const jsonData = options.jsonData;
|
const jsonData = options.jsonData;
|
||||||
|
|
||||||
@ -103,9 +106,9 @@ export const PostgresConfigEditor = (props: DataSourcePluginOptionsEditorProps<P
|
|||||||
<Input
|
<Input
|
||||||
width={40}
|
width={40}
|
||||||
name="database"
|
name="database"
|
||||||
value={options.database || ''}
|
value={jsonData.database || ''}
|
||||||
placeholder="database name"
|
placeholder="database name"
|
||||||
onChange={onDSOptionChanged('database')}
|
onChange={onUpdateDatasourceJsonDataOption(props, 'database')}
|
||||||
></Input>
|
></Input>
|
||||||
</InlineField>
|
</InlineField>
|
||||||
<InlineFieldRow>
|
<InlineFieldRow>
|
||||||
|
@ -76,7 +76,7 @@ export function useAutoDetectFeatures({ props, setVersionOptions }: Options) {
|
|||||||
function isValidConfig(options: DataSourceSettings<PostgresOptions, SecureJsonData>) {
|
function isValidConfig(options: DataSourceSettings<PostgresOptions, SecureJsonData>) {
|
||||||
return (
|
return (
|
||||||
options.url &&
|
options.url &&
|
||||||
options.database &&
|
options.jsonData.database &&
|
||||||
options.user &&
|
options.user &&
|
||||||
(options.secureJsonData?.password || options.secureJsonFields?.password) &&
|
(options.secureJsonData?.password || options.secureJsonFields?.password) &&
|
||||||
(options.jsonData.sslmode === PostgresTLSModes.disable ||
|
(options.jsonData.sslmode === PostgresTLSModes.disable ||
|
||||||
|
Reference in New Issue
Block a user