Skip to content

Commit

Permalink
fix the bug when collect daved object
Browse files Browse the repository at this point in the history
  • Loading branch information
yujin-emma committed Feb 2, 2024
1 parent e5aeb22 commit c756c5b
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export interface ConflictsForDataSourceParams {
objects: Array<SavedObject<{ title?: string }>>;
ignoreRegularConflicts?: boolean;
retries?: SavedObjectsImportRetry[];
createNewCopies?: boolean;
dataSourceId?: string;
}

Expand Down Expand Up @@ -73,37 +72,26 @@ export async function checkConflictsForDataSource({
} = object;
const { destinationId } = retryMap.get(`${type}:${id}`) || {};

const currentDataSourceId = dataSourceId;

if (!object.type.includes('data-source')) {
// check the previous data source existed or not
// by extract it from the id
// e.g. e0c9e490-bdd7-11ee-b216-d78a57002330_ff959d40-b880-11e8-a6d9-e546fe2bba5f
// e0c9e490-bdd7-11ee-b216-d78a57002330 is the data source id
// for saved object data source itself, e0c9e490-bdd7-11ee-b216-d78a57002330 return undefined
const parts = id.split('_'); // this is the array to host the split results of the id
if (object.type !== 'data-source') {
const parts = id.split('_');
const previoudDataSourceId = parts.length > 1 ? parts[0] : undefined;
// case for import saved object from osd exported
// when the imported daved objects with the different dataSourceId comparing to the current dataSourceId
// previous data source id not exist, push it to filtered object
// no conflict
if (!previoudDataSourceId || previoudDataSourceId === currentDataSourceId) {
if (!previoudDataSourceId || previoudDataSourceId === dataSourceId) {
filteredObjects.push(object);
} else if (previoudDataSourceId && previoudDataSourceId !== currentDataSourceId) {
} else if (previoudDataSourceId && previoudDataSourceId !== dataSourceId) {
if (ignoreRegularConflicts) {
// overwrite
// ues old key and new value in the importIdMap
// old key is used to look up, new key is used to be the id of new object
const omitOriginId = ignoreRegularConflicts;
// e.g. e0c9e490-bdd7-11ee-b216-d78a57002330_ff959d40-b880-11e8-a6d9-e546fe2bba5f
// rawId is ff959d40-b880-11e8-a6d9-e546fe2bba5f
const rawId = parts[1];
importIdMap.set(`${type}:${id}`, { id: `${currentDataSourceId}_${rawId}`, omitOriginId });
importIdMap.set(`${type}:${id}`, { id: `${dataSourceId}_${rawId}`, omitOriginId });
pendingOverwrites.add(`${type}:${id}`);
filteredObjects.push({ ...object, id: `${currentDataSourceId}_${rawId}` });
filteredObjects.push({ ...object, id: `${dataSourceId}_${rawId}` });
} else {
// not override
// push error
const error = { type: 'conflict' as 'conflict', ...(destinationId && { destinationId }) };
errors.push({ type, id, title, meta: { title }, error });
}
Expand Down
1 change: 0 additions & 1 deletion src/core/server/saved_objects/import/check_conflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ interface CheckConflictsParams {
ignoreRegularConflicts?: boolean;
retries?: SavedObjectsImportRetry[];
createNewCopies?: boolean;
dataSourceId?: string;
}

const isUnresolvableConflict = (error: SavedObjectError) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ export async function checkOriginConflicts({ objects, ...params }: CheckOriginCo
if (sources.length === 1 && destinations.length === 1) {
// This is a simple "inexact match" result -- a single import object has a single destination conflict.
if (params.ignoreRegularConflicts) {
// importIdMap.set(`${type}:${id}`, { id: dataSourceId ? `${dataSourceId}_${destinations[0].id}` : `${destinations[0].id}` });
importIdMap.set(`${type}:${id}`, { id: destinations[0].id });
pendingOverwrites.add(`${type}:${id}`);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ export async function collectSavedObjects({
}),
createFilterStream<SavedObject>((obj) => (filter ? filter(obj) : true)),
createMapStream((obj: SavedObject) => {
if (dataSourceId) {
importIdMap.set(`${dataSourceId}_${obj.type}:${obj.id}`, {});
} else {
importIdMap.set(`${obj.type}:${obj.id}`, {});
}

importIdMap.set(`${obj.type}:${obj.id}`, {});
// Ensure migrations execute on every saved object
return Object.assign({ migrationVersion: {} }, obj);
}),
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/saved_objects/import/import_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export async function importSavedObjectsFromStream({
savedObjectsClient,
namespace,
ignoreRegularConflicts: overwrite,
dataSourceId,
};

// resolve when data source exist, pass the filtered objects to next check conflict
Expand All @@ -111,7 +110,6 @@ export async function importSavedObjectsFromStream({
]);
}

// Check single-namespace objects for conflicts in this namespace, and check multi-namespace objects for conflicts across all namespaces
const checkConflictsResult = await checkConflicts(checkConflictsParams);
errorAccumulator = [...errorAccumulator, ...checkConflictsResult.errors];
importIdMap = new Map([...importIdMap, ...checkConflictsResult.importIdMap]);
Expand Down
1 change: 0 additions & 1 deletion src/core/server/saved_objects/import/regenerate_ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { SavedObject } from '../types';
* @param objects The saved objects to generate new IDs for.
*/
export const regenerateIds = (objects: SavedObject[], dataSourceId: string | undefined) => {
// add datasource?
const importIdMap = objects.reduce((acc, object) => {
if (dataSourceId) {
return acc.set(`${object.type}:${object.id}`, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export class SavedObjectTypeRegistry {
* To only get the visible types (which is the most common use case), use `getVisibleTypes` instead.
*/
public getAllTypes() {
// console.log("[DEBUG] getAllTypes", this.types.values());
return [...this.types.values()];
}

Expand Down

0 comments on commit c756c5b

Please sign in to comment.