Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(LSP): implement setMetadata command for identifier instance and database (#9045)
rebelicebc27552a1f81
diff --git a/backend/api/lsp/commands.go b/backend/api/lsp/commands.gonew file mode 100644index 0000000000..07c0644a90--- /dev/null+++ b/backend/api/lsp/commands.go@@ -0,0 +1,26 @@+package lsp++import "github.com/sourcegraph/go-lsp"++type CommandName string++const (+ CommandNameSetMetadata CommandName = "setMetadata"+)++// SetMetadataCommandParams are the parameters to the "setMetadata" command.+type SetMetadataCommandParams struct {+ lsp.ExecuteCommandParams+ Arguments []SetMetadataCommandArguments `json:"arguments,omitempty"`+}++// SetMetadataCommandArguments are the arguments to the "setMetadata" command.+type SetMetadataCommandArguments struct {+ // The InstanceID is the instance ID to set metadata for.+ // Format: instances/{instance}+ InstanceID string `json:"instanceId,omitempty"`+ // The DatabaseName is the connection database name.+ // For PostgreSQL, it's required.+ // For other database engines, it's optional.+ DatabaseName string `json:"databaseName,omitempty"`+}diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.goindex 0377e9bf5d..5f5cec7b95 100644--- a/backend/api/lsp/handler.go+++ b/backend/api/lsp/handler.go@@ -15,11 +15,12 @@ import (type Method stringconst (- LSPMethodInitialize Method = "initialize"- LSPMethodInitialized Method = "initialized"- LSPMethodShutdown Method = "shutdown"- LSPMethodExit Method = "exit"- LSPMethodCancelRequest Method = "$/cancelRequest"+ LSPMethodInitialize Method = "initialize"+ LSPMethodInitialized Method = "initialized"+ LSPMethodShutdown Method = "shutdown"+ LSPMethodExit Method = "exit"+ LSPMethodCancelRequest Method = "$/cancelRequest"+ LSPMethodExecuteCommand Method = "workspace/executeCommand"LSPMethodTextDocumentDidOpen Method = "textDocument/didOpen"LSPMethodTextDocumentDidChange Method = "textDocument/didChange"@@ -46,9 +47,10 @@ func (h lspHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrp// Handler handles Language Server Protocol requests.type Handler struct {- mu sync.Mutex- fs *MemFS- init *lsp.InitializeParams // set by LSPMethodInitialize request+ mu sync.Mutex+ fs *MemFS+ init *lsp.InitializeParams // set by LSPMethodInitialize request+ metadata *SetMetadataCommandArgumentsshutDown bool}@@ -64,6 +66,12 @@ func (h *Handler) ShutDown() {h.fs = nil}+func (h *Handler) setMetadata(arg SetMetadataCommandArguments) {+ h.mu.Lock()+ defer h.mu.Unlock()+ h.metadata = &arg+}+func (h *Handler) checkInitialized(req *jsonrpc2.Request) error {h.mu.Lock()defer h.mu.Unlock()@@ -124,6 +132,9 @@ func (h *Handler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2CompletionProvider: &lsp.CompletionOptions{TriggerCharacters: []string{"."},},+ ExecuteCommandProvider: &lsp.ExecuteCommandOptions{+ Commands: []string{string(CommandNameSetMetadata)},+ },},}, nilcase LSPMethodInitialized:@@ -139,6 +150,29 @@ func (h *Handler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2case LSPMethodCancelRequest:// Do nothing for now.return nil, nil+ case LSPMethodExecuteCommand:+ if req.Params == nil {+ return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams}+ }+ var params lsp.ExecuteCommandParams+ if err := json.Unmarshal(*req.Params, ¶ms); err != nil {+ return nil, err+ }++ switch CommandName(params.Command) {+ case CommandNameSetMetadata:+ var setMetadataParams SetMetadataCommandParams+ if err := json.Unmarshal(*req.Params, &setMetadataParams); err != nil {+ return nil, err+ }+ if len(setMetadataParams.Arguments) != 1 {+ return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams, Message: "expected exactly one argument"}+ }+ h.setMetadata(setMetadataParams.Arguments[0])+ return nil, nil+ default:+ return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams, Message: fmt.Sprintf("command not supported: %s", params.Command)}+ }default:if isFileSystemRequest(req.Method) {_, _, err := h.handleFileSystemRequest(ctx, req)