From 03a728d53959d32223c71f06b584524909f1e37f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 5 Mar 2024 23:04:53 +0000 Subject: [PATCH] Create a migration function for datasource to add migrationVersion field (#6025) This PR is to add a migration function with version 2.4.0 for datasource to add a migrationVersion field. For more information, please refer to the RFC: #6022 Issues Resolved #6022 Signed-off-by: Yibo Wang (cherry picked from commit 70adcc96869c2add8324b197b83c8db9a8644247) Signed-off-by: github-actions[bot] --- .../server/saved_objects/data_source.test.ts | 29 +++++++++++++++++++ .../server/saved_objects/data_source.ts | 11 ++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/plugins/data_source/server/saved_objects/data_source.test.ts diff --git a/src/plugins/data_source/server/saved_objects/data_source.test.ts b/src/plugins/data_source/server/saved_objects/data_source.test.ts new file mode 100644 index 000000000000..d465ee2a575b --- /dev/null +++ b/src/plugins/data_source/server/saved_objects/data_source.test.ts @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { migrateDataSource } from './data_source'; +import { savedObjectsServiceMock } from '../../../../core/server/mocks'; + +const contextMock = savedObjectsServiceMock.createMigrationContext(); + +describe('migrateDataSource Function', () => { + it('should return the input document unchanged', () => { + const mockDoc = { + id: 'test-id', + type: 'test-type', + attributes: { + name: 'Test Name', + description: 'Test Description', + }, + references: [], + }; + + // Call the migrateDataSource function with the mock document + const result = migrateDataSource(mockDoc, contextMock); + + // Expect the result to be deeply equal to the mock document + expect(result).toEqual(mockDoc); + }); +}); diff --git a/src/plugins/data_source/server/saved_objects/data_source.ts b/src/plugins/data_source/server/saved_objects/data_source.ts index 9404a4bcf371..3f31e7bd14b7 100644 --- a/src/plugins/data_source/server/saved_objects/data_source.ts +++ b/src/plugins/data_source/server/saved_objects/data_source.ts @@ -3,7 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { SavedObjectsType } from 'opensearch-dashboards/server'; +import { flow } from 'lodash'; +import { SavedObjectMigrationFn, SavedObjectsType } from 'opensearch-dashboards/server'; + +// create a migration function which return the doc without any changes +export const migrateDataSource: SavedObjectMigrationFn = (doc) => ({ + ...doc, +}); export const dataSource: SavedObjectsType = { name: 'data-source', @@ -34,4 +40,7 @@ export const dataSource: SavedObjectsType = { }, }, }, + migrations: { + '2.4.0': flow(migrateDataSource), // 2.4.0 is the version that introduces the datasource + }, };