add migrations for datasource config

This commit is contained in:
Alexander Zobnin
2018-08-28 20:37:10 +03:00
parent ed5cc05c13
commit 07c7e4fc9e
5 changed files with 76 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ module.exports = {
"moduleNameMapper": {
"^[./a-zA-Z0-9$_-]+\.css\!?$": "<rootDir>/src/test-setup/cssStub.js",
},
"testRegex": "(\\.|/)(test|spec)\\.(jsx?|tsx?)$",
"transform": {
"^.+\\.js$": "babel-jest"
},

View File

@@ -1,11 +1,16 @@
import _ from 'lodash';
import { migrateDSConfig } from './migrations';
const SUPPORTED_SQL_DS = ['mysql', 'postgres'];
const defaultConfig = {
dbConnection: {
enable: false,
}
trends: false,
dbConnectionEnable: false,
dbConnectionDatasourceId: null,
alerting: false,
addThresholds: false,
alertingMinSeverity: 3,
disableReadOnlyUsersAck: false
};
export class ZabbixDSConfigController {
@@ -14,6 +19,7 @@ export class ZabbixDSConfigController {
constructor($scope, $injector, datasourceSrv) {
this.datasourceSrv = datasourceSrv;
this.current.jsonData = migrateDSConfig(this.current.jsonData);
_.defaults(this.current.jsonData, defaultConfig);
this.sqlDataSources = this.getSupportedSQLDataSources();
}

View File

@@ -25,7 +25,7 @@ export class ZabbixDatasource {
this.basicAuth = instanceSettings.basicAuth;
this.withCredentials = instanceSettings.withCredentials;
const jsonData = instanceSettings.jsonData || {};
const jsonData = migrations.migrateDSConfig(instanceSettings.jsonData);
// Zabbix API credentials
this.username = jsonData.username;
@@ -49,9 +49,8 @@ export class ZabbixDatasource {
this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck;
// Direct DB Connection options
let dbConnectionOptions = jsonData.dbConnection || {};
this.enableDirectDBConnection = dbConnectionOptions.enable || jsonData.dbConnectionEnable || false;
this.datasourceId = dbConnectionOptions.datasourceId || jsonData.dbConnectionDatasourceId;
this.enableDirectDBConnection = jsonData.dbConnectionEnable || false;
this.datasourceId = jsonData.dbConnectionDatasourceId;
let zabbixOptions = {
url: this.url,

View File

@@ -41,3 +41,25 @@ function convertToRegex(str) {
return '/.*/';
}
}
export const DS_CONFIG_SCHEMA = 2;
export function migrateDSConfig(jsonData) {
if (!jsonData) {
jsonData = {};
}
const oldVersion = jsonData.schema || 1;
jsonData.schema = DS_CONFIG_SCHEMA;
if (oldVersion === DS_CONFIG_SCHEMA) {
return jsonData;
}
if (oldVersion < 2) {
const dbConnectionOptions = jsonData.dbConnection || {};
jsonData.dbConnectionEnable = dbConnectionOptions.enable || false;
jsonData.dbConnectionDatasourceId = dbConnectionOptions.datasourceId || null;
delete jsonData.dbConnection;
}
return jsonData;
}

View File

@@ -0,0 +1,41 @@
import { migrateDSConfig, DS_CONFIG_SCHEMA } from '../migrations';
describe('Migrations', () => {
let ctx = {};
describe('When migrating datasource config', () => {
beforeEach(() => {
ctx.jsonData = {
dbConnection: {
enable: true,
datasourceId: 1
}
};
});
it('should change direct DB connection setting to flat style', () => {
migrateDSConfig(ctx.jsonData);
expect(ctx.jsonData).toMatchObject({
dbConnectionEnable: true,
dbConnectionDatasourceId: 1,
schema: DS_CONFIG_SCHEMA
});
});
it('should not touch anything if schema is up to date', () => {
ctx.jsonData = {
futureOptionOne: 'foo',
futureOptionTwo: 'bar',
schema: DS_CONFIG_SCHEMA
};
migrateDSConfig(ctx.jsonData);
expect(ctx.jsonData).toMatchObject({
futureOptionOne: 'foo',
futureOptionTwo: 'bar',
schema: DS_CONFIG_SCHEMA
});
expect(ctx.jsonData.dbConnectionEnable).toBeUndefined();
expect(ctx.jsonData.dbConnectionDatasourceId).toBeUndefined();
});
});
});