Skip to content

Instantly share code, notes, and snippets.

@stacey-gammon
Last active June 17, 2021 13:59
Show Gist options
  • Save stacey-gammon/bd64bd678cf3c437e6bc443efad50b0f to your computer and use it in GitHub Desktop.
Save stacey-gammon/bd64bd678cf3c437e6bc443efad50b0f to your computer and use it in GitHub Desktop.
Persistable state migration fn
interface PersistableStateService {
  getMigrations: { [key: string]: MigrationFn }
}


function collectDashboardPanelMigrations(): { [key:string]: MigrationFn } {
  // Dashboard plugin may have registered some of it's own panel migrations.
  const panelMigrations: { [key:string]: MigrationFn } = {
    ['7.5']: migratePanelTo75(state: PanelStatePre75);
  }
  
  // Now collect any migrations required from embeddables.
  const embeddableMigrations = embeddableServices.getMigrations();
  
  // The logic here would still be pretty complex because you need to merge the two migrations objects.
  // If, for instance, embeddable input changed where it was stored
  Object.keys(embeddableMigrations).forEach(version => {
    if (!panelMigrations[version]) {
      // Easy case - no merging neccessary, but be careful about the shape of the panel! What happens
      // If there was a particular version where "panel.embeddableInput" changed?
      panelMigrations[version] = (panel) => panel.embeddableInput = embeddableMigrations[version](panel.embeddableInput)
    } else {
      // More complicated case, have to merge. Does it matter which one you run first?
      panelMigrations[version] = (panel) => {
        const newPanel = panelMigrations[version](panel);
        newPanel.embeddableInput = embeddableMigrations[version](newPanel.embeddableInput);
      }
    }
  }
  
  // Return the merged object that maps version to migration function.
  return panelMigrations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment