Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
133 changes: 1 addition & 132 deletions extensions/ql-vscode/src/databases/config/db-config-store.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { pathExists, outputJSON, readJSON, readJSONSync } from "fs-extra";
import { join } from "path";
import {
clearLocalDbConfig,
cloneDbConfig,
DbConfig,
initializeLocalDbConfig,
removeLocalDb,
removeLocalList,
removeRemoteList,
removeRemoteOwner,
removeRemoteRepo,
renameLocalDb,
renameLocalList,
renameRemoteList,
SelectedDbItem,
DB_CONFIG_VERSION,
Expand All @@ -30,13 +24,7 @@ import {
DbConfigValidationErrorKind,
} from "../db-validation-errors";
import { ValueResult } from "../../common/value-result";
import {
LocalDatabaseDbItem,
LocalListDbItem,
RemoteUserDefinedListDbItem,
DbItem,
DbItemKind,
} from "../db-item";
import { RemoteUserDefinedListDbItem, DbItem, DbItemKind } from "../db-item";

export class DbConfigStore extends DisposableObject {
public static readonly databaseConfigFileName = "databases.json";
Expand Down Expand Up @@ -119,20 +107,9 @@ export class DbConfigStore extends DisposableObject {
let config: DbConfig;

switch (dbItem.kind) {
case DbItemKind.LocalList:
config = removeLocalList(this.config, dbItem.listName);
break;
case DbItemKind.RemoteUserDefinedList:
config = removeRemoteList(this.config, dbItem.listName);
break;
case DbItemKind.LocalDatabase:
// When we start using local databases these need to be removed from disk as well.
config = removeLocalDb(
this.config,
dbItem.databaseName,
dbItem.parentListName,
);
break;
case DbItemKind.RemoteRepo:
config = removeRemoteRepo(
this.config,
Expand Down Expand Up @@ -229,22 +206,6 @@ export class DbConfigStore extends DisposableObject {
await this.writeConfig(config);
}

public async addLocalList(listName: string): Promise<void> {
if (!this.config) {
throw Error("Cannot add local list if config is not loaded");
}

this.validateLocalListName(listName);

const config = cloneDbConfig(this.config);
config.databases.local.lists.push({
name: listName,
databases: [],
});

await this.writeConfig(config);
}

public async addRemoteList(listName: string): Promise<void> {
if (!this.config) {
throw Error("Cannot add variant analysis list if config is not loaded");
Expand All @@ -261,25 +222,6 @@ export class DbConfigStore extends DisposableObject {
await this.writeConfig(config);
}

public async renameLocalList(
currentDbItem: LocalListDbItem,
newName: string,
) {
if (!this.config) {
throw Error("Cannot rename local list if config is not loaded");
}

this.validateLocalListName(newName);

const updatedConfig = renameLocalList(
this.config,
currentDbItem.listName,
newName,
);

await this.writeConfig(updatedConfig);
}

public async renameRemoteList(
currentDbItem: RemoteUserDefinedListDbItem,
newName: string,
Expand All @@ -301,27 +243,6 @@ export class DbConfigStore extends DisposableObject {
await this.writeConfig(updatedConfig);
}

public async renameLocalDb(
currentDbItem: LocalDatabaseDbItem,
newName: string,
parentListName?: string,
): Promise<void> {
if (!this.config) {
throw Error("Cannot rename local db if config is not loaded");
}

this.validateLocalDbName(newName);

const updatedConfig = renameLocalDb(
this.config,
currentDbItem.databaseName,
newName,
parentListName,
);

await this.writeConfig(updatedConfig);
}

public doesRemoteListExist(listName: string): boolean {
if (!this.config) {
throw Error(
Expand All @@ -334,31 +255,6 @@ export class DbConfigStore extends DisposableObject {
);
}

public doesLocalListExist(listName: string): boolean {
if (!this.config) {
throw Error("Cannot check local list existence if config is not loaded");
}

return this.config.databases.local.lists.some((l) => l.name === listName);
}

public doesLocalDbExist(dbName: string, listName?: string): boolean {
if (!this.config) {
throw Error(
"Cannot check variant analysis repository existence if config is not loaded",
);
}

if (listName) {
return this.config.databases.local.lists.some(
(l) =>
l.name === listName && l.databases.some((d) => d.name === dbName),
);
}

return this.config.databases.local.databases.some((d) => d.name === dbName);
}

public doesRemoteDbExist(dbName: string, listName?: string): boolean {
if (!this.config) {
throw Error(
Expand All @@ -384,7 +280,6 @@ export class DbConfigStore extends DisposableObject {
}

private async writeConfig(config: DbConfig): Promise<void> {
clearLocalDbConfig(config);
await outputJSON(this.configPath, config, {
spaces: 2,
});
Expand Down Expand Up @@ -416,7 +311,6 @@ export class DbConfigStore extends DisposableObject {
}

if (newConfig) {
initializeLocalDbConfig(newConfig);
this.configErrors = this.configValidator.validate(newConfig);
}

Expand Down Expand Up @@ -451,7 +345,6 @@ export class DbConfigStore extends DisposableObject {
}

if (newConfig) {
initializeLocalDbConfig(newConfig);
this.configErrors = this.configValidator.validate(newConfig);
}

Expand Down Expand Up @@ -499,10 +392,6 @@ export class DbConfigStore extends DisposableObject {
owners: [],
repositories: [],
},
local: {
lists: [],
databases: [],
},
},
selected: {
kind: SelectedDbItemKind.VariantAnalysisSystemDefinedList,
Expand All @@ -511,16 +400,6 @@ export class DbConfigStore extends DisposableObject {
};
}

private validateLocalListName(listName: string): void {
if (listName === "") {
throw Error("List name cannot be empty");
}

if (this.doesLocalListExist(listName)) {
throw Error(`A local list with the name '${listName}' already exists`);
}
}

private validateRemoteListName(listName: string): void {
if (listName === "") {
throw Error("List name cannot be empty");
Expand All @@ -532,14 +411,4 @@ export class DbConfigStore extends DisposableObject {
);
}
}

private validateLocalDbName(dbName: string): void {
if (dbName === "") {
throw Error("Database name cannot be empty");
}

if (this.doesLocalDbExist(dbName)) {
throw Error(`A local database with the name '${dbName}' already exists`);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readJsonSync } from "fs-extra";
import { resolve } from "path";
import Ajv, { ValidateFunction } from "ajv";
import { clearLocalDbConfig, DbConfig } from "./db-config";
import { DbConfig } from "./db-config";
import { findDuplicateStrings } from "../../common/text-utils";
import {
DbConfigValidationError,
Expand All @@ -19,8 +19,6 @@ export class DbConfigValidator {
}

public validate(dbConfig: DbConfig): DbConfigValidationError[] {
const localDbs = clearLocalDbConfig(dbConfig);

this.validateSchemaFn(dbConfig);

if (this.validateSchemaFn.errors) {
Expand All @@ -30,13 +28,6 @@ export class DbConfigValidator {
}));
}

// Add any local db config back so that we have a config
// object that respects its type and validation can happen
// as normal.
if (localDbs) {
dbConfig.databases.local = localDbs;
}

return [
...this.validateDbListNames(dbConfig),
...this.validateDbNames(dbConfig),
Expand All @@ -55,14 +46,6 @@ export class DbConfigValidator {
)}`,
});

const duplicateLocalDbLists = findDuplicateStrings(
dbConfig.databases.local.lists.map((n) => n.name),
);

if (duplicateLocalDbLists.length > 0) {
errors.push(buildError(duplicateLocalDbLists));
}

const duplicateRemoteDbLists = findDuplicateStrings(
dbConfig.databases.variantAnalysis.repositoryLists.map((n) => n.name),
);
Expand All @@ -81,14 +64,6 @@ export class DbConfigValidator {
message: `There are databases with the same name: ${dups.join(", ")}`,
});

const duplicateLocalDbs = findDuplicateStrings(
dbConfig.databases.local.databases.map((d) => d.name),
);

if (duplicateLocalDbs.length > 0) {
errors.push(buildError(duplicateLocalDbs));
}

const duplicateRemoteDbs = findDuplicateStrings(
dbConfig.databases.variantAnalysis.repositories,
);
Expand All @@ -111,13 +86,6 @@ export class DbConfigValidator {
)}`,
});

for (const list of dbConfig.databases.local.lists) {
const dups = findDuplicateStrings(list.databases.map((d) => d.name));
if (dups.length > 0) {
errors.push(buildError(list.name, dups));
}
}

for (const list of dbConfig.databases.variantAnalysis.repositoryLists) {
const dups = findDuplicateStrings(list.repositories);
if (dups.length > 0) {
Expand Down
Loading