Skip to content

Commit 78dd30c

Browse files
committed
fix(compass-user-data): remove divergent user data types from atlas implementation CLOUDP-334901
1 parent d0f73a8 commit 78dd30c

File tree

10 files changed

+337
-68
lines changed

10 files changed

+337
-68
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-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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
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) {
910
// TODO: https://jira.mongodb.org/browse/COMPASS-7080
10-
this.userData = new FileUserData(z.string().array(), getAppName() ?? '', {
11+
this.userData = new FileUserData(z.string().array(), 'ShellHistory', {
1112
basePath,
1213
});
1314
}
1415

16+
/**
17+
* Migrates history from old app-name-based folder to new ShellHistory folder.
18+
* Only runs once per instance and only if old folder exists.
19+
*/
20+
private async migrateIfNeeded(): Promise<void> {
21+
if (this.migrationChecked) {
22+
return;
23+
}
24+
this.migrationChecked = true;
25+
26+
const oldAppName = getAppName();
27+
if (oldAppName && oldAppName !== 'ShellHistory') {
28+
await this.userData.migrateFromOldFolder(oldAppName);
29+
}
30+
}
31+
1532
/**
1633
* Saves the history to disk, it creates the directory and the file if
1734
* not existing and replaces the file content.
@@ -20,6 +37,7 @@ export class HistoryStorage {
2037
* newest to oldest.
2138
*/
2239
async save(history: string[]) {
40+
await this.migrateIfNeeded();
2341
await this.userData.write(this.fileName, history);
2442
}
2543

@@ -31,6 +49,7 @@ export class HistoryStorage {
3149
* newest to oldest.
3250
*/
3351
async load(): Promise<string[]> {
52+
await this.migrateIfNeeded();
3453
try {
3554
return (await this.userData.readOne(this.fileName)) ?? [];
3655
} 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)