diff --git a/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseDialog.ts b/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseDialog.ts index 3d4f390920..741a1d200d 100644 --- a/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseDialog.ts +++ b/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseDialog.ts @@ -3,7 +3,7 @@ * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import type * as azdataType from 'azdata'; +import * as azdataType from 'azdata'; import * as vscode from 'vscode'; import * as constants from '../common/constants'; import * as newProjectTool from '../tools/newProjectTool'; @@ -50,11 +50,37 @@ export class CreateProjectFromDatabaseDialog { this.dialog.cancelButton.label = constants.cancelButtonText; + let connected = false; + if (this.profile) { + const connections = await azdataType.connection.getConnections(true); + connected = !!connections.find(c => c.connectionId === this.profile!.id); + + if (!connected) { + // if the connection clicked on isn't currently connected, try to connect + const result = await azdataType.connection.connect(this.profile, true, false); + connected = result.connected; + + if (!result.connected) { + // if can't connect automatically, open connection dialog with the info from the profile + const connection = await azdataType.connection.openConnectionDialog(undefined, this.profile); + connected = !!connection; + + // update these fields if connection was successful, to ensure they match the connection made + if (connected) { + this.profile.id = connection.connectionId; + this.profile.databaseName = connection.options['databaseName']; + this.profile.serverName = connection.options['server']; + this.profile.userName = connection.options['user']; + } + } + } + } + getAzdataApi()!.window.openDialog(this.dialog); await this.initDialogComplete.promise; - if (this.profile) { - await this.updateConnectionComponents(getConnectionName(this.profile), this.profile.id, this.profile.databaseName!); + if (connected) { + await this.updateConnectionComponents(getConnectionName(this.profile), this.profile!.id, this.profile!.databaseName); } this.tryEnableCreateButton(); diff --git a/extensions/sql-database-projects/src/test/dialogs/createProjectFromDatabaseDialog.test.ts b/extensions/sql-database-projects/src/test/dialogs/createProjectFromDatabaseDialog.test.ts index 71bf7cb6a8..1b4992f148 100644 --- a/extensions/sql-database-projects/src/test/dialogs/createProjectFromDatabaseDialog.test.ts +++ b/extensions/sql-database-projects/src/test/dialogs/createProjectFromDatabaseDialog.test.ts @@ -17,6 +17,8 @@ describe('Create Project From Database Dialog', () => { }); it('Should open dialog successfully', async function (): Promise { + sinon.stub(azdata.connection, 'getConnections').resolves([]); + sinon.stub(azdata.connection, 'connect').resolves({ connected: true, connectionId: '0', errorMessage: '', errorCode: 0}); sinon.stub(azdata.connection, 'listDatabases').resolves([]); const dialog = new CreateProjectFromDatabaseDialog(mockConnectionProfile); await dialog.openDialog(); @@ -24,6 +26,8 @@ describe('Create Project From Database Dialog', () => { }); it('Should enable ok button correctly with a connection profile', async function (): Promise { + sinon.stub(azdata.connection, 'getConnections').resolves([]); + sinon.stub(azdata.connection, 'connect').resolves({ connected: true, connectionId: '0', errorMessage: '', errorCode: 0}); sinon.stub(azdata.connection, 'listDatabases').resolves([]); const dialog = new CreateProjectFromDatabaseDialog(mockConnectionProfile); await dialog.openDialog(); // should set connection details @@ -74,6 +78,8 @@ describe('Create Project From Database Dialog', () => { }); it('Should create default project name correctly when database information is populated', async function (): Promise { + sinon.stub(azdata.connection, 'getConnections').resolves([]); + sinon.stub(azdata.connection, 'connect').resolves({ connected: true, connectionId: '0', errorMessage: '', errorCode: 0}); sinon.stub(azdata.connection, 'listDatabases').resolves(['My Database']); const dialog = new CreateProjectFromDatabaseDialog(mockConnectionProfile); await dialog.openDialog(); @@ -85,6 +91,8 @@ describe('Create Project From Database Dialog', () => { it('Should include all info in import data model and connect to appropriate call back properties', async function (): Promise { const stubUri = 'My URI'; const dialog = new CreateProjectFromDatabaseDialog(mockConnectionProfile); + sinon.stub(azdata.connection, 'getConnections').resolves([]); + sinon.stub(azdata.connection, 'connect').resolves({ connected: true, connectionId: '0', errorMessage: '', errorCode: 0}); sinon.stub(azdata.connection, 'listDatabases').resolves(['My Database']); sinon.stub(azdata.connection, 'getUriForConnection').resolves(stubUri); await dialog.openDialog();