diff --git a/extensions/sql-database-projects/src/models/project.ts b/extensions/sql-database-projects/src/models/project.ts index 5aca304079..4f2d4fb518 100644 --- a/extensions/sql-database-projects/src/models/project.ts +++ b/extensions/sql-database-projects/src/models/project.ts @@ -644,7 +644,7 @@ export class Project { referenceNode.appendChild(databaseSqlCmdVariableElement); // add SQLCMD variable - this.addSqlCmdVariable((entry).databaseSqlCmdVariable!, (entry).databaseName); + this.addSqlCmdVariable((entry).databaseSqlCmdVariable!, (entry).databaseVariableLiteralValue!); } else if (entry.databaseVariableLiteralValue) { const databaseVariableLiteralValueElement = this.projFileXmlDoc.createElement(constants.DatabaseVariableLiteralValue); const databaseTextNode = this.projFileXmlDoc.createTextNode(entry.databaseVariableLiteralValue); diff --git a/extensions/sql-database-projects/src/test/project.test.ts b/extensions/sql-database-projects/src/test/project.test.ts index adbd9e5745..9124980fae 100644 --- a/extensions/sql-database-projects/src/test/project.test.ts +++ b/extensions/sql-database-projects/src/test/project.test.ts @@ -246,6 +246,7 @@ describe('Project: sqlproj content operations', function (): void { should(projFileText).containEql('test2.dacpac'); should(projFileText).containEql('test2Db'); should(projFileText).containEql(''); + should(projFileText).containEql('test2DbName'); }); it('Should add a dacpac reference to a different database in a different server correctly', async function (): Promise { @@ -270,8 +271,10 @@ describe('Project: sqlproj content operations', function (): void { should(projFileText).containEql('test3.dacpac'); should(projFileText).containEql('test3Db'); should(projFileText).containEql(''); + should(projFileText).containEql('test3DbName'); should(projFileText).containEql('otherServer'); should(projFileText).containEql(''); + should(projFileText).containEql('otherServerName'); }); it('Should add a project reference to the same database correctly', async function (): Promise { @@ -322,6 +325,7 @@ describe('Project: sqlproj content operations', function (): void { should(projFileText).containEql('project1'); should(projFileText).containEql('testdb'); should(projFileText).containEql(''); + should(projFileText).containEql('testdbName'); }); it('Should add a project reference to a different database in a different server correctly', async function (): Promise { @@ -351,8 +355,10 @@ describe('Project: sqlproj content operations', function (): void { should(projFileText).containEql('project1'); should(projFileText).containEql('testdb'); should(projFileText).containEql(''); + should(projFileText).containEql('testdbName'); should(projFileText).containEql('otherServer'); should(projFileText).containEql(''); + should(projFileText).containEql('otherServerName'); }); it('Should not allow adding duplicate database references', async function (): Promise { @@ -377,6 +383,57 @@ describe('Project: sqlproj content operations', function (): void { should(project.databaseReferences.length).equal(2, 'There should be two database references after trying to add a reference to test.dacpac again'); }); + it('Should update sqlcmd variable values if value changes', async function (): Promise { + projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline); + const project = await Project.openProject(projFilePath); + const databaseVariable = 'test3Db'; + const serverVariable = 'otherServer'; + + should(project.databaseReferences.length).equal(0, 'There should be no database references to start with'); + await project.addDatabaseReference({ + dacpacFileLocation: Uri.file('test3.dacpac'), + databaseName: 'test3DbName', + databaseVariable: databaseVariable, + serverName: 'otherServerName', + serverVariable: serverVariable, + suppressMissingDependenciesErrors: false + }); + should(project.databaseReferences.length).equal(1, 'There should be a database reference after adding a reference to test3'); + should(project.databaseReferences[0].databaseName).equal('test3', 'The database reference should be test3'); + should(Object.keys(project.sqlCmdVariables).length).equal(2, 'There should be 2 sqlcmdvars after adding the dacpac reference'); + + // make sure reference to test3.dacpac and SQLCMD variables were added + let projFileText = (await fs.readFile(projFilePath)).toString(); + should(projFileText).containEql(''); + should(projFileText).containEql('test3DbName'); + should(projFileText).containEql(''); + should(projFileText).containEql('otherServerName'); + + // delete reference + await project.deleteDatabaseReference(project.databaseReferences[0]); + should(project.databaseReferences.length).equal(0, 'There should be no database references after deleting'); + should(Object.keys(project.sqlCmdVariables).length).equal(2, 'There should still be 2 sqlcmdvars after deleting the dacpac reference'); + + // add reference to the same dacpac again but with different values for the sqlcmd variables + await project.addDatabaseReference({ + dacpacFileLocation: Uri.file('test3.dacpac'), + databaseName: 'newDbName', + databaseVariable: databaseVariable, + serverName: 'newServerName', + serverVariable: serverVariable, + suppressMissingDependenciesErrors: false + }); + should(project.databaseReferences.length).equal(1, 'There should be a database reference after adding a reference to test3'); + should(project.databaseReferences[0].databaseName).equal('test3', 'The database reference should be test3'); + should(Object.keys(project.sqlCmdVariables).length).equal(2, 'There should still be 2 sqlcmdvars after adding the dacpac reference again with different sqlcmdvar values'); + + projFileText = (await fs.readFile(projFilePath)).toString(); + should(projFileText).containEql(''); + should(projFileText).containEql('newDbName'); + should(projFileText).containEql(''); + should(projFileText).containEql('newServerName'); + }); + it('Should add pre and post deployment scripts as entries to sqlproj', async function (): Promise { projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline); const project: Project = await Project.openProject(projFilePath);