Skip to content

Commit 271e21c

Browse files
committed
Add migration
1 parent 4a7075f commit 271e21c

File tree

2 files changed

+218
-5
lines changed

2 files changed

+218
-5
lines changed

x-pack/plugins/data_enhanced/server/saved_objects/search_session_migration.test.ts

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
searchSessionSavedObjectMigrations,
1010
SearchSessionSavedObjectAttributesPre$7$13$0,
1111
SearchSessionSavedObjectAttributesPre$7$14$0,
12+
SearchSessionSavedObjectAttributesPre$8$0$0,
1213
} from './search_session_migration';
1314
import { SavedObject } from '../../../../../src/core/types';
1415
import { SEARCH_SESSION_TYPE, SearchSessionStatus } from '../../../../../src/plugins/data/common';
@@ -164,3 +165,193 @@ describe('7.13.0 -> 7.14.0', () => {
164165
`);
165166
});
166167
});
168+
169+
describe('7.14.0 -> 7.18.0', () => {
170+
const migration = searchSessionSavedObjectMigrations['8.0.0'];
171+
172+
test('Discover app URL generator migrates to locator', () => {
173+
const mockSessionSavedObject: SavedObject<SearchSessionSavedObjectAttributesPre$8$0$0> = {
174+
id: 'id',
175+
type: SEARCH_SESSION_TYPE,
176+
attributes: {
177+
name: 'my_name',
178+
appId: 'my_app_id',
179+
sessionId: 'sessionId',
180+
urlGeneratorId: 'DISCOVER_APP_URL_GENERATOR',
181+
initialState: {},
182+
restoreState: {},
183+
persisted: true,
184+
idMapping: {},
185+
realmType: 'realmType',
186+
realmName: 'realmName',
187+
username: 'username',
188+
created: '2021-03-26T00:00:00.000Z',
189+
expires: '2021-03-30T00:00:00.000Z',
190+
touched: '2021-03-29T00:00:00.000Z',
191+
completed: '2021-03-29T00:00:00.000Z',
192+
status: SearchSessionStatus.COMPLETE,
193+
version: '7.14.0',
194+
},
195+
references: [],
196+
};
197+
198+
const migratedSession = migration(mockSessionSavedObject, {} as SavedObjectMigrationContext);
199+
200+
expect(migratedSession.attributes).toMatchInlineSnapshot(`
201+
Object {
202+
"appId": "my_app_id",
203+
"completed": "2021-03-29T00:00:00.000Z",
204+
"created": "2021-03-26T00:00:00.000Z",
205+
"expires": "2021-03-30T00:00:00.000Z",
206+
"idMapping": Object {},
207+
"initialState": Object {},
208+
"locatorId": "DISCOVER_APP_LOCATOR",
209+
"name": "my_name",
210+
"persisted": true,
211+
"realmName": "realmName",
212+
"realmType": "realmType",
213+
"restoreState": Object {},
214+
"sessionId": "sessionId",
215+
"status": "complete",
216+
"touched": "2021-03-29T00:00:00.000Z",
217+
"username": "username",
218+
"version": "7.14.0",
219+
}
220+
`);
221+
});
222+
223+
test('Dashboard app URL generator migrates to locator', () => {
224+
const mockSessionSavedObject: SavedObject<SearchSessionSavedObjectAttributesPre$8$0$0> = {
225+
id: 'id',
226+
type: SEARCH_SESSION_TYPE,
227+
attributes: {
228+
name: 'my_name',
229+
appId: 'my_app_id',
230+
sessionId: 'sessionId',
231+
urlGeneratorId: 'DASHBOARD_APP_URL_GENERATOR',
232+
initialState: {},
233+
restoreState: {},
234+
persisted: true,
235+
idMapping: {},
236+
realmType: 'realmType',
237+
realmName: 'realmName',
238+
username: 'username',
239+
created: '2021-03-26T00:00:00.000Z',
240+
expires: '2021-03-30T00:00:00.000Z',
241+
touched: '2021-03-29T00:00:00.000Z',
242+
completed: '2021-03-29T00:00:00.000Z',
243+
status: SearchSessionStatus.COMPLETE,
244+
version: '7.14.0',
245+
},
246+
references: [],
247+
};
248+
249+
const migratedSession = migration(mockSessionSavedObject, {} as SavedObjectMigrationContext);
250+
251+
expect(migratedSession.attributes).toMatchInlineSnapshot(`
252+
Object {
253+
"appId": "my_app_id",
254+
"completed": "2021-03-29T00:00:00.000Z",
255+
"created": "2021-03-26T00:00:00.000Z",
256+
"expires": "2021-03-30T00:00:00.000Z",
257+
"idMapping": Object {},
258+
"initialState": Object {},
259+
"locatorId": "DASHBOARD_APP_LOCATOR",
260+
"name": "my_name",
261+
"persisted": true,
262+
"realmName": "realmName",
263+
"realmType": "realmType",
264+
"restoreState": Object {},
265+
"sessionId": "sessionId",
266+
"status": "complete",
267+
"touched": "2021-03-29T00:00:00.000Z",
268+
"username": "username",
269+
"version": "7.14.0",
270+
}
271+
`);
272+
});
273+
274+
test('Undefined URL generator returns undefined locator', () => {
275+
const mockSessionSavedObject: SavedObject<SearchSessionSavedObjectAttributesPre$8$0$0> = {
276+
id: 'id',
277+
type: SEARCH_SESSION_TYPE,
278+
attributes: {
279+
name: 'my_name',
280+
appId: 'my_app_id',
281+
sessionId: 'sessionId',
282+
urlGeneratorId: undefined,
283+
initialState: {},
284+
restoreState: {},
285+
persisted: true,
286+
idMapping: {},
287+
realmType: 'realmType',
288+
realmName: 'realmName',
289+
username: 'username',
290+
created: '2021-03-26T00:00:00.000Z',
291+
expires: '2021-03-30T00:00:00.000Z',
292+
touched: '2021-03-29T00:00:00.000Z',
293+
completed: '2021-03-29T00:00:00.000Z',
294+
status: SearchSessionStatus.COMPLETE,
295+
version: '7.14.0',
296+
},
297+
references: [],
298+
};
299+
300+
const migratedSession = migration(mockSessionSavedObject, {} as SavedObjectMigrationContext);
301+
302+
expect(migratedSession.attributes).toMatchInlineSnapshot(`
303+
Object {
304+
"appId": "my_app_id",
305+
"completed": "2021-03-29T00:00:00.000Z",
306+
"created": "2021-03-26T00:00:00.000Z",
307+
"expires": "2021-03-30T00:00:00.000Z",
308+
"idMapping": Object {},
309+
"initialState": Object {},
310+
"locatorId": undefined,
311+
"name": "my_name",
312+
"persisted": true,
313+
"realmName": "realmName",
314+
"realmType": "realmType",
315+
"restoreState": Object {},
316+
"sessionId": "sessionId",
317+
"status": "complete",
318+
"touched": "2021-03-29T00:00:00.000Z",
319+
"username": "username",
320+
"version": "7.14.0",
321+
}
322+
`);
323+
});
324+
325+
test('Other URL generator throws error', () => {
326+
const mockSessionSavedObject: SavedObject<SearchSessionSavedObjectAttributesPre$8$0$0> = {
327+
id: 'id',
328+
type: SEARCH_SESSION_TYPE,
329+
attributes: {
330+
name: 'my_name',
331+
appId: 'my_app_id',
332+
sessionId: 'sessionId',
333+
urlGeneratorId: 'my_url_generator_id',
334+
initialState: {},
335+
restoreState: {},
336+
persisted: true,
337+
idMapping: {},
338+
realmType: 'realmType',
339+
realmName: 'realmName',
340+
username: 'username',
341+
created: '2021-03-26T00:00:00.000Z',
342+
expires: '2021-03-30T00:00:00.000Z',
343+
touched: '2021-03-29T00:00:00.000Z',
344+
completed: '2021-03-29T00:00:00.000Z',
345+
status: SearchSessionStatus.COMPLETE,
346+
version: '7.14.0',
347+
},
348+
references: [],
349+
};
350+
351+
expect(() =>
352+
migration(mockSessionSavedObject, {} as SavedObjectMigrationContext)
353+
).toThrowErrorMatchingInlineSnapshot(
354+
`"No migration found for search session URL generator my_url_generator_id"`
355+
);
356+
});
357+
});

x-pack/plugins/data_enhanced/server/saved_objects/search_session_migration.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,28 @@ export type SearchSessionSavedObjectAttributesPre$7$13$0 = Omit<
2929
* but what is important for 7.14.0 is that the version is less then "7.14.0"
3030
*/
3131
export type SearchSessionSavedObjectAttributesPre$7$14$0 = Omit<
32-
SearchSessionSavedObjectAttributesLatest,
32+
SearchSessionSavedObjectAttributesPre$8$0$0,
3333
'version'
3434
>;
3535

36+
/**
37+
* In 8.0.0, we migrated from using URL generators to the locators service. As a result, we move
38+
* from using `urlGeneratorId` to `locatorId`.
39+
*/
40+
export type SearchSessionSavedObjectAttributesPre$8$0$0 = Omit<
41+
SearchSessionSavedObjectAttributesLatest,
42+
'locatorId'
43+
> & {
44+
urlGeneratorId?: string;
45+
};
46+
47+
function getLocatorId(urlGeneratorId?: string) {
48+
if (!urlGeneratorId) return;
49+
if (urlGeneratorId === 'DISCOVER_APP_URL_GENERATOR') return 'DISCOVER_APP_LOCATOR';
50+
if (urlGeneratorId === 'DASHBOARD_APP_URL_GENERATOR') return 'DASHBOARD_APP_LOCATOR';
51+
throw new Error(`No migration found for search session URL generator ${urlGeneratorId}`);
52+
}
53+
3654
export const searchSessionSavedObjectMigrations: SavedObjectMigrationMap = {
3755
'7.13.0': (
3856
doc: SavedObjectUnsanitizedDoc<SearchSessionSavedObjectAttributesPre$7$13$0>
@@ -60,10 +78,14 @@ export const searchSessionSavedObjectMigrations: SavedObjectMigrationMap = {
6078
},
6179
};
6280
},
63-
'7.16.0': (
64-
doc: SavedObjectUnsanitizedDoc<SearchSessionSavedObjectAttributesLatest>
81+
'8.0.0': (
82+
doc: SavedObjectUnsanitizedDoc<SearchSessionSavedObjectAttributesPre$8$0$0>
6583
): SavedObjectUnsanitizedDoc<SearchSessionSavedObjectAttributesLatest> => {
66-
// TODO: Convert `urlGeneratorId` to `locatorId` (do we need to convert state as well?)
67-
return doc;
84+
const {
85+
attributes: { urlGeneratorId, ...otherAttrs },
86+
} = doc;
87+
const locatorId = getLocatorId(urlGeneratorId);
88+
const attributes = { ...otherAttrs, locatorId };
89+
return { ...doc, attributes };
6890
},
6991
};

0 commit comments

Comments
 (0)