#3224 Fix extra connections populating MRU List (#4368)

* Modify where we add active connections so that we can use the saveTheConnection option to decide whether to add a connection to the MRU. This was necessary because the old location was called from onConnectionComplete which is sent by the SqlToolsService - and that doesn't get any UI related information like the options. The new location is still only called after the connection completes and will be added only if the connection succeeds.

Added new test and updated existing tests to handle new logic (plus a bit of async-refactoring).

* Fix couple spacing issues

* Add logic back in to short-circuit if we already have the connection in the active connections list.

* Fix spaces -> tabs
This commit is contained in:
Charles Gagnon
2019-03-12 14:05:50 -07:00
committed by GitHub
parent e783aeab66
commit 9fdeec6128
6 changed files with 118 additions and 94 deletions

View File

@@ -464,6 +464,12 @@ export class ConnectionManagementService extends Disposable implements IConnecti
}
this.createNewConnection(uri, connection).then(connectionResult => {
if (connectionResult && connectionResult.connected) {
// The connected succeeded so add it to our active connections now, optionally adding it to the MRU based on
// the options.saveTheConnection setting
let connectionMgmtInfo = this._connectionStatusManager.findConnection(uri);
let activeConnection = connectionMgmtInfo.connectionProfile;
this.tryAddActiveConnection(connectionMgmtInfo, activeConnection, options.saveTheConnection);
if (callbacks.onConnectSuccess) {
callbacks.onConnectSuccess(options.params);
}
@@ -860,9 +866,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti
/**
* Add a connection to the active connections list.
*/
private tryAddActiveConnection(connectionManagementInfo: ConnectionManagementInfo, newConnection: IConnectionProfile, isConnectionToDefaultDb: boolean): void {
private tryAddActiveConnection(connectionManagementInfo: ConnectionManagementInfo, newConnection: IConnectionProfile, addToMru: boolean): void {
if (newConnection) {
this._connectionStore.addActiveConnection(newConnection, isConnectionToDefaultDb)
this._connectionStore.addActiveConnection(newConnection, addToMru)
.then(() => {
connectionManagementInfo.connectHandler(true);
}, err => {
@@ -896,10 +902,6 @@ export class ConnectionManagementService extends Disposable implements IConnecti
let connection = this._connectionStatusManager.onConnectionComplete(info);
if (info.connectionId) {
let isConnectionToDefaultDb = false;
if (connection.connectionProfile && (!connection.connectionProfile.databaseName || connection.connectionProfile.databaseName.trim() === '')) {
isConnectionToDefaultDb = true;
}
if (info.connectionSummary && info.connectionSummary.databaseName) {
this._connectionStatusManager.updateDatabaseName(info);
}
@@ -907,8 +909,6 @@ export class ConnectionManagementService extends Disposable implements IConnecti
connection.extensionTimer.stop();
connection.connectHandler(true);
let activeConnection = connection.connectionProfile;
self.tryAddActiveConnection(connection, activeConnection, isConnectionToDefaultDb);
self.addTelemetryForConnection(connection);
if (self._connectionStatusManager.isDefaultTypeUri(info.ownerUri)) {

View File

@@ -181,6 +181,13 @@ export class ConnectionProfile extends ProviderConnectionInfo implements interfa
};
}
/**
* Returns whether this profile is connected to the default database (it doesn't specify a database to connect to)
*/
public static isConnectionToDefaultDb(profile: azdata.IConnectionProfile): boolean {
return !profile.databaseName || profile.databaseName.trim() === '';
}
public static fromIConnectionProfile(capabilitiesService: ICapabilitiesService, profile: azdata.IConnectionProfile) {
if (profile) {
if (profile instanceof ConnectionProfile) {

View File

@@ -277,20 +277,30 @@ export class ConnectionStore {
* Password values are stored to a separate credential store if the "savePassword" option is true
*
* @param {IConnectionCredentials} conn the connection to add
* @param {boolean} addToMru Whether to add this connection to the MRU
* @returns {Promise<void>} a Promise that returns when the connection was saved
*/
public addActiveConnection(conn: IConnectionProfile, isConnectionToDefaultDb: boolean = false): Promise<void> {
if (this.getActiveConnections().some(existingConn => existingConn.id === conn.id)) {
return Promise.resolve(undefined);
} else {
return this.addConnectionToMemento(conn, Constants.activeConnections, undefined, conn.savePassword).then(() => {
let maxConnections = this.getMaxRecentConnectionsCount();
if (isConnectionToDefaultDb) {
conn.databaseName = '';
}
return this.addConnectionToMemento(conn, Constants.recentConnections, maxConnections);
});
public async addActiveConnection(conn: IConnectionProfile, addToMru: boolean): Promise<void> {
if (addToMru) {
await this.addConnectionToMru(conn);
}
// Only add connections we don't already know about
if (!this.getActiveConnections().some(existingConn => existingConn.id === conn.id)) {
await this.addConnectionToMemento(conn, Constants.activeConnections, undefined, conn.savePassword);
}
}
/**
* Adds the specified connection to the MRU list
* @param conn The connection to add
*/
private async addConnectionToMru(conn: IConnectionProfile): Promise<void> {
let maxConnections = this.getMaxRecentConnectionsCount();
if (ConnectionProfile.isConnectionToDefaultDb(conn)) {
conn.databaseName = '';
}
await this.addConnectionToMemento(conn, Constants.recentConnections, maxConnections);
}
public addConnectionToMemento(conn: IConnectionProfile, mementoKey: string, maxConnections?: number, savePassword?: boolean): Promise<void> {