Skip to content

SQL Notebooks reconnect to original database#21475

Merged
lewis-sanchez merged 5 commits into
mainfrom
lewissanchez/sql-notebooks/reconnect-to-db
Mar 6, 2026
Merged

SQL Notebooks reconnect to original database#21475
lewis-sanchez merged 5 commits into
mainfrom
lewissanchez/sql-notebooks/reconnect-to-db

Conversation

@lewis-sanchez
Copy link
Copy Markdown
Contributor

@lewis-sanchez lewis-sanchez commented Mar 5, 2026

Description

This PR fixes #21333

Connection metadata (server + database) is persisted using a VS Code Memento (ExtensionContext.workspaceState), which is a simple key-value store scoped to the current workspace.

SQL Notebooks reconnect to original database

Provide a clear, concise summary of the changes in this PR. What problem does it solve? Why is it needed? Link any related issues using issue closing keywords.

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

This PR addresses issue #21333 by persisting SQL Notebook connection metadata so that reopening a notebook can reconnect to the originally-used database even when the user selects an existing server-level saved connection profile.

Changes:

  • Persist notebook connection metadata (server + database) to workspaceState and restore it when creating a new NotebookConnectionManager.
  • Add reconnection-context support in NotebookConnectionManager to restore the saved database when the chosen profile has no database specified.
  • Improve stale-connection handling to attempt a silent reconnect using stored connectionInfo before prompting the user.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
extensions/mssql/src/notebooks/sqlNotebookController.ts Persist/restore notebook connection metadata; re-key connection managers when notebook URI changes after save.
extensions/mssql/src/notebooks/notebookConnectionManager.ts Add reconnection context + silent reconnect path for stale connections; restore database for server-level profiles.
extensions/mssql/src/controllers/mainController.ts Inject ExtensionContext.workspaceState into SqlNotebookController for metadata persistence.
extensions/mssql/test/unit/notebooks/sqlNotebookController.test.ts Add unit tests for workspaceState persistence and reconnection-context restoration behavior.
extensions/mssql/test/unit/notebooks/notebookConnectionManager.test.ts Update/add unit tests for silent reconnect and reconnection-context database restoration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread extensions/mssql/src/notebooks/sqlNotebookController.ts Outdated
Comment thread extensions/mssql/src/notebooks/sqlNotebookController.ts Outdated
Comment thread extensions/mssql/src/notebooks/sqlNotebookController.ts
laurenastrid1
laurenastrid1 previously approved these changes Mar 5, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 5, 2026

PR Changes

Category Target Branch PR Branch Difference
vscode-mssql VSIX 6401 KB 6401 KB ⚪ 0 KB ( 0% )
sql-database-projects VSIX 7024 KB 7024 KB ⚪ 0 KB ( 0% )
data-workspace VSIX 535 KB 535 KB ⚪ 0 KB ( 0% )

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.58228% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.54%. Comparing base (88b90f4) to head (30d4fc5).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...sions/mssql/src/notebooks/sqlNotebookController.ts 66.97% 36 Missing ⚠️
extensions/mssql/src/controllers/mainController.ts 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main   #21475    +/-   ##
========================================
  Coverage   72.53%   72.54%            
========================================
  Files         330      330            
  Lines       97997    98154   +157     
  Branches     5435     5449    +14     
========================================
+ Hits        71084    71204   +120     
- Misses      26913    26950    +37     
Files with missing lines Coverage Δ
...s/mssql/src/notebooks/notebookConnectionManager.ts 99.14% <100.00%> (+0.13%) ⬆️
extensions/mssql/src/controllers/mainController.ts 30.35% <0.00%> (-0.01%) ⬇️
...sions/mssql/src/notebooks/sqlNotebookController.ts 73.16% <66.97%> (-0.98%) ⬇️
🚀 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.

@lewis-sanchez lewis-sanchez merged commit ce2d985 into main Mar 6, 2026
3 checks passed
@lewis-sanchez lewis-sanchez deleted the lewissanchez/sql-notebooks/reconnect-to-db branch March 6, 2026 19:29
try {
await connMgr.ensureConnection();
this.connectCellsForIntellisense(notebook);
this.saveConnectionMetadataIfConnected(notebook);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this is always called with connectCellsForIntellisense(). Pull those two lines into a helper function to make sure that future changes don't miss one of them.

Comment on lines +351 to +360
this._workspaceState
.update(key, {
server: info.server,
database: info.database,
})
.then(undefined, (err) => {
this.log.warn(
`[saveConnectionMetadataIfConnected] Failed to persist connection metadata for ${notebook.uri.toString()}: ${err?.message ?? err}`,
);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's use more modern syntax here (try/catch, async):

        void (async () => {
            try {
                await this._workspaceState.update(key, {
                    server: info.server,
                    database: info.database,
                });
            } catch (err) {
                this.log.warn(
                    `[saveConnectionMetadataIfConnected] Failed to persist connection metadata for ${notebook.uri.toString()}: ${getErrorMessage(err)}`,
                );
            }
        })();

const uri = await this.connectInternal(this.connectionInfo);
this.connectionUri = uri;
const actualDb = this.getActualDatabase(uri);
this.connectionLabel = formatConnectionLabel(this.connectionInfo.server, actualDb);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this connection label should match the codelens format we use elsewhere: server,port | database

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.

[Bug]: SQL Notebook can only connect to a saved connection profile

5 participants