github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/cluster/replication_service.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cluster 16 17 import ( 18 "context" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/mysql_db" 22 "github.com/sirupsen/logrus" 23 "google.golang.org/grpc/codes" 24 "google.golang.org/grpc/status" 25 26 replicationapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/replicationapi/v1alpha1" 27 "github.com/dolthub/dolt/go/libraries/utils/filesys" 28 ) 29 30 type BranchControlPersistence interface { 31 LoadData(context.Context, []byte, bool) error 32 SaveData(context.Context, filesys.Filesys) error 33 } 34 35 type replicationServiceServer struct { 36 replicationapi.UnimplementedReplicationServiceServer 37 38 mysqlDb *mysql_db.MySQLDb 39 lgr *logrus.Entry 40 41 ctxFactory func(context.Context) (*sql.Context, error) 42 43 branchControl BranchControlPersistence 44 branchControlFilesys filesys.Filesys 45 46 dropDatabase func(*sql.Context, string) error 47 } 48 49 func (s *replicationServiceServer) UpdateUsersAndGrants(ctx context.Context, req *replicationapi.UpdateUsersAndGrantsRequest) (*replicationapi.UpdateUsersAndGrantsResponse, error) { 50 sqlCtx, err := s.ctxFactory(ctx) 51 if err != nil { 52 return nil, err 53 } 54 55 ed := s.mysqlDb.Editor() 56 defer ed.Close() 57 err = s.mysqlDb.OverwriteUsersAndGrantData(sqlCtx, ed, req.SerializedContents) 58 if err != nil { 59 return nil, err 60 } 61 err = s.mysqlDb.Persist(sqlCtx, ed) 62 if err != nil { 63 return nil, err 64 } 65 return &replicationapi.UpdateUsersAndGrantsResponse{}, nil 66 } 67 68 func (s *replicationServiceServer) UpdateBranchControl(ctx context.Context, req *replicationapi.UpdateBranchControlRequest) (*replicationapi.UpdateBranchControlResponse, error) { 69 sqlCtx, err := s.ctxFactory(ctx) 70 if err != nil { 71 return nil, err 72 } 73 74 err = s.branchControl.LoadData(sqlCtx, req.SerializedContents /* isFirstLoad */, false) 75 if err != nil { 76 return nil, err 77 } 78 err = s.branchControl.SaveData(sqlCtx, s.branchControlFilesys) 79 if err != nil { 80 return nil, err 81 } 82 return &replicationapi.UpdateBranchControlResponse{}, nil 83 } 84 85 func (s *replicationServiceServer) DropDatabase(ctx context.Context, req *replicationapi.DropDatabaseRequest) (*replicationapi.DropDatabaseResponse, error) { 86 if s.dropDatabase == nil { 87 return nil, status.Error(codes.Unimplemented, "unimplemented") 88 } 89 90 sqlCtx, err := s.ctxFactory(ctx) 91 if err != nil { 92 return nil, err 93 } 94 95 err = s.dropDatabase(sqlCtx, req.Name) 96 s.lgr.Tracef("dropped database [%s] through sqle.DropDatabase. err: %v", req.Name, err) 97 if err != nil && !sql.ErrDatabaseNotFound.Is(err) { 98 return nil, err 99 } 100 return &replicationapi.DropDatabaseResponse{}, nil 101 }