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(cosmos): support cosmosdb query simply (#14912)
h3n4lf2ff57fbb762
diff --git a/backend/plugin/db/cosmosdb/cosmosdb.go b/backend/plugin/db/cosmosdb/cosmosdb.goindex 8573d77289..f206d09892 100644--- a/backend/plugin/db/cosmosdb/cosmosdb.go+++ b/backend/plugin/db/cosmosdb/cosmosdb.go@@ -4,6 +4,7 @@ package cosmosdbimport ("context""database/sql"+ "encoding/json""io""google.golang.org/grpc/codes"@@ -85,6 +86,49 @@ func (*Driver) Dump(_ context.Context, _ io.Writer, _ *storepb.DatabaseSchemaMet}// QueryConn queries a SQL statement in a given connection.-func (*Driver) QueryConn(_ context.Context, _ *sql.Conn, _ string, _ db.QueryContext) ([]*v1pb.QueryResult, error) {- return nil, status.Errorf(codes.Unimplemented, "method QueryConn unimplemented")+func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {+ if queryContext.Container == "" {+ return nil, status.Errorf(codes.InvalidArgument, "container argument is required for CosmosDB")+ }+ container, err := driver.client.NewContainer(driver.databaseName, queryContext.Container)+ if err != nil {+ return nil, status.Error(codes.Internal, errors.Wrapf(err, "failed to create container").Error())+ }+ pager := container.NewCrossPartitionQueryItemsPager(statement, nil)+ result := &v1pb.QueryResult{+ ColumnNames: []string{"id", "category"},+ ColumnTypeNames: []string{"string", "string"},+ }+ for pager.More() {+ response, err := pager.NextPage(ctx)+ if err != nil {+ return nil, status.Error(codes.Internal, errors.Wrapf(err, "failed to read more items").Error())+ }+ // TODO(zp): test only.+ type Item struct {+ ID string `json:"id"`+ Category string `json:"category"`+ }+ for _, bytes := range response.Items {+ var item Item+ if err := json.Unmarshal(bytes, &item); err != nil {+ return nil, status.Error(codes.Internal, errors.Wrapf(err, "failed to unmarshal JSON").Error())+ }+ result.Rows = append(result.Rows, &v1pb.QueryRow{+ Values: []*v1pb.RowValue{+ {+ Kind: &v1pb.RowValue_StringValue{+ StringValue: item.ID,+ },+ },+ {+ Kind: &v1pb.RowValue_StringValue{+ StringValue: item.Category,+ },+ },+ },+ })+ }+ }+ return []*v1pb.QueryResult{result}, nil}diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 25051873a3..df10a19841 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -251,6 +251,9 @@ type QueryContext struct {// Schema is the specific schema for the query.// Mainly used for the search path of PostgreSQL.Schema string+ // Container is the specific container for the query.+ // Mainly used for CosmosDB.+ Container string// Limit is the maximum row count returned. No limit enforced if limit <= 0Limit intExplain booldiff --git a/go.mod b/go.modindex de254e55e9..ca58b2eb81 100644--- a/go.mod+++ b/go.mod@@ -117,7 +117,7 @@ require (github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirectgithub.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.3.2 // indirectgithub.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect- github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect+ github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirectgithub.com/BurntSushi/toml v1.4.0 // indirectgithub.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.0 // indirectgithub.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.2 // indirect@@ -370,3 +370,5 @@ replace (replace github.com/microsoft/go-mssqldb => github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898replace github.com/youmark/pkcs8 => github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94++replace github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos => github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20250107031355-540f7300ab93diff --git a/go.sum b/go.sumindex c7e7dd43bf..685136bc1d 100644--- a/go.sum+++ b/go.sum@@ -640,8 +640,6 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 h1:B/dfvscEQtew9dVuoxqxrgithub.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0/go.mod h1:fiPSssYvltE08HJchL04dOy+RD4hgrjph0cwGGMntdI=github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0 h1:+m0M/LFxN43KvULkDNfdXOgrjtg6UYJPFBJyuEcRCAw=github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0/go.mod h1:PwOyop78lveYMRs6oCxjiVyBdyCgIYH6XHIVZO9/SFQ=-github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.2.0 h1:1y5G4XTBTEt0nKNFtM7j6CxqkY5fxSuJb/mD8Zf0gPc=-github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.2.0/go.mod h1:1Dp+C8Sly0hnhX8k5zDuw72Z2ehd9Lv+pkLFn8dgXMA=github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.5.0 h1:AifHbc4mg0x9zW52WOpKbsHaDKuRhlI7TVl47thgQ70=@@ -657,8 +655,8 @@ github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 h1:mFRzDkZVAjdal+github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=-github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU=-github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=+github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 h1:kYRSnvJju5gYVyhkij+RTJ/VR6QIUaCfWeaFm2ycsjQ=+github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=@@ -817,6 +815,8 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUgithub.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=github.com/bytebase/antlr/v4 v4.0.0-20240827034948-8c385f108920 h1:IfmPt5o5R70NKtOrs+QHOoCgViYZelZysGxVBvV4ybA=github.com/bytebase/antlr/v4 v4.0.0-20240827034948-8c385f108920/go.mod h1:ykhjIPiv0IWpu3OGXCHdz2eUSe8UNGGD6baqjs8jSuU=+github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20250107031355-540f7300ab93 h1:6lr7MhgFFWzZ5E3grbpkzJKd9bDuclYNFHfFC/CKk5c=+github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20250107031355-540f7300ab93/go.mod h1:YwUyrNUtcZcibA99JcfCP6UUp95VVQKO2MJfBzgJDwA=github.com/bytebase/gh-ost2 v0.0.0-20240731091141-e7a12a9401ab h1:hJ3tfjfm5D63QwCH+znK1ZQQzk9/rG4Jjz7CSxVtSrQ=github.com/bytebase/gh-ost2 v0.0.0-20240731091141-e7a12a9401ab/go.mod h1:VRPqaDM7UYxJbcCtQkjBcNkxW17p6LJJdkM/nYwQhqg=github.com/bytebase/go-lsp v0.0.0-20240130071507-c04b5c75010c h1:W832GNANCst4C6TgYbQnKKxbuMZKQxTD4++b3HOuzBo=