From 5e728961d9868f869d95d6c8209562f99ae4c27b Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Thu, 16 Oct 2025 15:07:31 +0200 Subject: [PATCH 1/6] fix: try out and revert invalid edit COMMIT-9945 --- .../src/store/diagram.ts | 63 ++++++++++++------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/packages/compass-data-modeling/src/store/diagram.ts b/packages/compass-data-modeling/src/store/diagram.ts index a6dc2849d77..0ead589f1f6 100644 --- a/packages/compass-data-modeling/src/store/diagram.ts +++ b/packages/compass-data-modeling/src/store/diagram.ts @@ -59,7 +59,6 @@ export type DiagramState = current: [Edit, ...Edit[]]; next: Edit[][]; }; - editErrors?: string[]; selectedItems: SelectedItems | null; isNewlyCreated: boolean; draftCollection?: string; @@ -72,9 +71,9 @@ export enum DiagramActionTypes { RENAME_DIAGRAM = 'data-modeling/diagram/RENAME_DIAGRAM', APPLY_INITIAL_LAYOUT = 'data-modeling/diagram/APPLY_INITIAL_LAYOUT', APPLY_EDIT = 'data-modeling/diagram/APPLY_EDIT', - APPLY_EDIT_FAILED = 'data-modeling/diagram/APPLY_EDIT_FAILED', UNDO_EDIT = 'data-modeling/diagram/UNDO_EDIT', REDO_EDIT = 'data-modeling/diagram/REDO_EDIT', + REVERT_FAILED_EDIT = 'data-modeling/diagram/REVERT_FAILED_EDIT', COLLECTION_SELECTED = 'data-modeling/diagram/COLLECTION_SELECTED', RELATIONSHIP_SELECTED = 'data-modeling/diagram/RELATIONSHIP_SELECTED', FIELD_SELECTED = 'data-modeling/diagram/FIELD_SELECTED', @@ -102,15 +101,14 @@ export type ApplyEditAction = { edit: Edit; }; -export type ApplyEditFailedAction = { - type: DiagramActionTypes.APPLY_EDIT_FAILED; - errors: string[]; -}; - export type UndoEditAction = { type: DiagramActionTypes.UNDO_EDIT; }; +export type RevertFailedEditAction = { + type: DiagramActionTypes.REVERT_FAILED_EDIT; +}; + export type RedoEditAction = { type: DiagramActionTypes.REDO_EDIT; }; @@ -140,7 +138,7 @@ export type DiagramActions = | DeleteDiagramAction | RenameDiagramAction | ApplyEditAction - | ApplyEditFailedAction + | RevertFailedEditAction | UndoEditAction | RedoEditAction | CollectionSelectedAction @@ -254,23 +252,23 @@ export const diagramReducer: Reducer = ( action.edit.type === 'AddCollection' ? action.edit.ns : undefined, }; } - if (isAction(action, DiagramActionTypes.APPLY_EDIT_FAILED)) { - return { - ...state, - editErrors: action.errors, - }; - } - if (isAction(action, DiagramActionTypes.UNDO_EDIT)) { + if ( + isAction(action, DiagramActionTypes.UNDO_EDIT) || + isAction(action, DiagramActionTypes.REVERT_FAILED_EDIT) + ) { const newCurrent = state.edits.prev.pop() || []; if (!isNonEmptyArray(newCurrent)) { return state; } + const next = isAction(action, DiagramActionTypes.REVERT_FAILED_EDIT) + ? [...state.edits.next] + : [...state.edits.next, state.edits.current]; return { ...state, edits: { prev: [...state.edits.prev], current: newCurrent, - next: [...state.edits.next, state.edits.current], + next, }, updatedAt: new Date().toISOString(), }; @@ -594,27 +592,46 @@ export function renameCollection( return applyEdit(edit); } +function handleError(messages: string[]) { + openToast('data-modeling-error', { + variant: 'warning', + title: 'Error opening diagram', + description: messages.join(' '), + }); +} + export function applyEdit( rawEdit: EditAction -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState, { dataModelStorage }) => { const edit = { ...rawEdit, id: new UUID().toString(), timestamp: new Date().toISOString(), }; - const { result: isValid, errors } = validateEdit(edit); - if (!isValid) { - dispatch({ - type: DiagramActionTypes.APPLY_EDIT_FAILED, - errors, - }); + const { result, errors } = validateEdit(edit); + let isValid = result; + if (!result) { + handleError(errors); return isValid; } dispatch({ type: DiagramActionTypes.APPLY_EDIT, edit, }); + + // try to build the model with the latest edit + try { + selectCurrentModelFromState(getState()); + } catch (e) { + handleError([ + 'Something went wrong when applying the changes.', + (e as Error).message, + ]); + dispatch({ type: DiagramActionTypes.REVERT_FAILED_EDIT }); + isValid = false; + } + void dataModelStorage.save(getCurrentDiagramFromState(getState())); return isValid; }; From e8447d5451e8dcdfe579af985014fc5425dfc91e Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Fri, 17 Oct 2025 12:22:50 +0200 Subject: [PATCH 2/6] ensureCollectionExists --- .../src/store/apply-edit.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/compass-data-modeling/src/store/apply-edit.ts b/packages/compass-data-modeling/src/store/apply-edit.ts index 2e34d273a39..68c7a320284 100644 --- a/packages/compass-data-modeling/src/store/apply-edit.ts +++ b/packages/compass-data-modeling/src/store/apply-edit.ts @@ -32,6 +32,16 @@ function renameFieldInRelationshipSide( }; } +function ensureCollectionExists( + collections: DataModelCollection[], + ns: string +): void { + const collection = collections.find((c) => c.ns === ns); + if (!collection) { + throw new Error(`Collection '${ns}' not found`); + } +} + export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { if (edit.type === 'SetModel') { return edit.model; @@ -81,6 +91,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'MoveCollection': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { @@ -95,6 +106,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RemoveCollection': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, // Remove any relationships involving the collection being removed. @@ -109,6 +121,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RenameCollection': { + ensureCollectionExists(model.collections, edit.fromNS); return { ...model, // Update relationships to point to the renamed namespace. @@ -137,6 +150,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'UpdateCollectionNote': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { @@ -151,6 +165,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'AddField': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { @@ -169,6 +184,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RemoveField': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, // Remove any relationships involving the field being removed. @@ -193,6 +209,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RenameField': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, // Update any relationships involving the field being renamed. @@ -227,6 +244,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'ChangeFieldType': { + ensureCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { From 2d0acb1db9d957869fe523266a9e181b4cbb08d0 Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Fri, 17 Oct 2025 12:23:00 +0200 Subject: [PATCH 3/6] tests and cleanup --- .../src/components/diagram-editor.tsx | 2 - .../src/store/diagram.spec.ts | 32 ++++++++++----- .../src/store/diagram.ts | 39 ++++++++++--------- .../compass-data-modeling/src/store/index.ts | 8 +++- .../src/store/reducer.ts | 2 + .../test/setup-store.tsx | 8 +++- 6 files changed, 56 insertions(+), 35 deletions(-) diff --git a/packages/compass-data-modeling/src/components/diagram-editor.tsx b/packages/compass-data-modeling/src/components/diagram-editor.tsx index a6128564a9e..0fe597fafdc 100644 --- a/packages/compass-data-modeling/src/components/diagram-editor.tsx +++ b/packages/compass-data-modeling/src/components/diagram-editor.tsx @@ -128,7 +128,6 @@ const DiagramContent: React.FunctionComponent<{ isNewlyCreatedDiagram?: boolean; model: StaticModel | null; isInRelationshipDrawingMode: boolean; - editErrors?: string[]; newCollection?: string; onAddFieldToObjectField: (ns: string, parentPath: string[]) => void; onAddNewFieldToCollection: (ns: string) => void; @@ -526,7 +525,6 @@ export default connect( const { diagram, step } = state; return { step: step, - editErrors: diagram?.editErrors, diagramId: diagram?.id, }; }, diff --git a/packages/compass-data-modeling/src/store/diagram.spec.ts b/packages/compass-data-modeling/src/store/diagram.spec.ts index f17ae1d7bff..5c7d1f99bb5 100644 --- a/packages/compass-data-modeling/src/store/diagram.spec.ts +++ b/packages/compass-data-modeling/src/store/diagram.spec.ts @@ -18,6 +18,7 @@ import type { StaticModel, } from '../services/data-model-storage'; import { UUID } from 'bson'; +import Sinon from 'sinon'; const model: StaticModel = { collections: [ @@ -67,9 +68,11 @@ const loadedDiagram: MongoDBDataModelDescription = { describe('Data Modeling store', function () { let store: DataModelingStore; + let openToastSpy: Sinon.SinonSpy; beforeEach(function () { - store = setupStore(); + openToastSpy = Sinon.spy(); + store = setupStore({}, undefined, openToastSpy); }); describe('New Diagram', function () { @@ -157,7 +160,7 @@ describe('Data Modeling store', function () { const state = store.getState(); const diagram = getCurrentDiagramFromState(state); - expect(state.diagram?.editErrors).to.be.undefined; + expect(openToastSpy).not.to.have.been.called; expect(diagram.edits).to.have.length(2); expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]); expect(diagram.edits[1]).to.deep.include(edit); @@ -193,7 +196,7 @@ describe('Data Modeling store', function () { const state = store.getState(); const diagram = getCurrentDiagramFromState(state); - expect(state.diagram?.editErrors).to.be.undefined; + expect(openToastSpy).not.to.have.been.called; expect(diagram.edits).to.have.length(2); expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]); expect(diagram.edits[1]).to.deep.include({ @@ -217,9 +220,8 @@ describe('Data Modeling store', function () { } as unknown as Edit; store.dispatch(applyEdit(edit)); - const editErrors = store.getState().diagram?.editErrors; - expect(editErrors).to.have.length(1); - expect(editErrors && editErrors[0]).to.equal( + expect(openToastSpy).to.have.been.calledOnce; + expect(openToastSpy.firstCall.args[1].description).to.include( "'relationship,relationship' is required" ); const diagram = getCurrentDiagramFromState(store.getState()); @@ -353,7 +355,7 @@ describe('Data Modeling store', function () { const state = store.getState(); const diagram = getCurrentDiagramFromState(state); - expect(state.diagram?.editErrors).to.be.undefined; + expect(openToastSpy).not.to.have.been.called; expect(diagram.edits).to.have.length(2); expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]); expect(diagram.edits[1]).to.deep.include(edit); @@ -373,12 +375,22 @@ describe('Data Modeling store', function () { } as unknown as Edit; store.dispatch(applyEdit(edit)); - const editErrors = store.getState().diagram?.editErrors; - expect(editErrors).to.have.length(1); - expect(editErrors && editErrors[0]).to.equal("'newPosition' is required"); + expect(openToastSpy).to.have.been.calledOnce; + expect(openToastSpy.firstCall.args[1].description).to.include( + "'newPosition' is required" + ); const diagram = getCurrentDiagramFromState(store.getState()); expect(diagram.edits).to.deep.equal(loadedDiagram.edits); }); + + it('should handle an invalid RenameCollection edit', function () { + store.dispatch(openDiagram(loadedDiagram)); + store.dispatch(renameCollection('nonExisting', 'newName')); + expect(openToastSpy).to.have.been.calledOnce; + expect(openToastSpy.firstCall.args[1].description).to.include( + "Collection 'nonExisting' not found" + ); + }); }); it('undo & redo', function () { diff --git a/packages/compass-data-modeling/src/store/diagram.ts b/packages/compass-data-modeling/src/store/diagram.ts index 0ead589f1f6..dfe9f402c29 100644 --- a/packages/compass-data-modeling/src/store/diagram.ts +++ b/packages/compass-data-modeling/src/store/diagram.ts @@ -18,7 +18,7 @@ import { memoize } from 'lodash'; import type { DataModelingState, DataModelingThunkAction } from './reducer'; import { getCoordinatesForNewNode, - openToast, + type openToast as _openToast, showConfirmation, showPrompt, } from '@mongodb-js/compass-components'; @@ -225,7 +225,6 @@ export const diagramReducer: Reducer = ( state.draftCollection, action.edit.toNS ), - editErrors: undefined, updatedAt: new Date().toISOString(), selectedItems: { type: 'collection', @@ -242,7 +241,6 @@ export const diagramReducer: Reducer = ( current: [...state.edits.current, action.edit] as [Edit, ...Edit[]], next: [], }, - editErrors: undefined, updatedAt: new Date().toISOString(), selectedItems: updateSelectedItemsFromAppliedEdit( state.selectedItems, @@ -501,7 +499,7 @@ export function redoEdit(): DataModelingThunkAction { export function onAddNestedField( ns: string, parentFieldPath: string[] -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState) => { const modelState = selectCurrentModelFromState(getState()); @@ -532,7 +530,7 @@ export function onAddNestedField( export function addNewFieldToCollection( ns: string -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState) => { const modelState = selectCurrentModelFromState(getState()); @@ -561,7 +559,7 @@ export function addNewFieldToCollection( export function moveCollection( ns: string, newPosition: [number, number] -): DataModelingThunkAction { +): DataModelingThunkAction { const edit: Omit< Extract, 'id' | 'timestamp' @@ -578,7 +576,7 @@ export function renameCollection( toNS: string ): DataModelingThunkAction< void, - ApplyEditAction | ApplyEditFailedAction | CollectionSelectedAction + ApplyEditAction | RevertFailedEditAction | CollectionSelectedAction > { const edit: Omit< Extract, @@ -592,7 +590,7 @@ export function renameCollection( return applyEdit(edit); } -function handleError(messages: string[]) { +function handleError(openToast: typeof _openToast, messages: string[]) { openToast('data-modeling-error', { variant: 'warning', title: 'Error opening diagram', @@ -600,10 +598,13 @@ function handleError(messages: string[]) { }); } +/** + * Not intended to be called directly, only exported for testing. + */ export function applyEdit( rawEdit: EditAction ): DataModelingThunkAction { - return (dispatch, getState, { dataModelStorage }) => { + return (dispatch, getState, { dataModelStorage, openToast }) => { const edit = { ...rawEdit, id: new UUID().toString(), @@ -612,7 +613,7 @@ export function applyEdit( const { result, errors } = validateEdit(edit); let isValid = result; if (!result) { - handleError(errors); + handleError(openToast, errors); return isValid; } dispatch({ @@ -624,7 +625,7 @@ export function applyEdit( try { selectCurrentModelFromState(getState()); } catch (e) { - handleError([ + handleError(openToast, [ 'Something went wrong when applying the changes.', (e as Error).message, ]); @@ -689,7 +690,7 @@ export function renameDiagram( export function openDiagramFromFile( file: File ): DataModelingThunkAction, OpenDiagramAction> { - return async (dispatch, getState, { dataModelStorage, track }) => { + return async (dispatch, getState, { dataModelStorage, track, openToast }) => { try { const { name, edits } = await getDiagramContentsFromFile(file); @@ -720,7 +721,7 @@ export function openDiagramFromFile( export function updateRelationship( relationship: Relationship -): DataModelingThunkAction { +): DataModelingThunkAction { return applyEdit({ type: 'UpdateRelationship', relationship, @@ -748,7 +749,7 @@ export function deleteRelationship( export function deleteCollection( ns: string -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState, { track }) => { track('Data Modeling Collection Removed', { source: 'side_panel', @@ -761,14 +762,14 @@ export function deleteCollection( export function updateCollectionNote( ns: string, note: string -): DataModelingThunkAction { +): DataModelingThunkAction { return applyEdit({ type: 'UpdateCollectionNote', ns, note }); } export function removeField( ns: string, field: FieldPath -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState, { track }) => { track('Data Modeling Field Removed', { source: 'side_panel', @@ -782,7 +783,7 @@ export function renameField( ns: string, field: FieldPath, newName: string -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState, { track }) => { track('Data Modeling Field Renamed', { source: 'side_panel', @@ -818,7 +819,7 @@ export function changeFieldType( ns: string, fieldPath: FieldPath, newTypes: string[] -): DataModelingThunkAction { +): DataModelingThunkAction { return (dispatch, getState, { track }) => { const collectionSchema = selectCurrentModelFromState( getState() @@ -886,7 +887,7 @@ export function addCollection( position?: [number, number] ): DataModelingThunkAction< void, - ApplyEditAction | ApplyEditFailedAction | CollectionSelectedAction + ApplyEditAction | RevertFailedEditAction | CollectionSelectedAction > { return (dispatch, getState, { track }) => { const existingCollections = selectCurrentModelFromState( diff --git a/packages/compass-data-modeling/src/store/index.ts b/packages/compass-data-modeling/src/store/index.ts index eb36b8b91a2..0896eddea4a 100644 --- a/packages/compass-data-modeling/src/store/index.ts +++ b/packages/compass-data-modeling/src/store/index.ts @@ -9,8 +9,11 @@ import reducer from './reducer'; import type { DataModelingExtraArgs } from './reducer'; import thunk from 'redux-thunk'; import type { ActivateHelpers } from '@mongodb-js/compass-app-registry'; +import { openToast as _openToast } from '@mongodb-js/compass-components'; -export type DataModelingStoreOptions = Record; +export type DataModelingStoreOptions = { + openToast?: typeof _openToast; +}; export type DataModelingStoreServices = { preferences: PreferencesAccess; @@ -22,7 +25,7 @@ export type DataModelingStoreServices = { }; export function activateDataModelingStore( - _: DataModelingStoreOptions, + { openToast = _openToast }: DataModelingStoreOptions, services: DataModelingStoreServices, { cleanup }: ActivateHelpers ) { @@ -35,6 +38,7 @@ export function activateDataModelingStore( ...services, cancelAnalysisControllerRef, cancelExportControllerRef, + openToast, }) ) ); diff --git a/packages/compass-data-modeling/src/store/reducer.ts b/packages/compass-data-modeling/src/store/reducer.ts index d7de974b1b2..2dafb926e99 100644 --- a/packages/compass-data-modeling/src/store/reducer.ts +++ b/packages/compass-data-modeling/src/store/reducer.ts @@ -20,6 +20,7 @@ import type { ExportDiagramActions, } from './export-diagram'; import { exportDiagramReducer } from './export-diagram'; +import { type openToast as _openToast } from '@mongodb-js/compass-components'; const reducer = combineReducers({ step: stepReducer, @@ -46,6 +47,7 @@ export type DataModelingState = ReturnType; export type DataModelingExtraArgs = DataModelingStoreServices & { cancelAnalysisControllerRef: { current: AbortController | null }; cancelExportControllerRef: { current: AbortController | null }; + openToast: typeof _openToast; }; export type DataModelingThunkAction = ThunkAction< diff --git a/packages/compass-data-modeling/test/setup-store.tsx b/packages/compass-data-modeling/test/setup-store.tsx index ae36d9d4eec..1b0d89ea509 100644 --- a/packages/compass-data-modeling/test/setup-store.tsx +++ b/packages/compass-data-modeling/test/setup-store.tsx @@ -10,6 +10,7 @@ import { activateDataModelingStore } from '../src/store'; import type { DataModelingStoreServices } from '../src/store'; import { noopDataModelStorageService } from '../src/provider'; import { Provider } from 'react-redux'; +import { openToast as _openToast } from '@mongodb-js/compass-components'; type ConnectionInfoWithMockData = ConnectionInfo & { databases: Array<{ @@ -136,10 +137,13 @@ const testConnections = [ export const setupStore = ( services: Partial = {}, - connections: ConnectionInfoWithMockData[] = testConnections + connections: ConnectionInfoWithMockData[] = testConnections, + openToast: typeof _openToast = _openToast ) => { return activateDataModelingStore( - {}, + { + openToast, + }, { logger: createNoopLogger('TEST'), track: createNoopTrack(), From ca66727d1bde1a16f236b47efd78e34f63ba973b Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Fri, 17 Oct 2025 12:34:19 +0200 Subject: [PATCH 4/6] Update packages/compass-data-modeling/src/store/diagram.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- packages/compass-data-modeling/src/store/diagram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compass-data-modeling/src/store/diagram.ts b/packages/compass-data-modeling/src/store/diagram.ts index dfe9f402c29..dc8e0d7f085 100644 --- a/packages/compass-data-modeling/src/store/diagram.ts +++ b/packages/compass-data-modeling/src/store/diagram.ts @@ -259,7 +259,7 @@ export const diagramReducer: Reducer = ( return state; } const next = isAction(action, DiagramActionTypes.REVERT_FAILED_EDIT) - ? [...state.edits.next] + ? state.edits.next : [...state.edits.next, state.edits.current]; return { ...state, From a6fbc146a213a4f8820aa50e1916bd525b95a6b5 Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Fri, 17 Oct 2025 13:07:48 +0200 Subject: [PATCH 5/6] cleanup --- .../src/store/diagram.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/compass-data-modeling/src/store/diagram.ts b/packages/compass-data-modeling/src/store/diagram.ts index dc8e0d7f085..cc8e4c85ab2 100644 --- a/packages/compass-data-modeling/src/store/diagram.ts +++ b/packages/compass-data-modeling/src/store/diagram.ts @@ -590,10 +590,14 @@ export function renameCollection( return applyEdit(edit); } -function handleError(openToast: typeof _openToast, messages: string[]) { +function handleError( + openToast: typeof _openToast, + title: string, + messages: string[] +) { openToast('data-modeling-error', { variant: 'warning', - title: 'Error opening diagram', + title, description: messages.join(' '), }); } @@ -613,7 +617,7 @@ export function applyEdit( const { result, errors } = validateEdit(edit); let isValid = result; if (!result) { - handleError(openToast, errors); + handleError(openToast, 'Could not apply changes', errors); return isValid; } dispatch({ @@ -625,7 +629,7 @@ export function applyEdit( try { selectCurrentModelFromState(getState()); } catch (e) { - handleError(openToast, [ + handleError(openToast, 'Could not apply changes', [ 'Something went wrong when applying the changes.', (e as Error).message, ]); @@ -710,11 +714,9 @@ export function openDiagramFromFile( track('Data Modeling Diagram Imported', {}); void dataModelStorage.save(diagram); } catch (error) { - openToast('data-modeling-file-read-error', { - variant: 'warning', - title: 'Error opening diagram', - description: (error as Error).message, - }); + handleError(openToast, 'Error opening diagram', [ + (error as Error).message, + ]); } }; } From 6f119b01701e275b186619a546ffab52b2adbb8e Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Fri, 17 Oct 2025 16:40:50 +0200 Subject: [PATCH 6/6] rename fn --- .../src/store/apply-edit.ts | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/compass-data-modeling/src/store/apply-edit.ts b/packages/compass-data-modeling/src/store/apply-edit.ts index 68c7a320284..0baa0846d55 100644 --- a/packages/compass-data-modeling/src/store/apply-edit.ts +++ b/packages/compass-data-modeling/src/store/apply-edit.ts @@ -32,7 +32,12 @@ function renameFieldInRelationshipSide( }; } -function ensureCollectionExists( +/** + * @param collections + * @param ns + * @throws Will throw an error if the namespace is not found in the collections. + */ +function assertCollectionExists( collections: DataModelCollection[], ns: string ): void { @@ -91,7 +96,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'MoveCollection': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { @@ -106,7 +111,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RemoveCollection': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, // Remove any relationships involving the collection being removed. @@ -121,7 +126,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RenameCollection': { - ensureCollectionExists(model.collections, edit.fromNS); + assertCollectionExists(model.collections, edit.fromNS); return { ...model, // Update relationships to point to the renamed namespace. @@ -150,7 +155,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'UpdateCollectionNote': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { @@ -165,7 +170,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'AddField': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => { @@ -184,7 +189,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RemoveField': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, // Remove any relationships involving the field being removed. @@ -209,7 +214,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'RenameField': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, // Update any relationships involving the field being renamed. @@ -244,7 +249,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel { }; } case 'ChangeFieldType': { - ensureCollectionExists(model.collections, edit.ns); + assertCollectionExists(model.collections, edit.ns); return { ...model, collections: model.collections.map((collection) => {