SQL Notebooks reconnect to original database#21475
Conversation
There was a problem hiding this comment.
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
workspaceStateand restore it when creating a newNotebookConnectionManager. - Add reconnection-context support in
NotebookConnectionManagerto restore the saved database when the chosen profile has no database specified. - Improve stale-connection handling to attempt a silent reconnect using stored
connectionInfobefore 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.
PR Changes
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
| try { | ||
| await connMgr.ensureConnection(); | ||
| this.connectCellsForIntellisense(notebook); | ||
| this.saveConnectionMetadataIfConnected(notebook); |
There was a problem hiding this comment.
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.
| 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}`, | ||
| ); | ||
| }); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
this connection label should match the codelens format we use elsewhere: server,port | database
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.
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
npm run test)Reviewers: Please read our reviewer guidelines