Skip to content

Commit 540ce12

Browse files
VizPanel: Add shouldMigrate() callback to migration logic (#1092)
Co-authored-by: Adela Almasan <[email protected]>
1 parent 1b96cd8 commit 540ce12

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

packages/scenes/src/components/VizPanel/VizPanel.test.tsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,86 @@ describe('VizPanel', () => {
642642
});
643643
});
644644

645+
describe('Migration with shouldMigrate functionality', () => {
646+
let onPanelMigration: jest.Mock;
647+
let shouldMigrate: jest.Mock;
648+
let panel: VizPanel<OptionsPlugin1, FieldConfigPlugin1>;
649+
650+
beforeEach(() => {
651+
onPanelMigration = jest.fn().mockReturnValue({ option2: 'migration option' });
652+
shouldMigrate = jest.fn();
653+
});
654+
655+
it('should call migration when shouldMigrate returns true even with same plugin version', async () => {
656+
panel = new VizPanel<OptionsPlugin1, FieldConfigPlugin1>({
657+
pluginId: 'custom-plugin-id',
658+
pluginVersion: '1.0.0',
659+
});
660+
661+
pluginToLoad = getTestPlugin1();
662+
pluginToLoad.onPanelMigration = onPanelMigration;
663+
// @ts-expect-error
664+
pluginToLoad.shouldMigrate = shouldMigrate.mockReturnValue(true);
665+
666+
await panel.activate();
667+
668+
expect(onPanelMigration).toHaveBeenCalled();
669+
expect(shouldMigrate).toHaveBeenCalled();
670+
expect(panel.state.options.option2).toBe('migration option');
671+
});
672+
673+
it('should run migration due to version change without calling shouldMigrate', async () => {
674+
panel = new VizPanel<OptionsPlugin1, FieldConfigPlugin1>({
675+
pluginId: 'custom-plugin-id',
676+
pluginVersion: '0.9.0',
677+
});
678+
679+
pluginToLoad = getTestPlugin1();
680+
pluginToLoad.onPanelMigration = onPanelMigration;
681+
// @ts-expect-error
682+
pluginToLoad.shouldMigrate = shouldMigrate.mockReturnValue(false);
683+
684+
await panel.activate();
685+
686+
expect(onPanelMigration).toHaveBeenCalled();
687+
expect(shouldMigrate).not.toHaveBeenCalled();
688+
expect(panel.state.options.option2).toBe('migration option');
689+
});
690+
691+
it('should not call migration when shouldMigrate returns false with same plugin version', async () => {
692+
panel = new VizPanel<OptionsPlugin1, FieldConfigPlugin1>({
693+
pluginId: 'custom-plugin-id',
694+
pluginVersion: '1.0.0',
695+
});
696+
697+
pluginToLoad = getTestPlugin1();
698+
pluginToLoad.onPanelMigration = onPanelMigration;
699+
// @ts-expect-error
700+
pluginToLoad.shouldMigrate = shouldMigrate.mockReturnValue(false);
701+
702+
await panel.activate();
703+
704+
expect(shouldMigrate).toHaveBeenCalled();
705+
expect(onPanelMigration).not.toHaveBeenCalled();
706+
expect(panel.state.options.option2).toBeUndefined();
707+
});
708+
709+
it('should work with existing migration when shouldMigrate is undefined', async () => {
710+
panel = new VizPanel<OptionsPlugin1, FieldConfigPlugin1>({
711+
pluginId: 'custom-plugin-id',
712+
pluginVersion: '0.9.0',
713+
});
714+
715+
pluginToLoad = getTestPlugin1();
716+
pluginToLoad.onPanelMigration = onPanelMigration;
717+
718+
await panel.activate();
719+
720+
expect(onPanelMigration).toHaveBeenCalled();
721+
expect(panel.state.options.option2).toBe('migration option');
722+
});
723+
});
724+
645725
describe('Should provide a panel context', () => {
646726
let panel: VizPanel<OptionsPlugin1, FieldConfigPlugin1>;
647727

packages/scenes/src/components/VizPanel/VizPanel.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ export class VizPanel<TOptions = {}, TFieldConfig extends {} = {}> extends Scene
233233

234234
_UNSAFE_customMigrationHandler?.(panel, plugin);
235235

236-
if (plugin.onPanelMigration && currentVersion !== pluginVersion && !isAfterPluginChange) {
236+
//@ts-expect-error (TODO: remove after upgrading with https://github.com/grafana/grafana/pull/108998)
237+
const needsMigration = currentVersion !== pluginVersion || plugin.shouldMigrate?.(panel);
238+
239+
if (plugin.onPanelMigration && needsMigration && !isAfterPluginChange) {
237240
// These migration handlers also mutate panel.fieldConfig to migrate fieldConfig
238241
panel.options = await plugin.onPanelMigration(panel);
239242
}

0 commit comments

Comments
 (0)