Skip to content

Commit 7cdd0cb

Browse files
authored
fix(compass-user-data): remove divergent user data types from atlas implementation CLOUDP-334901 (#7543)
1 parent 67fb453 commit 7cdd0cb

File tree

13 files changed

+345
-85
lines changed

13 files changed

+345
-85
lines changed

packages/atlas-service/src/atlas-service.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import type { Logger } from '@mongodb-js/compass-logging';
1010
import type { PreferencesAccess } from 'compass-preferences-model';
1111
import type { AtlasClusterMetadata } from '@mongodb-js/connection-info';
12+
import { type UserDataType } from '@mongodb-js/compass-user-data';
1213

1314
export type AtlasServiceOptions = {
1415
defaultHeaders?: Record<string, string>;
@@ -81,12 +82,7 @@ export class AtlasService {
8182
userDataEndpoint(
8283
orgId: string,
8384
groupId: string,
84-
type:
85-
| 'favoriteQueries'
86-
| 'recentQueries'
87-
| 'favoriteAggregations'
88-
| 'savedWorkspaces'
89-
| 'dataModelDescriptions',
85+
type: UserDataType,
9086
id?: string
9187
): string {
9288
const encodedOrgId = encodeURIComponent(orgId);

packages/compass-data-modeling/src/services/data-model-storage-web.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DataModelStorageAtlas implements DataModelStorage {
2121
constructor(orgId: string, projectId: string, atlasService: AtlasService) {
2222
this.userData = new AtlasUserData(
2323
MongoDBDataModelDescriptionSchema,
24-
'dataModelDescriptions',
24+
'DataModelDescriptions',
2525
{
2626
orgId,
2727
projectId,
@@ -37,7 +37,7 @@ class DataModelStorageAtlas implements DataModelStorage {
3737
!type ||
3838
!pathOrgId ||
3939
!pathProjectId ||
40-
type !== 'dataModelDescriptions'
40+
type !== 'DataModelDescriptions'
4141
) {
4242
throw new Error(
4343
'DataModelStorageAtlas is used outside of Atlas Cloud context'

packages/compass-e2e-tests/tests/collection-import.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ describe('Collection import', function () {
643643
// Find the log file
644644
const logFilePath = path.resolve(
645645
compass.userDataPath || '',
646-
compass.appName || '',
647646
'ImportErrorLogs',
648647
`import-${filename}.log`
649648
);
@@ -1293,7 +1292,6 @@ describe('Collection import', function () {
12931292

12941293
const logFilePath = path.resolve(
12951294
compass.userDataPath || '',
1296-
compass.appName || '',
12971295
'ImportErrorLogs',
12981296
`import-${fileName}.log`
12991297
);

packages/compass-import-export/src/modules/import.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { analyzeCSVFields } from '../import/analyze-csv-fields';
2121
import type { AnalyzeCSVFieldsResult } from '../import/analyze-csv-fields';
2222
import { importCSV } from '../import/import-csv';
2323
import { importJSON } from '../import/import-json';
24-
import { getUserDataFolderPath } from '../utils/get-user-data-file-path';
24+
import { getStoragePath } from '@mongodb-js/compass-utils';
2525
import {
2626
showBloatedDocumentSignalToast,
2727
showUnboundArraySignalToast,
@@ -177,6 +177,14 @@ const onFileSelectError = (error: Error) => ({
177177
error,
178178
});
179179

180+
export function getUserDataFolderPath() {
181+
const basepath = getStoragePath();
182+
if (basepath === undefined) {
183+
throw new Error('cannot access user data folder path');
184+
}
185+
return basepath;
186+
}
187+
180188
async function getErrorLogPath(fileName: string) {
181189
// Create the error log output file.
182190
const userDataPath = getUserDataFolderPath();

packages/compass-import-export/src/utils/get-user-data-file-path.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/compass-shell/src/modules/history-storage.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import fs from 'fs/promises';
66
import { HistoryStorage } from './history-storage';
77

88
describe('HistoryStorage', function () {
9-
let tmpDir;
10-
let historyFilePath;
9+
let tmpDir: string;
10+
let historyFilePath: string;
1111

12-
let historyStorage;
12+
let historyStorage: HistoryStorage;
1313

1414
beforeEach(async function () {
1515
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'compass-shell-test'));
16-
historyFilePath = path.join(tmpDir, 'shell-history.json');
16+
historyFilePath = path.join(tmpDir, 'ShellHistory', 'shell-history.json');
1717

1818
historyStorage = new HistoryStorage(tmpDir);
1919
});

packages/compass-shell/src/modules/history-storage.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
import { getAppName } from '@mongodb-js/compass-utils';
21
import { FileUserData, z } from '@mongodb-js/compass-user-data';
2+
import { getAppName } from '@mongodb-js/compass-utils';
33

44
export class HistoryStorage {
55
fileName = 'shell-history';
66
userData;
7+
private migrationChecked = false;
78

89
constructor(basePath?: string) {
9-
// TODO: https://jira.mongodb.org/browse/COMPASS-7080
10-
this.userData = new FileUserData(z.string().array(), getAppName() ?? '', {
10+
this.userData = new FileUserData(z.string().array(), 'ShellHistory', {
1111
basePath,
1212
});
1313
}
1414

15+
/**
16+
* Migrates history from old app-name-based folder to new ShellHistory folder.
17+
* Only runs once per instance and only if old folder exists.
18+
*/
19+
private async migrateIfNeeded(): Promise<void> {
20+
if (this.migrationChecked) {
21+
return;
22+
}
23+
this.migrationChecked = true;
24+
25+
const oldAppName = getAppName();
26+
if (oldAppName) {
27+
await this.userData.migrateFromOldFolder(oldAppName);
28+
}
29+
}
30+
1531
/**
1632
* Saves the history to disk, it creates the directory and the file if
1733
* not existing and replaces the file content.
@@ -20,6 +36,7 @@ export class HistoryStorage {
2036
* newest to oldest.
2137
*/
2238
async save(history: string[]) {
39+
await this.migrateIfNeeded();
2340
await this.userData.write(this.fileName, history);
2441
}
2542

@@ -31,6 +48,7 @@ export class HistoryStorage {
3148
* newest to oldest.
3249
*/
3350
async load(): Promise<string[]> {
51+
await this.migrateIfNeeded();
3452
try {
3553
return (await this.userData.readOne(this.fileName)) ?? [];
3654
} catch {
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
export type { ReadAllResult } from './user-data';
2-
export { IUserData, FileUserData, AtlasUserData } from './user-data';
1+
export type { ReadAllResult, UserDataType } from './user-data';
2+
export {
3+
IUserData,
4+
FileUserData,
5+
AtlasUserData,
6+
assertsUserDataType,
7+
} from './user-data';
38
export { z } from 'zod';

0 commit comments

Comments
 (0)