Skip to content

Commit 5fdfb44

Browse files
authored
Use downgrades when fixing dbscheme mismatches where possible. (#765)
1 parent 6e40478 commit 5fdfb44

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

extensions/ql-vscode/src/cli.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ export class CodeQLCliServer implements Disposable {
131131
*/
132132
private static CLI_VERSION_WITH_LANGUAGE = new SemVer('2.4.1');
133133

134+
/**
135+
* CLI version where `codeql resolve upgrades` supports
136+
* the `--allow-downgrades` flag
137+
*/
138+
private static CLI_VERSION_WITH_DOWNGRADES = new SemVer('2.4.4');
139+
134140
/** The process for the cli server, or undefined if one doesn't exist yet */
135141
process?: child_process.ChildProcessWithoutNullStreams;
136142
/** Queue of future commands*/
@@ -662,15 +668,19 @@ export class CodeQLCliServer implements Disposable {
662668
* Gets information necessary for upgrading a database.
663669
* @param dbScheme the path to the dbscheme of the database to be upgraded.
664670
* @param searchPath A list of directories to search for upgrade scripts.
671+
* @param allowDowngradesIfPossible Whether we should try and include downgrades of we can.
665672
* @param targetDbScheme The dbscheme to try to upgrade to.
666673
* @returns A list of database upgrade script directories
667674
*/
668-
resolveUpgrades(dbScheme: string, searchPath: string[], targetDbScheme?: string): Promise<UpgradesInfo> {
675+
async resolveUpgrades(dbScheme: string, searchPath: string[], allowDowngradesIfPossible: boolean, targetDbScheme?: string): Promise<UpgradesInfo> {
669676
const args = ['--additional-packs', searchPath.join(path.delimiter), '--dbscheme', dbScheme];
670677
if (targetDbScheme) {
671678
args.push('--target-dbscheme', targetDbScheme);
679+
if (allowDowngradesIfPossible && await this.supportsDowngrades()) {
680+
args.push('--allow-downgrades');
681+
}
672682
}
673-
return this.runJsonCodeQlCliCommand<UpgradesInfo>(
683+
return await this.runJsonCodeQlCliCommand<UpgradesInfo>(
674684
['resolve', 'upgrades'],
675685
args,
676686
'Resolving database upgrade scripts',
@@ -744,6 +754,10 @@ export class CodeQLCliServer implements Disposable {
744754
return (await this.getVersion()).compare(CodeQLCliServer.CLI_VERSION_WITH_LANGUAGE) >= 0;
745755
}
746756

757+
public async supportsDowngrades() {
758+
return (await this.getVersion()).compare(CodeQLCliServer.CLI_VERSION_WITH_DOWNGRADES) >= 0;
759+
}
760+
747761
private async refreshVersion() {
748762
const distribution = await this.distributionProvider.getDistribution();
749763
switch (distribution.kind) {

extensions/ql-vscode/src/run-queries.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ async function checkDbschemeCompatibility(
294294
const searchPath = getOnDiskWorkspaceFolders();
295295

296296
if (query.dbItem.contents !== undefined && query.dbItem.contents.dbSchemeUri !== undefined) {
297-
const { finalDbscheme } = await cliServer.resolveUpgrades(query.dbItem.contents.dbSchemeUri.fsPath, searchPath);
297+
const { finalDbscheme } = await cliServer.resolveUpgrades(query.dbItem.contents.dbSchemeUri.fsPath, searchPath, false);
298298
const hash = async function(filename: string): Promise<string> {
299299
return crypto.createHash('sha256').update(await fs.readFile(filename)).digest('hex');
300300
};
@@ -348,7 +348,7 @@ async function compileNonDestructiveUpgrade(
348348
if (!query.dbItem?.contents?.dbSchemeUri) {
349349
throw new Error('Database is invalid, and cannot be upgraded.');
350350
}
351-
const { scripts, matchesTarget } = await qs.cliServer.resolveUpgrades(query.dbItem.contents.dbSchemeUri.fsPath, searchPath, query.queryDbscheme);
351+
const { scripts, matchesTarget } = await qs.cliServer.resolveUpgrades(query.dbItem.contents.dbSchemeUri.fsPath, searchPath, true, query.queryDbscheme);
352352

353353
if (!matchesTarget) {
354354
reportNoUpgradePath(query);

extensions/ql-vscode/src/upgrades.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ export async function upgradeDatabaseExplicit(
171171
}
172172
const upgradeInfo = await qs.cliServer.resolveUpgrades(
173173
db.contents.dbSchemeUri.fsPath,
174-
searchPath
174+
searchPath,
175+
false
175176
);
176177

177178
const { scripts, finalDbscheme } = upgradeInfo;

0 commit comments

Comments
 (0)