Skip to content

Commit

Permalink
Fix create project from db not loading dbs when disconnected (microso…
Browse files Browse the repository at this point in the history
…ft#19129)

* fix create project from database when launched from disconnected node

* don't open dashboard

* fix tests

* update order so connection dialog opens first if can't connect
  • Loading branch information
kisantia authored Apr 22, 2022
1 parent 7554fbc commit 23f9e37
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ describe('Create Project From Database Dialog', () => {
});

it('Should open dialog successfully', async function (): Promise<void> {
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.notEqual(dialog.createProjectFromDatabaseTab, undefined);
});

it('Should enable ok button correctly with a connection profile', async function (): Promise<void> {
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
Expand Down Expand Up @@ -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<void> {
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();
Expand All @@ -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<void> {
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();
Expand Down

0 comments on commit 23f9e37

Please sign in to comment.