add a project guid if a sql project doesn't have one (#18009)

This commit is contained in:
Kim Santiago
2022-01-07 16:01:39 -08:00
committed by GitHub
parent d13dd4d228
commit cd6b39ffee
5 changed files with 52 additions and 3 deletions

View File

@@ -376,6 +376,7 @@ export const True = 'True';
export const False = 'False';
export const Private = 'Private';
export const ProjectGuid = 'ProjectGuid';
export const PropertyGroup = 'PropertyGroup';
export const Type = 'Type';
export const ExternalStreamingJob: string = 'ExternalStreamingJob';
export const Sdk: string = 'Sdk';

View File

@@ -10,6 +10,7 @@ import * as utils from '../common/utils';
import * as xmlFormat from 'xml-formatter';
import * as os from 'os';
import * as templates from '../templates/templates';
import * as UUID from 'vscode-languageclient/lib/utils/uuid';
import { Uri, window } from 'vscode';
import { ISqlProject, SqlTargetPlatform } from 'sqldbproj';
@@ -146,8 +147,13 @@ export class Project implements ISqlProject {
try {
this._projectGuid = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectGuid)[0].childNodes[0].nodeValue!;
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ProjectGuid, this.projectFilePath));
console.error(utils.getErrorMessage(e));
// if no project guid, add a new one
this._projectGuid = UUID.generateUuid();
const newProjectGuidNode = this.projFileXmlDoc!.createElement(constants.ProjectGuid);
const newProjectGuidTextNode = this.projFileXmlDoc!.createTextNode(`{${this._projectGuid}}`);
newProjectGuidNode.appendChild(newProjectGuidTextNode);
this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.PropertyGroup)[0]?.appendChild(newProjectGuidNode);
await this.serializeToProjFile(this.projFileXmlDoc);
}
}

View File

@@ -34,6 +34,7 @@ export let openSdkStyleSqlProjectBaseline: string;
export let openSdkStyleSqlProjectWithFilesSpecifiedBaseline: string;
export let openSdkStyleSqlProjectWithGlobsSpecifiedBaseline: string;
export let openSdkStyleSqlProjectWithBuildRemoveBaseline: string;
export let openSdkStyleSqlProjectNoProjectGuidBaseline: string;
const baselineFolderPath = __dirname;
@@ -65,6 +66,7 @@ export async function loadBaselines() {
openSdkStyleSqlProjectWithFilesSpecifiedBaseline = await loadBaseline(baselineFolderPath, 'openSdkStyleSqlProjectWithFilesSpecifiedBaseline.xml');
openSdkStyleSqlProjectWithGlobsSpecifiedBaseline = await loadBaseline(baselineFolderPath, 'openSdkStyleSqlProjectWithGlobsSpecifiedBaseline.xml');
openSdkStyleSqlProjectWithBuildRemoveBaseline = await loadBaseline(baselineFolderPath, 'openSdkStyleSqlProjectWithBuildRemoveBaseline.xml');
openSdkStyleSqlProjectNoProjectGuidBaseline = await loadBaseline(baselineFolderPath, 'openSdkStyleSqlProjectNoProjectGuidBaseline.xml');
}
async function loadBaseline(baselineFolderPath: string, fileName: string): Promise<string> {

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build">
<Sdk Name="Microsoft.Build.Sql" Version="1.0.0" />
<PropertyGroup>
<Name>TestProjectName</Name>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql150DatabaseSchemaProvider</DSP>
<ModelCollation>1033, CI</ModelCollation>
</PropertyGroup>
<Target Name="BeforeBuild">
<Delete Files="$(BaseIntermediateOutputPath)\project.assets.json" />
</Target>
<ItemGroup>
<SqlCmdVariable Include="ProdDatabaseName">
<DefaultValue>MyProdDatabase</DefaultValue>
<Value>$(SqlCmdVar__1)</Value>
</SqlCmdVariable>
<SqlCmdVariable Include="BackupDatabaseName">
<DefaultValue>MyBackupDatabase</DefaultValue>
<Value>$(SqlCmdVar__2)</Value>
</SqlCmdVariable>
</ItemGroup>
<ItemGroup>
<PreDeploy Include="Script.PreDeployment1.sql" />
<None Include="Script.PreDeployment2.sql" />
</ItemGroup>
</Project>

View File

@@ -1097,7 +1097,6 @@ describe('Project: sdk style project content operations', function (): void {
should(projFileText.includes('<Build Remove="folder1\\nestedFolder\\**" />')).equal(false, projFileText);
});
it('Should handle excluding nested glob included folders', async function (): Promise<void> {
const testFolderPath = await testUtils.generateTestFolderPath();
projFilePath = await testUtils.createTestSqlProjFile(baselines.openSdkStyleSqlProjectBaseline, testFolderPath);
@@ -1344,6 +1343,21 @@ describe('Project: sdk style project content operations', function (): void {
should(projFileText.includes('<Build Remove="folder1\\**" />')).equal(false, projFileText);
should(projFileText.includes('<Build Remove="folder2\\**" />')).equal(false, projFileText);
});
it('Should add a project guid if there is not one in the sqlproj', async function (): Promise<void> {
projFilePath = await testUtils.createTestSqlProjFile(baselines.openSdkStyleSqlProjectNoProjectGuidBaseline);
let projFileText = (await fs.readFile(projFilePath)).toString();
// verify no project guid
should(projFileText.includes(constants.ProjectGuid)).equal(false);
const project: Project = await Project.openProject(projFilePath);
// verify project guid was added
projFileText = (await fs.readFile(projFilePath)).toString();
should(project.projectGuid).not.equal(undefined);
should(projFileText.includes(constants.ProjectGuid)).equal(true);
});
});
describe('Project: add SQLCMD Variables', function (): void {