Skip to content

Commit 09fc0f3

Browse files
committed
Extract DatabaseItem.refresh to DatabaseManager
This moves the `refresh` method from `DatabaseItem` to `DatabaseManager` and makes it private. This makes the `DatabaseItem` interface smaller and more focused and ensures that `refresh` cannot be called from outside of the `DatabaseManager`.
1 parent 295a08f commit 09fc0f3

7 files changed

Lines changed: 51 additions & 81 deletions

File tree

extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as cli from "../../codeql-cli/cli";
33
import vscode from "vscode";
44
import { FullDatabaseOptions } from "./database-options";
55
import { basename, dirname, join, relative } from "path";
6-
import { asError } from "../../pure/helpers-pure";
76
import {
87
decodeSourceArchiveUri,
98
encodeArchiveBasePath,
@@ -15,29 +14,27 @@ import { isLikelyDatabaseRoot } from "../../helpers";
1514
import { stat } from "fs-extra";
1615
import { pathsEqual } from "../../pure/files";
1716
import { DatabaseContents } from "./database-contents";
18-
import { DatabaseResolver } from "./database-resolver";
19-
import { DatabaseChangedEvent, DatabaseEventKind } from "./database-events";
2017

2118
export class DatabaseItemImpl implements DatabaseItem {
22-
private _error: Error | undefined = undefined;
23-
private _contents: DatabaseContents | undefined;
19+
// These are only public in the implementation, they are readonly in the interface
20+
public error: Error | undefined = undefined;
21+
public contents: DatabaseContents | undefined;
2422
/** A cache of database info */
2523
private _dbinfo: cli.DbInfo | undefined;
2624

2725
public constructor(
2826
public readonly databaseUri: vscode.Uri,
2927
contents: DatabaseContents | undefined,
3028
private options: FullDatabaseOptions,
31-
private readonly onChanged: (event: DatabaseChangedEvent) => void,
3229
) {
33-
this._contents = contents;
30+
this.contents = contents;
3431
}
3532

3633
public get name(): string {
3734
if (this.options.displayName) {
3835
return this.options.displayName;
39-
} else if (this._contents) {
40-
return this._contents.name;
36+
} else if (this.contents) {
37+
return this.contents.name;
4138
} else {
4239
return basename(this.databaseUri.fsPath);
4340
}
@@ -48,45 +45,17 @@ export class DatabaseItemImpl implements DatabaseItem {
4845
}
4946

5047
public get sourceArchive(): vscode.Uri | undefined {
51-
if (this.options.ignoreSourceArchive || this._contents === undefined) {
48+
if (this.options.ignoreSourceArchive || this.contents === undefined) {
5249
return undefined;
5350
} else {
54-
return this._contents.sourceArchiveUri;
51+
return this.contents.sourceArchiveUri;
5552
}
5653
}
5754

58-
public get contents(): DatabaseContents | undefined {
59-
return this._contents;
60-
}
61-
6255
public get dateAdded(): number | undefined {
6356
return this.options.dateAdded;
6457
}
6558

66-
public get error(): Error | undefined {
67-
return this._error;
68-
}
69-
70-
public async refresh(): Promise<void> {
71-
try {
72-
try {
73-
this._contents = await DatabaseResolver.resolveDatabaseContents(
74-
this.databaseUri,
75-
);
76-
this._error = undefined;
77-
} catch (e) {
78-
this._contents = undefined;
79-
this._error = asError(e);
80-
throw e;
81-
}
82-
} finally {
83-
this.onChanged({
84-
kind: DatabaseEventKind.Refresh,
85-
item: this,
86-
});
87-
}
88-
}
89-
9059
public resolveSourceFile(uriStr: string | undefined): vscode.Uri {
9160
const sourceArchive = this.sourceArchive;
9261
const uri = uriStr ? vscode.Uri.parse(uriStr, true) : undefined;

extensions/ql-vscode/src/databases/local-databases/database-item.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ export interface DatabaseItem {
2727

2828
/** If the database is invalid, describes why. */
2929
readonly error: Error | undefined;
30-
/**
31-
* Resolves the contents of the database.
32-
*
33-
* @remarks
34-
* The contents include the database directory, source archive, and metadata about the database.
35-
* If the database is invalid, `this.error` is updated with the error object that describes why
36-
* the database is invalid. This error is also thrown.
37-
*/
38-
refresh(): Promise<void>;
30+
3931
/**
4032
* Resolves a filename to its URI in the source archive.
4133
*

extensions/ql-vscode/src/databases/local-databases/database-manager.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,7 @@ export class DatabaseManager extends DisposableObject {
173173
dateAdded: Date.now(),
174174
language: await this.getPrimaryLanguage(uri.fsPath),
175175
};
176-
const databaseItem = new DatabaseItemImpl(
177-
uri,
178-
contents,
179-
fullOptions,
180-
(event) => {
181-
this._onDidChangeDatabaseItem.fire(event);
182-
},
183-
);
176+
const databaseItem = new DatabaseItemImpl(uri, contents, fullOptions);
184177

185178
return databaseItem;
186179
}
@@ -359,14 +352,7 @@ export class DatabaseManager extends DisposableObject {
359352
dateAdded,
360353
language,
361354
};
362-
const item = new DatabaseItemImpl(
363-
dbBaseUri,
364-
undefined,
365-
fullOptions,
366-
(event) => {
367-
this._onDidChangeDatabaseItem.fire(event);
368-
},
369-
);
355+
const item = new DatabaseItemImpl(dbBaseUri, undefined, fullOptions);
370356

371357
// Avoid persisting the database state after adding since that should happen only after
372358
// all databases have been added.
@@ -407,7 +393,7 @@ export class DatabaseManager extends DisposableObject {
407393
database,
408394
);
409395
try {
410-
await databaseItem.refresh();
396+
await this.refreshDatabase(databaseItem);
411397
await this.registerDatabase(progress, token, databaseItem);
412398
if (currentDatabaseUri === database.uri) {
413399
await this.setCurrentDatabaseItem(databaseItem, true);
@@ -449,8 +435,12 @@ export class DatabaseManager extends DisposableObject {
449435
item: DatabaseItem | undefined,
450436
skipRefresh = false,
451437
): Promise<void> {
452-
if (!skipRefresh && item !== undefined) {
453-
await item.refresh(); // Will throw on invalid database.
438+
if (
439+
!skipRefresh &&
440+
item !== undefined &&
441+
item instanceof DatabaseItemImpl
442+
) {
443+
await this.refreshDatabase(item); // Will throw on invalid database.
454444
}
455445
if (this._currentDatabaseItem !== item) {
456446
this._currentDatabaseItem = item;
@@ -616,6 +606,34 @@ export class DatabaseManager extends DisposableObject {
616606
await this.qs.registerDatabase(progress, token, dbItem);
617607
}
618608

609+
/**
610+
* Resolves the contents of the database.
611+
*
612+
* @remarks
613+
* The contents include the database directory, source archive, and metadata about the database.
614+
* If the database is invalid, `databaseItem.error` is updated with the error object that describes why
615+
* the database is invalid. This error is also thrown.
616+
*/
617+
private async refreshDatabase(databaseItem: DatabaseItemImpl) {
618+
try {
619+
try {
620+
databaseItem.contents = await DatabaseResolver.resolveDatabaseContents(
621+
databaseItem.databaseUri,
622+
);
623+
databaseItem.error = undefined;
624+
} catch (e) {
625+
databaseItem.contents = undefined;
626+
databaseItem.error = asError(e);
627+
throw e;
628+
}
629+
} finally {
630+
this._onDidChangeDatabaseItem.fire({
631+
kind: DatabaseEventKind.Refresh,
632+
item: databaseItem,
633+
});
634+
}
635+
}
636+
619637
private updatePersistedCurrentDatabaseItem(): void {
620638
void this.ctx.workspaceState.update(
621639
CURRENT_DB,

extensions/ql-vscode/test/factories/databases/databases.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function createMockDB(
3333
datasetUri: databaseUri,
3434
} as DatabaseContents,
3535
dbOptions,
36-
() => void 0,
3736
);
3837
}
3938

extensions/ql-vscode/test/vscode-tests/cli-integration/skeleton-query-wizard.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,7 @@ describe("SkeletonQueryWizard", () => {
546546
dateAdded: 123,
547547
} as FullDatabaseOptions);
548548

549-
jest
550-
.spyOn(mockDbItem, "error", "get")
551-
.mockReturnValue(asError("database go boom!"));
549+
mockDbItem.error = asError("database go boom!");
552550

553551
const sortedList =
554552
await SkeletonQueryWizard.sortDatabaseItemsByDateAdded([

extensions/ql-vscode/test/vscode-tests/minimal-workspace/local-databases.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ describe("local databases", () => {
327327
mockDbOptions(),
328328
Uri.parse("file:/sourceArchive-uri/"),
329329
);
330-
(db as any)._contents.sourceArchiveUri = undefined;
330+
(db as any).contents.sourceArchiveUri = undefined;
331331
expect(() => db.resolveSourceFile("abc")).toThrowError(
332332
"Scheme is missing",
333333
);
@@ -339,7 +339,7 @@ describe("local databases", () => {
339339
mockDbOptions(),
340340
Uri.parse("file:/sourceArchive-uri/"),
341341
);
342-
(db as any)._contents.sourceArchiveUri = undefined;
342+
(db as any).contents.sourceArchiveUri = undefined;
343343
expect(() => db.resolveSourceFile("http://abc")).toThrowError(
344344
"Invalid uri scheme",
345345
);
@@ -352,7 +352,7 @@ describe("local databases", () => {
352352
mockDbOptions(),
353353
Uri.parse("file:/sourceArchive-uri/"),
354354
);
355-
(db as any)._contents.sourceArchiveUri = undefined;
355+
(db as any).contents.sourceArchiveUri = undefined;
356356
const resolved = db.resolveSourceFile(undefined);
357357
expect(resolved.toString(true)).toBe(dbLocationUri(dir).toString(true));
358358
});
@@ -363,7 +363,7 @@ describe("local databases", () => {
363363
mockDbOptions(),
364364
Uri.parse("file:/sourceArchive-uri/"),
365365
);
366-
(db as any)._contents.sourceArchiveUri = undefined;
366+
(db as any).contents.sourceArchiveUri = undefined;
367367
const resolved = db.resolveSourceFile("file:");
368368
expect(resolved.toString()).toBe("file:///");
369369
});

extensions/ql-vscode/test/vscode-tests/no-workspace/query-testing/test-runner.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,11 @@ describe("test-runner", () => {
4040
Uri.file("/path/to/test/dir/dir.testproj"),
4141
undefined,
4242
mockedObject<FullDatabaseOptions>({ displayName: "custom display name" }),
43-
(_) => {
44-
/* no change event listener */
45-
},
4643
);
4744
const postTestDatabaseItem = new DatabaseItemImpl(
4845
Uri.file("/path/to/test/dir/dir.testproj"),
4946
undefined,
5047
mockedObject<FullDatabaseOptions>({ displayName: "default name" }),
51-
(_) => {
52-
/* no change event listener */
53-
},
5448
);
5549

5650
beforeEach(() => {

0 commit comments

Comments
 (0)