Skip to content

Adding support for default connections#22140

Open
Benjin wants to merge 2 commits into
mainfrom
dev/benjin/defaultConnection
Open

Adding support for default connections#22140
Benjin wants to merge 2 commits into
mainfrom
dev/benjin/defaultConnection

Conversation

@Benjin
Copy link
Copy Markdown
Contributor

@Benjin Benjin commented May 19, 2026

Description

Adding support for default connections. Replaces mssql.transferActiveEditorConnections setting with a three-way mssql.newEditorConnectionBehavior enum (transferActive | none | defaultConnection) and adds a new mssql.defaultConnectionId setting, allowing users to designate a specific saved connection that new query editors automatically connect to. The old setting is auto-migrated on activation and marked deprecated.

  • Old transferActiveEditorConnections setting is auto-migrated on activation
  • Invalid defaultConnection config prompts to select or change setting
  • Missing/unknown default connection ID falls back to transferActive

Code Changes Checklist

  • New or updated unit tests added
  • All existing tests pass (npm run test)
  • Code follows contributing guidelines
  • Telemetry/logging updated if relevant
  • No regressions or UX breakage

Reviewers: Please read our reviewer guidelines

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Replaces the boolean mssql.transferActiveEditorConnections setting with a tri‑state mssql.newEditorConnectionBehavior enum (transferActive | none | defaultConnection) and adds a paired mssql.defaultConnectionId setting so new SQL editors can auto‑connect to a designated saved connection. The old setting is auto‑migrated on activation and marked deprecated; an invalid defaultConnection configuration triggers a one‑shot prompt to fix it.

Changes:

  • New settings + enum, deprecation message, and migration logic from the old boolean (controller activation + telemetry).
  • SqlDocumentService rewrites its handleNewQueryCommand precedence and onDidOpenTextDocument auto-connect to branch on the new behavior enum and adds a UseDefaultConnection strategy.
  • ConnectionConfig.validateDefaultConnectionId warns and prompts the user (select connection / change setting) when the configuration is invalid.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
extensions/mssql/package.json Adds new settings (newEditorConnectionBehavior, defaultConnectionId) and deprecation metadata on the old setting.
extensions/mssql/package.nls.json Adds localized strings for the new settings and deprecation.
extensions/mssql/src/constants/constants.ts Adds setting keys and NewEditorConnectionBehavior enum; marks old constant deprecated.
extensions/mssql/src/constants/locConstants.ts Adds connection-default warning/prompt strings.
extensions/mssql/src/controllers/mainController.ts Calls one-shot migration from the old setting on activation and reports new behavior via telemetry.
extensions/mssql/src/controllers/sqlDocumentService.ts Rewires connection precedence in handleNewQueryCommand/onDidOpenTextDocument, adds UseDefaultConnection strategy and helper getters; removes OE-single-selection fallback.
extensions/mssql/src/connectionconfig/connectionconfig.ts Adds validateDefaultConnectionId + prompt helpers fired during initialization.
extensions/mssql/src/sharedInterfaces/telemetry.ts Adds MigrateEditorConnectionBehavior action.
extensions/mssql/test/unit/sqlDocumentService.test.ts Replaces old setting tests with comprehensive coverage of new behavior and UseDefaultConnection.
extensions/mssql/test/unit/mainController.test.ts New suite covering migration of the old setting across scopes and failure paths.
extensions/mssql/test/unit/connectionConfig.test.ts Adds coverage for validateDefaultConnectionId prompts and follow-up actions.
extensions/mssql/l10n/bundle.l10n.json, localization/xliff/vscode-mssql.xlf Auto-generated localization entries (not reviewed per repo guidelines).
Comments suppressed due to low confidence (1)

extensions/mssql/src/controllers/sqlDocumentService.ts:162

  • This change removes the previous Case 3 — when the user runs "New Query" from the command palette with no last‑active connection but with exactly one selected node in the Object Explorer tree, the new editor used that node's connection profile. After this PR, that fallback is gone for every behavior value (transferActive, none, defaultConnection); the only remaining OE path is a direct right‑click that passes node in. This is a user-facing behavior regression that isn't called out in the PR description ("Replaces transferActiveEditorConnections setting…"). If the removal is intentional, please call it out explicitly; otherwise consider keeping the OE single-selection branch as a context source (e.g., before falling through to PromptForConnection in the else branch). The updated test handleNewQueryCommand uses OE selection when exactly one node is selected and behavior is transferActive now asserts PromptForConnection, which silently encodes this behavior change.
        const behavior = this.getNewEditorConnectionBehavior();

        if (node) {
            // Case 1: User right-clicked on an OE node and selected "New Query"
            nodeType = node.nodeType;
            connectionStrategy = ConnectionStrategy.CopyConnectionFromInfo;
            sourceNode = node;
        } else if (behavior === Constants.NewEditorConnectionBehavior.DefaultConnection) {
            // Case 2: Use the configured default connection
            nodeType = "defaultConnection";
            connectionStrategy = ConnectionStrategy.UseDefaultConnection;
        } else if (
            behavior === Constants.NewEditorConnectionBehavior.TransferActive &&
            this._lastActiveConnectionInfo
        ) {
            // Case 3: User triggered "New Query" from command palette and the active document has a connection
            nodeType = "previousEditor";
            connectionStrategy = ConnectionStrategy.CopyLastActive;
        } else {
            // Case 4: User triggered "New Query" from command palette and there's no reasonable context
            connectionStrategy = ConnectionStrategy.PromptForConnection;
        }

Comment on lines +126 to +164
if (selected) {
await this._vscodeWrapper.setConfiguration(
Constants.extensionName,
"defaultConnectionId",
selected.profile.id,
vscode.ConfigurationTarget.Global,
);
}
}

/** Shows a QuickPick to switch newEditorConnectionBehavior away from 'defaultConnection'. */
private async promptChangeEditorConnectionBehavior(): Promise<void> {
interface BehaviorQuickPickItem extends vscode.QuickPickItem {
value: string;
}

const items: BehaviorQuickPickItem[] = [
{
label: LocalizedConstants.Connection.defaultConnectionBehaviorTransferActive,
value: Constants.NewEditorConnectionBehavior.TransferActive,
},
{
label: LocalizedConstants.Connection.defaultConnectionBehaviorNone,
value: Constants.NewEditorConnectionBehavior.None,
},
];

const selected = await this._vscodeWrapper.showQuickPick<BehaviorQuickPickItem>(items, {
placeHolder: LocalizedConstants.Connection.defaultConnectionChangeSettingPlaceholder,
});

if (selected) {
await this._vscodeWrapper.setConfiguration(
Constants.extensionName,
"newEditorConnectionBehavior",
selected.value,
vscode.ConfigurationTarget.Global,
);
}
Comment on lines +51 to 53
void this.validateDefaultConnectionId();

this.initialized.resolve();
Comment on lines 1736 to 1742
"mssql.transferActiveEditorConnections": {
"type": "boolean",
"default": true,
"description": "%mssql.transferActiveEditorConnections.description%",
"deprecationMessage": "%mssql.transferActiveEditorConnections.deprecationMessage%",
"scope": "application"
},
Comment on lines +408 to +428
if (behavior === Constants.NewEditorConnectionBehavior.DefaultConnection) {
const defaultProfile = await this.getDefaultConnectionProfile();
if (defaultProfile) {
this._logger.info("Auto-connecting opened SQL document from default connection", {
uri: docUri,
});
await this._connectionMgr.connect(docUri, Utils.deepClone(defaultProfile));
return;
}
// Default connection is not configured or invalid — fall through to transferActive
}

// behavior === TransferActive (or defaultConnection fallback): requires a last active connection
if (!this._lastActiveConnectionInfo) {
return;
}

this._logger.info("Auto-connecting opened SQL document from last active connection", {
uri: docUri,
});
await this._connectionMgr.connect(docUri, Utils.deepClone(this._lastActiveConnectionInfo));
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 19, 2026

PR Changes

Category Target Branch PR Branch Difference
vscode-mssql VSIX 78642 KB 78604 KB ⚪ -38 KB ( 0% )
sql-database-projects VSIX 6309 KB 6310 KB ⚪ 1 KB ( 0% )
data-workspace VSIX 535 KB 535 KB ⚪ 0 KB ( 0% )
keymap VSIX 7 KB 7 KB ⚪ 0 KB ( 0% )

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 19, 2026

Codecov Report

❌ Patch coverage is 95.36232% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.99%. Comparing base (fb3543a) to head (1c5b121).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
extensions/mssql/src/controllers/mainController.ts 89.02% 9 Missing ⚠️
...ons/mssql/src/connectionconfig/connectionconfig.ts 96.49% 4 Missing ⚠️
...nsions/mssql/src/controllers/sqlDocumentService.ts 97.05% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #22140      +/-   ##
==========================================
+ Coverage   74.88%   74.99%   +0.11%     
==========================================
  Files         388      396       +8     
  Lines      121083   121881     +798     
  Branches     7299     7348      +49     
==========================================
+ Hits        90668    91404     +736     
- Misses      30415    30477      +62     
Flag Coverage Δ
data-workspace 77.10% <ø> (ø)
mssql 74.70% <95.36%> (+0.13%) ⬆️
sqlproj 77.33% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
extensions/mssql/src/constants/constants.ts 100.00% <100.00%> (ø)
extensions/mssql/src/constants/locConstants.ts 75.57% <100.00%> (+0.20%) ⬆️
extensions/mssql/src/sharedInterfaces/telemetry.ts 100.00% <100.00%> (ø)
...nsions/mssql/src/controllers/sqlDocumentService.ts 89.48% <97.05%> (+0.17%) ⬆️
...ons/mssql/src/connectionconfig/connectionconfig.ts 93.28% <96.49%> (+0.93%) ⬆️
extensions/mssql/src/controllers/mainController.ts 38.64% <89.02%> (+1.17%) ⬆️

... and 10 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants