Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/atlas-service/src/atlas-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import type { Logger } from '@mongodb-js/compass-logging';
import type { PreferencesAccess } from 'compass-preferences-model';
import type { AtlasClusterMetadata } from '@mongodb-js/connection-info';
import { type UserDataType } from '@mongodb-js/compass-user-data';

export type AtlasServiceOptions = {
defaultHeaders?: Record<string, string>;
Expand Down Expand Up @@ -81,12 +82,7 @@ export class AtlasService {
userDataEndpoint(
orgId: string,
groupId: string,
type:
| 'favoriteQueries'
| 'recentQueries'
| 'favoriteAggregations'
| 'savedWorkspaces'
| 'dataModelDescriptions',
type: UserDataType,
id?: string
): string {
const encodedOrgId = encodeURIComponent(orgId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DataModelStorageAtlas implements DataModelStorage {
constructor(orgId: string, projectId: string, atlasService: AtlasService) {
this.userData = new AtlasUserData(
MongoDBDataModelDescriptionSchema,
'dataModelDescriptions',
'DataModelDescriptions',
{
orgId,
projectId,
Expand All @@ -37,7 +37,7 @@ class DataModelStorageAtlas implements DataModelStorage {
!type ||
!pathOrgId ||
!pathProjectId ||
type !== 'dataModelDescriptions'
type !== 'DataModelDescriptions'
) {
throw new Error(
'DataModelStorageAtlas is used outside of Atlas Cloud context'
Expand Down
8 changes: 4 additions & 4 deletions packages/compass-shell/src/modules/history-storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import fs from 'fs/promises';
import { HistoryStorage } from './history-storage';

describe('HistoryStorage', function () {
let tmpDir;
let historyFilePath;
let tmpDir: string;
let historyFilePath: string;

let historyStorage;
let historyStorage: HistoryStorage;

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

historyStorage = new HistoryStorage(tmpDir);
});
Expand Down
24 changes: 21 additions & 3 deletions packages/compass-shell/src/modules/history-storage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { getAppName } from '@mongodb-js/compass-utils';
import { FileUserData, z } from '@mongodb-js/compass-user-data';
import { getAppName } from '@mongodb-js/compass-utils';

export class HistoryStorage {
fileName = 'shell-history';
userData;
private migrationChecked = false;

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

/**
* Migrates history from old app-name-based folder to new ShellHistory folder.
* Only runs once per instance and only if old folder exists.
*/
private async migrateIfNeeded(): Promise<void> {
if (this.migrationChecked) {
return;
}
this.migrationChecked = true;

const oldAppName = getAppName();
if (oldAppName && oldAppName !== 'ShellHistory') {
await this.userData.migrateFromOldFolder(oldAppName);
}
Comment on lines 25 to 28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this condition is checking. This will always be true, appName is never going to be ShellHistory

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, was thinking very birds eye view of the file rename process, you're correct!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about instead throwing if the appName isn't present and a variable rename? A nit, not a blocker

Suggested change
const oldAppName = getAppName();
if (oldAppName && oldAppName !== 'ShellHistory') {
await this.userData.migrateFromOldFolder(oldAppName);
}
const appName = getAppName();
if (!appName) {
throw new Error('Cannot initialize shell history storage without app name');
}
// We used to store the shell-history in a folder like `MongoDB Compass/shell-history.json`.
await this.userData.migrateFromOldFolder(appName);

}

/**
* Saves the history to disk, it creates the directory and the file if
* not existing and replaces the file content.
Expand All @@ -20,6 +36,7 @@ export class HistoryStorage {
* newest to oldest.
*/
async save(history: string[]) {
await this.migrateIfNeeded();
await this.userData.write(this.fileName, history);
}

Expand All @@ -31,6 +48,7 @@ export class HistoryStorage {
* newest to oldest.
*/
async load(): Promise<string[]> {
await this.migrateIfNeeded();
try {
return (await this.userData.readOne(this.fileName)) ?? [];
} catch {
Expand Down
9 changes: 7 additions & 2 deletions packages/compass-user-data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export type { ReadAllResult } from './user-data';
export { IUserData, FileUserData, AtlasUserData } from './user-data';
export type { ReadAllResult, UserDataType } from './user-data';
export {
IUserData,
FileUserData,
AtlasUserData,
assertsUserDataType,
} from './user-data';
export { z } from 'zod';
Loading
Loading