Skip to content

Commit 7b69069

Browse files
committed
merg main
2 parents 69b3624 + 396c151 commit 7b69069

File tree

7 files changed

+69
-89
lines changed

7 files changed

+69
-89
lines changed

packages/compass-schema/src/components/schema-toolbar/schema-toolbar.spec.tsx

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import React from 'react';
33
import { render, screen } from '@mongodb-js/testing-library-compass';
44
import { expect } from 'chai';
55
import sinon from 'sinon';
6-
import {
7-
createSandboxFromDefaultPreferences,
8-
type PreferencesAccess,
9-
} from 'compass-preferences-model';
10-
import { PreferencesProvider } from 'compass-preferences-model/provider';
6+
import type { AllPreferences } from 'compass-preferences-model';
117
import { SchemaToolbar } from './schema-toolbar';
128
import QueryBarPlugin from '@mongodb-js/compass-query-bar';
139
import {
@@ -35,33 +31,28 @@ const testErrorMessage =
3531
const exportSchemaTestId = 'open-schema-export-button';
3632

3733
describe('SchemaToolbar', function () {
38-
let defaultPreferences: PreferencesAccess;
39-
40-
before(async function () {
41-
defaultPreferences = await createSandboxFromDefaultPreferences();
42-
});
43-
4434
const renderSchemaToolbar = (
4535
props: Partial<ComponentProps<typeof SchemaToolbar>> = {},
46-
preferences: PreferencesAccess = defaultPreferences
36+
preferences: Partial<AllPreferences> = {}
4737
) => {
4838
const queryBarProps = {};
4939
render(
50-
<PreferencesProvider value={preferences}>
51-
<MockQueryBarPlugin {...(queryBarProps as any)}>
52-
<SchemaToolbar
53-
analysisState="complete"
54-
errorMessage={''}
55-
isOutdated={false}
56-
onAnalyzeSchemaClicked={() => {}}
57-
onResetClicked={() => {}}
58-
sampleSize={10}
59-
schemaResultId="123"
60-
onExportSchemaClicked={() => {}}
61-
{...props}
62-
/>
63-
</MockQueryBarPlugin>
64-
</PreferencesProvider>
40+
<MockQueryBarPlugin {...(queryBarProps as any)}>
41+
<SchemaToolbar
42+
analysisState="complete"
43+
errorMessage={''}
44+
isOutdated={false}
45+
onAnalyzeSchemaClicked={() => {}}
46+
onResetClicked={() => {}}
47+
sampleSize={10}
48+
schemaResultId="123"
49+
onExportSchemaClicked={() => {}}
50+
{...props}
51+
/>
52+
</MockQueryBarPlugin>,
53+
{
54+
preferences,
55+
}
6556
);
6657
};
6758

@@ -125,17 +116,14 @@ describe('SchemaToolbar', function () {
125116
});
126117

127118
describe('when rendered with the enableExportSchema feature flag true', function () {
128-
beforeEach(async function () {
129-
const preferences = await createSandboxFromDefaultPreferences();
130-
await preferences.savePreferences({
131-
enableExportSchema: true,
132-
});
133-
119+
beforeEach(function () {
134120
renderSchemaToolbar(
135121
{
136122
sampleSize: 100,
137123
},
138-
preferences
124+
{
125+
enableExportSchema: true,
126+
}
139127
);
140128
});
141129

packages/compass-schema/src/modules/schema-analysis.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('schema-analysis', function () {
192192
);
193193
});
194194

195-
it('returns null if is cancelled', async function () {
195+
it('returns undefined if is cancelled', async function () {
196196
const dataService = {
197197
sample: () => Promise.reject(new Error('test error')),
198198
isCancelError: () => true,
@@ -210,7 +210,7 @@ describe('schema-analysis', function () {
210210
dummyLogger
211211
);
212212

213-
expect(result).to.equal(null);
213+
expect(result).to.equal(undefined);
214214
});
215215

216216
it('throws if sample throws', async function () {

packages/compass-schema/src/modules/schema-analysis.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AggregateOptions, Filter, Document } from 'mongodb';
22
import { analyzeDocuments } from 'mongodb-schema';
33
import type {
44
Schema,
5+
SchemaAccessor,
56
ArraySchemaType,
67
DocumentSchemaType,
78
SchemaField,
@@ -25,8 +26,6 @@ function promoteMongoErrorCode(err?: Error & { code?: unknown }) {
2526
return err;
2627
}
2728

28-
export type SchemaAccessor = Awaited<ReturnType<typeof analyzeDocuments>>;
29-
3029
export const analyzeSchema = async (
3130
dataService: DataService,
3231
abortSignal: AbortSignal,
@@ -40,7 +39,7 @@ export const analyzeSchema = async (
4039
| undefined,
4140
aggregateOptions: AggregateOptions,
4241
{ log, mongoLogId, debug }: Logger
43-
): Promise<SchemaAccessor | null> => {
42+
): Promise<SchemaAccessor | undefined> => {
4443
try {
4544
log.info(mongoLogId(1001000089), 'Schema', 'Starting schema analysis', {
4645
ns,
@@ -72,7 +71,7 @@ export const analyzeSchema = async (
7271
});
7372
if (dataService.isCancelError(err)) {
7473
debug('caught background operation terminated error', err);
75-
return null;
74+
return;
7675
}
7776

7877
const error = promoteMongoErrorCode(err);

packages/compass-schema/src/stores/schema-analysis-reducer.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { addLayer, generateGeoQuery } from '../modules/geo';
1515
import {
1616
analyzeSchema,
1717
calculateSchemaMetadata,
18-
type SchemaAccessor,
1918
} from '../modules/schema-analysis';
2019
import { capMaxTimeMSAtPreferenceLimit } from 'compass-preferences-model/provider';
2120
import type { Circle, Layer, LayerGroup, Polygon } from 'leaflet';
@@ -32,7 +31,6 @@ export type SchemaAnalysisState = {
3231
analysisState: AnalysisState;
3332
errorMessage: string;
3433
schema: Schema | null;
35-
schemaAccessor: SchemaAccessor | null;
3634
resultId: string;
3735
};
3836

@@ -48,7 +46,6 @@ export type AnalysisStartedAction = {
4846

4947
export type AnalysisFinishedAction = {
5048
type: SchemaAnalysisActions.analysisFinished;
51-
schemaAccessor: SchemaAccessor | null;
5249
schema: Schema | null;
5350
};
5451

@@ -72,7 +69,6 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
7269
analysisState: ANALYSIS_STATE_ANALYZING,
7370
errorMessage: '',
7471
schema: null,
75-
schemaAccessor: null,
7672
};
7773
}
7874

@@ -88,7 +84,6 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
8884
? ANALYSIS_STATE_COMPLETE
8985
: ANALYSIS_STATE_INITIAL,
9086
schema: action.schema,
91-
schemaAccessor: action.schemaAccessor,
9287
resultId: resultId(),
9388
};
9489
}
@@ -129,7 +124,6 @@ const getInitialState = (): SchemaAnalysisState => ({
129124
analysisState: ANALYSIS_STATE_INITIAL,
130125
errorMessage: '',
131126
schema: null,
132-
schemaAccessor: null,
133127
resultId: resultId(),
134128
});
135129

@@ -178,9 +172,9 @@ const userCancelledAnalysisAbortReason = 'Cancelled analysis';
178172
export const stopAnalysis = (
179173
userCancelled = false
180174
): SchemaThunkAction<void> => {
181-
return (dispatch, getState, { abortControllerRef }) => {
182-
if (!abortControllerRef.current) return;
183-
abortControllerRef.current?.abort(
175+
return (dispatch, getState, { analysisAbortControllerRef }) => {
176+
if (!analysisAbortControllerRef.current) return;
177+
analysisAbortControllerRef.current?.abort(
184178
userCancelled ? userCancelledAnalysisAbortReason : undefined
185179
);
186180
};
@@ -239,7 +233,8 @@ export const startAnalysis = (): SchemaThunkAction<
239233
dataService,
240234
logger,
241235
fieldStoreService,
242-
abortControllerRef,
236+
analysisAbortControllerRef,
237+
schemaAccessorRef,
243238
namespace,
244239
geoLayersRef,
245240
connectionInfoRef,
@@ -269,8 +264,8 @@ export const startAnalysis = (): SchemaThunkAction<
269264
maxTimeMS: capMaxTimeMSAtPreferenceLimit(preferences, query.maxTimeMS),
270265
};
271266

272-
abortControllerRef.current = new AbortController();
273-
const abortSignal = abortControllerRef.current.signal;
267+
analysisAbortControllerRef.current = new AbortController();
268+
const abortSignal = analysisAbortControllerRef.current.signal;
274269

275270
const analysisStartTime = Date.now();
276271
try {
@@ -286,11 +281,11 @@ export const startAnalysis = (): SchemaThunkAction<
286281
driverOptions,
287282
logger
288283
);
289-
290284
if (abortSignal?.aborted) {
291285
throw new Error(abortSignal?.reason || new Error('Operation aborted'));
292286
}
293287

288+
schemaAccessorRef.current = schemaAccessor;
294289
let schema: Schema | null = null;
295290
if (schemaAccessor) {
296291
schema = await schemaAccessor.getInternalSchema();
@@ -307,7 +302,6 @@ export const startAnalysis = (): SchemaThunkAction<
307302
dispatch({
308303
type: SchemaAnalysisActions.analysisFinished,
309304
schema,
310-
schemaAccessor,
311305
});
312306

313307
track(
@@ -350,7 +344,7 @@ export const startAnalysis = (): SchemaThunkAction<
350344
error: err as Error,
351345
});
352346
} finally {
353-
abortControllerRef.current = undefined;
347+
analysisAbortControllerRef.current = undefined;
354348
}
355349
};
356350
};

packages/compass-schema/src/stores/schema-export-reducer.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import type {
55
InternalSchema,
66
MongoDBJSONSchema,
77
ExpandedJSONSchema,
8+
SchemaAccessor,
89
} from 'mongodb-schema';
910
import { openToast } from '@mongodb-js/compass-components';
1011

1112
import type { SchemaThunkAction } from './store';
1213
import { isAction } from '../utils';
13-
import {
14-
calculateSchemaMetadata,
15-
type SchemaAccessor,
16-
} from '../modules/schema-analysis';
14+
import { calculateSchemaMetadata } from '../modules/schema-analysis';
1715

1816
export type SchemaFormat =
1917
| 'standardJSON'
@@ -22,7 +20,6 @@ export type SchemaFormat =
2220
| 'legacyJSON';
2321
export type ExportStatus = 'inprogress' | 'complete' | 'error';
2422
export type SchemaExportState = {
25-
abortController?: AbortController;
2623
isOpen: boolean;
2724
isLegacyBannerOpen: boolean;
2825
legacyBannerChoice?: 'legacy' | 'export';
@@ -80,11 +77,8 @@ export const closeExportSchema = (): SchemaThunkAction<
8077
void,
8178
CloseExportSchemaAction
8279
> => {
83-
return (dispatch, getState) => {
84-
const {
85-
schemaExport: { abortController },
86-
} = getState();
87-
abortController?.abort();
80+
return (dispatch, getState, { exportAbortControllerRef }) => {
81+
exportAbortControllerRef.current?.abort();
8882

8983
return dispatch({
9084
type: SchemaExportActions.closeExportSchema,
@@ -98,7 +92,6 @@ export type CancelExportSchemaAction = {
9892

9993
export type ChangeExportSchemaFormatStartedAction = {
10094
type: SchemaExportActions.changeExportSchemaFormatStarted;
101-
abortController: AbortController;
10295
exportFormat: SchemaFormat;
10396
};
10497

@@ -116,11 +109,8 @@ export const cancelExportSchema = (): SchemaThunkAction<
116109
void,
117110
CancelExportSchemaAction
118111
> => {
119-
return (dispatch, getState) => {
120-
const {
121-
schemaExport: { abortController },
122-
} = getState();
123-
abortController?.abort();
112+
return (dispatch, getState, { exportAbortControllerRef }) => {
113+
exportAbortControllerRef.current?.abort();
124114

125115
return dispatch({
126116
type: SchemaExportActions.cancelExportSchema,
@@ -219,19 +209,19 @@ export const changeExportSchemaFormat = (
219209
| ChangeExportSchemaFormatErroredAction
220210
| ChangeExportSchemaFormatCompletedAction
221211
> => {
222-
return async (dispatch, getState, { logger: { log } }) => {
223-
const {
224-
schemaExport: { abortController: existingAbortController },
225-
} = getState();
226-
212+
return async (
213+
dispatch,
214+
getState,
215+
{ logger: { log }, exportAbortControllerRef, schemaAccessorRef }
216+
) => {
227217
// If we're already in progress we abort their current operation.
228-
existingAbortController?.abort();
218+
exportAbortControllerRef.current?.abort();
229219

230-
const abortController = new AbortController();
220+
exportAbortControllerRef.current = new AbortController();
221+
const abortSignal = exportAbortControllerRef.current.signal;
231222

232223
dispatch({
233224
type: SchemaExportActions.changeExportSchemaFormatStarted,
234-
abortController,
235225
exportFormat,
236226
});
237227

@@ -244,21 +234,20 @@ export const changeExportSchemaFormat = (
244234
}
245235
);
246236

247-
const { schemaAccessor } = getState().schemaAnalysis;
248-
249237
let exportedSchema: string;
250238
try {
239+
const schemaAccessor = schemaAccessorRef.current;
251240
if (!schemaAccessor) {
252241
throw new Error('No schema analysis available');
253242
}
254243

255244
exportedSchema = await getSchemaByFormat({
256245
schemaAccessor,
257246
exportFormat,
258-
signal: abortController.signal,
247+
signal: abortSignal,
259248
});
260249
} catch (err: any) {
261-
if (abortController.signal.aborted) {
250+
if (abortSignal.aborted) {
262251
return;
263252
}
264253
log.error(
@@ -277,7 +266,7 @@ export const changeExportSchemaFormat = (
277266
return;
278267
}
279268

280-
if (abortController.signal.aborted) {
269+
if (abortSignal.aborted) {
281270
return;
282271
}
283272

@@ -370,7 +359,6 @@ export const schemaExportReducer: Reducer<SchemaExportState, Action> = (
370359
) {
371360
return {
372361
...state,
373-
abortController: action.abortController,
374362
exportStatus: 'inprogress',
375363
exportFormat: action.exportFormat,
376364
};

packages/compass-schema/src/stores/store.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('Schema Store', function () {
129129
store.dispatch(stopAnalysis());
130130
isCancelErrorStub.returns(true);
131131
await analysisPromise;
132-
expect(store.getState().schemaAnalysis.analysisState).to.equal('initial');
132+
expect(store.getState().schemaAnalysis.analysisState).to.equal('error');
133133
});
134134

135135
describe('schema export', function () {

0 commit comments

Comments
 (0)