-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[D&D] Save index pattern using proper saved object structure (#2218)
* Save index pattern as a proper saved object relationship, previously it is only saved as an id in the visualizationState. Signed-off-by: abbyhu2000 <[email protected]> * remove index id from visualization when saving & add comments Signed-off-by: abbyhu2000 <[email protected]> * add migration for existing wizard Signed-off-by: abbyhu2000 <[email protected]> * add migration unit test Signed-off-by: abbyhu2000 <[email protected]> * change wizard doc version to 2; change migration version to 2.3.0 Signed-off-by: abbyhu2000 <[email protected]> Signed-off-by: abbyhu2000 <[email protected]> (cherry picked from commit 428e832)
- Loading branch information
1 parent
71c7374
commit acb92f0
Showing
7 changed files
with
218 additions
and
7 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
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
20 changes: 20 additions & 0 deletions
20
src/plugins/wizard/public/saved_visualizations/saved_visualization_references.ts
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,20 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectReference } from '../../../../core/public'; | ||
import { WizardVisSavedObject } from '../types'; | ||
import { injectSearchSourceReferences } from '../../../data/public'; | ||
|
||
export function injectReferences( | ||
savedObject: WizardVisSavedObject, | ||
references: SavedObjectReference[] | ||
) { | ||
if (savedObject.searchSourceFields) { | ||
savedObject.searchSourceFields = injectSearchSourceReferences( | ||
savedObject.searchSourceFields as any, | ||
references | ||
); | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
src/plugins/wizard/server/saved_objects/wizard_migration.test.ts
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,107 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectMigrationFn, SavedObjectMigrationContext } from '../../../../core/server'; | ||
import { wizardSavedObjectTypeMigrations } from './wizard_migration'; | ||
|
||
const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext; | ||
|
||
describe('2.3.0', () => { | ||
const migrate = (doc: any) => | ||
wizardSavedObjectTypeMigrations['2.3.0']( | ||
doc as Parameters<SavedObjectMigrationFn>[0], | ||
savedObjectMigrationContext | ||
); | ||
|
||
it('should return original doc if visualizationState is not found', () => { | ||
const migratedDoc = migrate({ | ||
type: 'wizard', | ||
attributes: {}, | ||
}); | ||
|
||
expect(migratedDoc).toEqual({ | ||
type: 'wizard', | ||
attributes: {}, | ||
}); | ||
}); | ||
|
||
it('should return original doc if indexPattern is not found within visualizationState', () => { | ||
const migratedDoc = migrate({ | ||
type: 'wizard', | ||
attributes: { | ||
visualizationState: { | ||
searchSource: '', | ||
activeVisualization: {}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(migratedDoc).toEqual({ | ||
type: 'wizard', | ||
attributes: { | ||
visualizationState: { | ||
searchSource: '', | ||
activeVisualization: {}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return original doc if references is not an array', () => { | ||
const migratedDoc = migrate({ | ||
type: 'wizard', | ||
attributes: { | ||
visualizationState: {}, | ||
}, | ||
references: {}, | ||
}); | ||
|
||
expect(migratedDoc).toEqual({ | ||
type: 'wizard', | ||
attributes: { | ||
visualizationState: {}, | ||
}, | ||
references: {}, | ||
}); | ||
}); | ||
|
||
it('should migrate the old version wizard saved object to new version wizard saved object', () => { | ||
const migratedDoc = migrate({ | ||
type: 'wizard', | ||
attributes: { | ||
visualizationState: JSON.stringify({ | ||
searchFields: {}, | ||
activeVisualization: {}, | ||
indexPattern: 'indexPatternId', | ||
}), | ||
version: 1, | ||
}, | ||
references: [], | ||
}); | ||
|
||
expect(migratedDoc).toEqual({ | ||
type: 'wizard', | ||
attributes: { | ||
visualizationState: JSON.stringify({ | ||
searchFields: {}, | ||
activeVisualization: {}, | ||
}), | ||
version: 2, | ||
kibanaSavedObjectMeta: { | ||
searchSourceJSON: JSON.stringify({ | ||
indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', | ||
}), | ||
}, | ||
}, | ||
references: [ | ||
{ | ||
name: 'kibanaSavedObjectMeta.searchSourceJSON.index', | ||
type: 'index-pattern', | ||
id: 'indexPatternId', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
src/plugins/wizard/server/saved_objects/wizard_migration.ts
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,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { get, flow } from 'lodash'; | ||
import { SavedObjectMigrationFn } from '../../../../core/server'; | ||
|
||
const migrateIndexPattern: SavedObjectMigrationFn<any, any> = (doc) => { | ||
try { | ||
const visualizationStateJSON = get(doc, 'attributes.visualizationState'); | ||
const visualizationState = JSON.parse(visualizationStateJSON); | ||
const indexPatternId = visualizationState.indexPattern; | ||
const indexRefName = 'kibanaSavedObjectMeta.searchSourceJSON.index'; | ||
|
||
if (indexPatternId && Array.isArray(doc.references)) { | ||
const searchSourceIndex = { | ||
indexRefName, | ||
}; | ||
const visualizationWithoutIndex = { | ||
searchFields: visualizationState.searchFields, | ||
activeVisualization: visualizationState.activeVisualization, | ||
}; | ||
doc.attributes.visualizationState = JSON.stringify(visualizationWithoutIndex); | ||
|
||
doc.references.push({ | ||
name: indexRefName, | ||
type: 'index-pattern', | ||
id: indexPatternId, | ||
}); | ||
doc.attributes.version = 2; | ||
|
||
return { | ||
...doc, | ||
attributes: { | ||
...doc.attributes, | ||
kibanaSavedObjectMeta: { | ||
searchSourceJSON: JSON.stringify(searchSourceIndex), | ||
}, | ||
}, | ||
}; | ||
} | ||
return doc; | ||
} catch (e) { | ||
return doc; | ||
} | ||
}; | ||
|
||
export const wizardSavedObjectTypeMigrations = { | ||
'2.3.0': flow(migrateIndexPattern), | ||
}; |