Skip to content

Add getActiveDatabase and getDatabaseForConnectionId methods to connection sharing API #19985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 20, 2025

This PR adds two new methods to the connection sharing API that allow extensions to programmatically retrieve database information:

  • getActiveDatabase(extensionId: string): Promise<string | undefined> - Returns the database name for the connection in the active editor window
  • getDatabaseForConnectionId(extensionId: string, connectionId: string): Promise<string | undefined> - Returns the database name for a specific connection ID

Problem

Extensions using the connection sharing API previously had no way to determine which database is currently active in the connected editor or retrieve database information for specific connections. The existing methods (getServerInfo, listDatabases, executeSimpleQuery) don't provide this information:

  • getServerInfo returns server details but not the current database
  • listDatabases shows available databases but not which one is active
  • executeSimpleQuery with SELECT DB_NAME() returns the default database from the connection string, not the active one in the UI

This gap prevented extensions from maintaining state continuity when migrating from Azure Data Studio and accessing database information for specific connections.

Solution

Added two new methods that:

  • Return the database name as a string if connection exists
  • Return undefined if no connection exists or database information is unavailable
  • Throw appropriate errors for missing permissions or no active editor (for getActiveDatabase)
  • Follow the same validation patterns as existing connection sharing methods

Usage

// Command approach
const activeDatabase = await vscode.commands.executeCommand(
    'mssql.connectionSharing.getActiveDatabase',
    extensionId
);

const connectionDatabase = await vscode.commands.executeCommand(
    'mssql.connectionSharing.getDatabaseForConnectionId',
    extensionId,
    connectionId
);

// Direct API approach
const mssqlApi = vscode.extensions.getExtension('ms-mssql.mssql').exports;
const activeDatabase = await mssqlApi.connectionSharing.getActiveDatabase(extensionId);
const connectionDatabase = await mssqlApi.connectionSharing.getDatabaseForConnectionId(extensionId, connectionId);

Implementation

The implementation follows existing patterns in the codebase:

  • Added interface definitions to typings/vscode-mssql.d.ts
  • Implemented methods in ConnectionSharingService class with proper error handling
  • Exposed methods in the public API via extension.ts
  • Updated connection sharing sample with usage examples

Total changes: 95 lines across 4 files, maintaining minimal footprint and surgical precision.

Fixes #19971.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] [Feature Request]: Add getActiveDatabase to exposed functions Add getActiveDatabase method to connection sharing API Aug 20, 2025
@Copilot Copilot AI requested a review from aasimkhan30 August 20, 2025 07:40
Copilot finished work on behalf of aasimkhan30 August 20, 2025 07:40
@rayzinnz
Copy link

May I request this function gets moved from mssqlApi.connectionSharing.getActiveDatabase(); to mssqlApi.getActiveDatabase();. The reason for this request as I feel permissions for connection sharing should not be required for getting the active database name of the active editor.

@aasimkhan30
Copy link
Contributor

@rayzinnz since this is part of the connection info, we’d like to keep it consolidated within the connection sharing API. Over time, we’ll phase out the other APIs currently exposed through mssqlApi, so please plan extension features accordingly.
Once a user grants an extension permission to use the connection APIs, it shouldn’t prompt them for the permissions again.

@aasimkhan30
Copy link
Contributor

@copilot, can you also add an api that gets you database for a connection id?

Copy link
Contributor Author

Copilot AI commented Aug 20, 2025

@copilot, can you also add an api that gets you database for a connection id?

I've added the getDatabaseForConnectionId(extensionId: string, connectionId: string): Promise<string | undefined> method to the connection sharing API. This method takes a connection ID and returns the database name for that connection from the connection store.

The implementation follows the same validation patterns as existing methods and returns undefined if the connection is not found. I've also updated the sample with usage examples showing both command and direct API approaches.

Commit: e5c2fcf

@Copilot Copilot AI changed the title Add getActiveDatabase method to connection sharing API Add getActiveDatabase and getDatabaseForConnectionId methods to connection sharing API Aug 20, 2025
Copilot finished work on behalf of aasimkhan30 August 20, 2025 23:46
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.

[Feature Request]: Add getActiveDatabase to exposed functions
3 participants