From 75d6847a65e09c83871f0faa3509669f7e52ef5f Mon Sep 17 00:00:00 2001 From: nasc17 <69922333+nasc17@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:10:28 -0700 Subject: [PATCH] Set original database for connection done through connection dialog (#17266) * check if database connection * Change name of isMaster and remove import * Set to false * take out connecttodatabase * remove connecttodatabase * Original database * remove empty string check * clean * set original database for when saving connection * pass unit test * map -> find * PR changes * Comments for original database --- .../connection/common/connectionProfile.ts | 16 +++++++++ src/sql/platform/connection/common/utils.ts | 9 ++--- .../browser/asmtResultsView.component.ts | 2 +- .../dashboard/browser/dashboard.component.ts | 2 +- .../objectExplorer/test/browser/utils.test.ts | 2 +- .../browser/connectionManagementService.ts | 34 ++++++++----------- 6 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/sql/platform/connection/common/connectionProfile.ts b/src/sql/platform/connection/common/connectionProfile.ts index 4ad2a59ea1..5abac0751c 100644 --- a/src/sql/platform/connection/common/connectionProfile.ts +++ b/src/sql/platform/connection/common/connectionProfile.ts @@ -70,6 +70,9 @@ export class ConnectionProfile extends ProviderConnectionInfo implements interfa this.options.expiresOn = model.options.expiresOn; } } + if (model.options?.originalDatabase) { + this.originalDatabase = model.options.originalDatabase; + } } else { //Default for a new connection this.savePassword = false; @@ -142,6 +145,19 @@ export class ConnectionProfile extends ProviderConnectionInfo implements interfa this.options['azureResourceId'] = value; } + /** + * Database of server specified before connection. + * Some providers will modify the database field of the connection once a connection is made + * so that it reflects the actual database that was connected to. + */ + public get originalDatabase() { + return this.options['originalDatabase']; + } + + public set originalDatabase(value: string | undefined) { + this.options['originalDatabase'] = value; + } + public get registeredServerDescription(): string { return this.options['registeredServerDescription']; } diff --git a/src/sql/platform/connection/common/utils.ts b/src/sql/platform/connection/common/utils.ts index 2079567d53..ee38ec8978 100644 --- a/src/sql/platform/connection/common/utils.ts +++ b/src/sql/platform/connection/common/utils.ts @@ -6,7 +6,6 @@ import { IConnectionProfile } from 'sql/platform/connection/common/interfaces'; import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile'; import { ConnectionProfileGroup } from 'sql/platform/connection/common/connectionProfileGroup'; -import { mssqlProviderName } from 'sql/platform/connection/common/constants'; // CONSTANTS ////////////////////////////////////////////////////////////////////////////////////// const msInH = 3.6e6; @@ -134,9 +133,7 @@ export function findProfileInGroup(og: IConnectionProfile, groups: ConnectionPro return undefined; } -export function isMaster(profile: IConnectionProfile): boolean { - // TODO: the connection profile should have a property to indicate whether the connection is a server connection or database connection - // created issue to track the problem: https://github.com/Microsoft/azuredatastudio/issues/5193. - return (profile.providerName === mssqlProviderName && profile.databaseName?.toLowerCase() === 'master') - || (profile.providerName.toLowerCase() === 'pgsql' && profile.databaseName?.toLowerCase() === 'postgres'); +export function isServerConnection(profile: IConnectionProfile): boolean { + // If the user did not specify a database in the original connection, then this is considered a server-level connection + return !profile.options.originalDatabase; } diff --git a/src/sql/workbench/contrib/assessment/browser/asmtResultsView.component.ts b/src/sql/workbench/contrib/assessment/browser/asmtResultsView.component.ts index b392484b55..4342e40ff1 100644 --- a/src/sql/workbench/contrib/assessment/browser/asmtResultsView.component.ts +++ b/src/sql/workbench/contrib/assessment/browser/asmtResultsView.component.ts @@ -150,7 +150,7 @@ export class AsmtResultsViewComponent extends TabChild implements IAssessmentCom let self = this; const profile = this._commonService.connectionManagementService.connectionInfo.connectionProfile; - this.isServerMode = !profile.databaseName || Utils.isMaster(profile); + this.isServerMode = !profile.databaseName || Utils.isServerConnection(profile); if (this.isServerMode) { this.placeholderNoResultsLabel = nls.localize('asmt.TargetInstanceComplient', "Instance {0} is totally compliant with the best practices. Good job!", profile.serverName); diff --git a/src/sql/workbench/contrib/dashboard/browser/dashboard.component.ts b/src/sql/workbench/contrib/dashboard/browser/dashboard.component.ts index 25b5ba2d9d..88ac419373 100644 --- a/src/sql/workbench/contrib/dashboard/browser/dashboard.component.ts +++ b/src/sql/workbench/contrib/dashboard/browser/dashboard.component.ts @@ -40,7 +40,7 @@ export class DashboardComponent extends AngularDisposable implements OnInit { this._register(this.themeService.onDidColorThemeChange(this.updateTheme, this)); this.updateTheme(this.themeService.getColorTheme()); const profile: IConnectionProfile = this._bootstrapService.getOriginalConnectionProfile(); - if (profile && (!profile.databaseName || Utils.isMaster(profile))) { + if (profile && (!profile.databaseName || Utils.isServerConnection(profile))) { // Route to the server page as this is the default database this._router.navigate(['server-dashboard']).catch(onUnexpectedError); } diff --git a/src/sql/workbench/contrib/objectExplorer/test/browser/utils.test.ts b/src/sql/workbench/contrib/objectExplorer/test/browser/utils.test.ts index 2cdd0c06cb..1f688cdb6e 100644 --- a/src/sql/workbench/contrib/objectExplorer/test/browser/utils.test.ts +++ b/src/sql/workbench/contrib/objectExplorer/test/browser/utils.test.ts @@ -47,7 +47,7 @@ suite('Connection Utilities tests', () => { test('isMaster - test if isMaster recognizes Connection Profile as server connection', () => { - assert(ConnectionUtils.isMaster(connection)); + assert(ConnectionUtils.isServerConnection(connection)); }); test('parseTimeString - test if time is parsed correctly', () => { diff --git a/src/sql/workbench/services/connection/browser/connectionManagementService.ts b/src/sql/workbench/services/connection/browser/connectionManagementService.ts index cfe2f212bd..56f8b46e82 100644 --- a/src/sql/workbench/services/connection/browser/connectionManagementService.ts +++ b/src/sql/workbench/services/connection/browser/connectionManagementService.ts @@ -472,6 +472,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti if (!tokenFillSuccess) { throw new Error(nls.localize('connection.noAzureAccount', "Failed to get Azure account token for connection")); } + if (options.saveTheConnection) { + connection.options.originalDatabase = connection.databaseName; + } return this.createNewConnection(uri, connection).then(async connectionResult => { if (connectionResult && connectionResult.connected) { // The connected succeeded so add it to our active connections now, optionally adding it to the MRU based on @@ -629,27 +632,20 @@ export class ConnectionManagementService extends Disposable implements IConnecti } private focusDashboard(profile: interfaces.IConnectionProfile): boolean { - let found: boolean = false; + const matchingEditor = this._editorService.editors.find(editor => { + return editor instanceof DashboardInput && DashboardInput.profileMatches(profile, editor.connectionProfile); + }) as DashboardInput; - this._editorService.editors.map(editor => { - if (editor instanceof DashboardInput) { - if (DashboardInput.profileMatches(profile, editor.connectionProfile)) { - editor.connectionProfile.connectionName = profile.connectionName; - editor.connectionProfile.databaseName = profile.databaseName; - this._editorService.openEditor(editor) - .then(() => { - if (!profile.databaseName || Utils.isMaster(profile)) { - this._angularEventing.sendAngularEvent(editor.uri, AngularEventType.NAV_SERVER); - } else { - this._angularEventing.sendAngularEvent(editor.uri, AngularEventType.NAV_DATABASE); - } - found = true; - }, errors.onUnexpectedError); - } - } - }); + if (matchingEditor) { + matchingEditor.connectionProfile.connectionName = profile.connectionName; + matchingEditor.connectionProfile.databaseName = profile.databaseName; + this._editorService.openEditor(matchingEditor).then(() => { + const target = !profile.databaseName || Utils.isServerConnection(profile) ? AngularEventType.NAV_SERVER : AngularEventType.NAV_DATABASE; + this._angularEventing.sendAngularEvent(matchingEditor.uri, target); + }, errors.onUnexpectedError); + } - return found; + return !!matchingEditor; } public closeDashboard(uri: string): void {