Skip to content

Commit 319644f

Browse files
authored
fix(connections): add support for downgrade to a version with storage-mixin COMPASS-7174 (#4756)
1 parent d016374 commit 319644f

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

packages/connection-storage/src/connection-storage.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,34 @@ describe('ConnectionStorage', function () {
283283
expect(expectedConnectionInfo).to.deep.equal(connectionInfo);
284284
});
285285

286+
it('saves a connection with _id', async function () {
287+
const id: string = uuid();
288+
expect(fs.existsSync(getConnectionFilePath(tmpDir, id))).to.be.false;
289+
290+
await ConnectionStorage.save({
291+
connectionInfo: {
292+
id,
293+
connectionOptions: {
294+
connectionString: 'mongodb://root:password@localhost:27017',
295+
},
296+
},
297+
});
298+
299+
const savedConnection = JSON.parse(
300+
fs.readFileSync(getConnectionFilePath(tmpDir, id), 'utf-8')
301+
);
302+
303+
expect(savedConnection).to.deep.equal({
304+
connectionInfo: {
305+
id,
306+
connectionOptions: {
307+
connectionString: 'mongodb://root:password@localhost:27017',
308+
},
309+
},
310+
_id: id,
311+
});
312+
});
313+
286314
context(`max allowed connections ${maxAllowedConnections}`, function () {
287315
const createNumberOfConnections = (num: number) => {
288316
const connectionInfos = Array.from({ length: num }, (v, i) =>

packages/connection-storage/src/connection-storage.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ import type {
3131

3232
const { log, mongoLogId } = createLoggerAndTelemetry('CONNECTION-STORAGE');
3333

34+
type ConnectionLegacyProps = {
35+
_id: string;
36+
isFavorite?: boolean;
37+
name: string;
38+
};
39+
40+
type ConnectionWithLegacyProps = {
41+
connectionInfo: ConnectionInfo;
42+
} & ConnectionLegacyProps;
43+
3444
export class ConnectionStorage {
3545
private static calledOnce: boolean;
3646
private static path: string;
@@ -102,7 +112,7 @@ export class ConnectionStorage {
102112
}
103113
}
104114

105-
private static async getConnections(): Promise<any[]> {
115+
private static async getConnections(): Promise<ConnectionWithLegacyProps[]> {
106116
const connectionIds = (await fs.readdir(this.getFolderPath()))
107117
.filter((file) => file.endsWith('.json'))
108118
.map((file) => file.replace('.json', ''));
@@ -167,11 +177,8 @@ export class ConnectionStorage {
167177
return (
168178
connections
169179
// Ignore legacy connections and make sure connection has a connection string.
170-
.filter(
171-
(x: { connectionInfo?: ConnectionInfo }) =>
172-
x.connectionInfo?.connectionOptions?.connectionString
173-
)
174-
.map(({ connectionInfo }: { connectionInfo: ConnectionInfo }) =>
180+
.filter((x) => x.connectionInfo?.connectionOptions?.connectionString)
181+
.map(({ connectionInfo }) =>
175182
this.mapStoredConnectionToConnectionInfo(
176183
connectionInfo,
177184
secrets[connectionInfo.id]
@@ -210,11 +217,16 @@ export class ConnectionStorage {
210217
throw new Error('Connection string is required.');
211218
}
212219

220+
// While saving connections, we also save `_id` property
221+
// in order to support the downgrade of Compass to a version
222+
// where we use storage-mixin. storage-mixin uses this prop
223+
// to map keytar credentials to the stored connection.
224+
213225
// While testing, we don't use keychain to store secrets
214226
if (process.env.COMPASS_E2E_DISABLE_KEYCHAIN_USAGE === 'true') {
215227
await fs.writeFile(
216228
this.getFilePath(connectionInfo.id),
217-
JSON.stringify({ connectionInfo }, null, 2),
229+
JSON.stringify({ connectionInfo, _id: connectionInfo.id }, null, 2),
218230
'utf-8'
219231
);
220232
} else {
@@ -225,6 +237,7 @@ export class ConnectionStorage {
225237
JSON.stringify(
226238
{
227239
connectionInfo: connectionInfoWithoutSecrets,
240+
_id: connectionInfo.id,
228241
},
229242
null,
230243
2

0 commit comments

Comments
 (0)