From e52e296c1102a63929bf894ea813ed3a01cd0fb2 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 5 Mar 2019 14:13:05 +0300 Subject: [PATCH] fix datasource provisioning with direct DB connection enabled, closes #688 --- src/datasource-zabbix/migrations.ts | 19 ++++++++++-- .../specs/migrations.test.js | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/datasource-zabbix/migrations.ts b/src/datasource-zabbix/migrations.ts index 790e044..fd77d9f 100644 --- a/src/datasource-zabbix/migrations.ts +++ b/src/datasource-zabbix/migrations.ts @@ -1,3 +1,5 @@ +import _ from 'lodash'; + /** * Query format migration. * This module can detect query format version and make migration. @@ -54,13 +56,14 @@ export function migrateDSConfig(jsonData) { if (!jsonData) { jsonData = {}; } - const oldVersion = jsonData.schema || 1; - jsonData.schema = DS_CONFIG_SCHEMA; - if (oldVersion === DS_CONFIG_SCHEMA) { + if (!shouldMigrateDSConfig(jsonData)) { return jsonData; } + const oldVersion = jsonData.schema || 1; + jsonData.schema = DS_CONFIG_SCHEMA; + if (oldVersion < 2) { const dbConnectionOptions = jsonData.dbConnection || {}; jsonData.dbConnectionEnable = dbConnectionOptions.enable || false; @@ -70,3 +73,13 @@ export function migrateDSConfig(jsonData) { return jsonData; } + +function shouldMigrateDSConfig(jsonData): boolean { + if (jsonData.dbConnection && !_.isEmpty(jsonData.dbConnection)) { + return true; + } + if (jsonData.schema && jsonData.schema !== DS_CONFIG_SCHEMA) { + return true; + } + return false; +} diff --git a/src/datasource-zabbix/specs/migrations.test.js b/src/datasource-zabbix/specs/migrations.test.js index b11aadc..04f2159 100644 --- a/src/datasource-zabbix/specs/migrations.test.js +++ b/src/datasource-zabbix/specs/migrations.test.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; import { migrateDSConfig, DS_CONFIG_SCHEMA } from '../migrations'; describe('Migrations', () => { @@ -38,4 +39,32 @@ describe('Migrations', () => { expect(ctx.jsonData.dbConnectionDatasourceId).toBeUndefined(); }); }); + + describe('When handling provisioned datasource config', () => { + beforeEach(() => { + ctx.jsonData = { + username: 'zabbix', + password: 'zabbix', + trends: true, + trendsFrom: '7d', + trendsRange: '4d', + cacheTTL: '1h', + alerting: true, + addThresholds: false, + alertingMinSeverity: 3, + disableReadOnlyUsersAck: true, + dbConnectionEnable: true, + dbConnectionDatasourceName: 'MySQL Zabbix', + dbConnectionRetentionPolicy: 'one_year' + }; + }); + + it('should not touch anything if schema is up to date', () => { + const originalConf = _.cloneDeep(ctx.jsonData); + migrateDSConfig(ctx.jsonData); + expect(ctx.jsonData).toMatchObject(originalConf); + expect(ctx.jsonData.dbConnectionEnable).toBe(true); + expect(ctx.jsonData.dbConnectionDatasourceName).toBeDefined(); + }); + }); });