forked from grafana/clickhouse-datasource
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client side migration for v3 config name changes
- Loading branch information
1 parent
eac3968
commit ec71bff
Showing
3 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { DataSourceSettings } from "@grafana/data"; | ||
import { renderHook } from "@testing-library/react"; | ||
import { CHConfig } from "types/config"; | ||
import { useMigrateV3Config } from "./CHConfigEditorHooks"; | ||
|
||
describe('useMigrateV3Config', () => { | ||
it('should not call onOptionsChange if no v3 fields are present', async () => { | ||
const onOptionsChange = jest.fn(); | ||
const options = { | ||
jsonData: { | ||
host: 'new', | ||
dialTimeout: '8' | ||
} | ||
} as any as DataSourceSettings<CHConfig>; | ||
|
||
renderHook(opts => useMigrateV3Config(opts, onOptionsChange), { initialProps: options }); | ||
|
||
expect(onOptionsChange).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should rename v3 fields to latest config names', async () => { | ||
const onOptionsChange = jest.fn(); | ||
const options = { | ||
jsonData: { | ||
server: 'address', | ||
timeout: '8' | ||
} | ||
} as any as DataSourceSettings<CHConfig>; | ||
|
||
renderHook(opts => useMigrateV3Config(opts, onOptionsChange), { initialProps: options }); | ||
|
||
const expectedOptions = { | ||
jsonData: { | ||
host: 'address', | ||
dialTimeout: '8' | ||
} | ||
}; | ||
expect(onOptionsChange).toHaveBeenCalledTimes(1); | ||
expect(onOptionsChange).toHaveBeenCalledWith(expect.objectContaining(expectedOptions)); | ||
}); | ||
|
||
it('should remove v3 fields without overwriting latest config names', async () => { | ||
const onOptionsChange = jest.fn(); | ||
const options = { | ||
jsonData: { | ||
server: 'old', | ||
host: 'new', | ||
timeout: '6', | ||
dialTimeout: '8' | ||
} | ||
} as any as DataSourceSettings<CHConfig>; | ||
|
||
renderHook(opts => useMigrateV3Config(opts, onOptionsChange), { initialProps: options }); | ||
|
||
const expectedOptions = { | ||
jsonData: { | ||
host: 'new', | ||
dialTimeout: '8' | ||
} | ||
}; | ||
expect(onOptionsChange).toHaveBeenCalledTimes(1); | ||
expect(onOptionsChange).toHaveBeenCalledWith(expect.objectContaining(expectedOptions)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DataSourceSettings } from "@grafana/data"; | ||
import { useEffect, useRef } from "react"; | ||
import { CHConfig } from "types/config"; | ||
|
||
/** | ||
* Migrates v3 config to latest config schema. | ||
* Copies and removes old "server" to "host" field | ||
* Copies and removes old "timeout" to "dialTimeout" field | ||
*/ | ||
export const useMigrateV3Config = (options: DataSourceSettings<CHConfig>, onOptionsChange: (opts: DataSourceSettings<CHConfig>) => void) => { | ||
const { jsonData } = options; | ||
const v3ServerField = (jsonData as any)['server']; | ||
const v3TimeoutField = (jsonData as any)['timeout']; | ||
|
||
const migrated = useRef<boolean>(!v3ServerField && !v3TimeoutField); | ||
useEffect(() => { | ||
if (!migrated.current) { | ||
const nextJsonOptions = { ...options.jsonData }; | ||
|
||
if (v3ServerField && !nextJsonOptions.host) { | ||
nextJsonOptions.host = v3ServerField; | ||
} | ||
delete (nextJsonOptions as any)['server']; | ||
|
||
if (v3TimeoutField && !nextJsonOptions.dialTimeout) { | ||
nextJsonOptions.dialTimeout = v3TimeoutField; | ||
} | ||
delete (nextJsonOptions as any)['timeout']; | ||
|
||
onOptionsChange({ | ||
...options, | ||
jsonData: nextJsonOptions, | ||
}); | ||
migrated.current = true; | ||
} | ||
}, [v3ServerField, v3TimeoutField, options, onOptionsChange]); | ||
}; |