github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/binlogreplication/binlog_metadata_persistence.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 binlogreplication 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/mysql_db" 22 23 "github.com/dolthub/dolt/go/libraries/doltcore/sqlserver" 24 ) 25 26 // persistReplicationConfiguration saves the specified |replicaSourceInfo| to disk; if any problems are encountered 27 // while saving to disk, an error is returned. 28 func persistReplicationConfiguration(ctx *sql.Context, replicaSourceInfo *mysql_db.ReplicaSourceInfo) error { 29 server := sqlserver.GetRunningServer() 30 if server == nil { 31 return fmt.Errorf("no SQL server running; " + 32 "replication commands may only be used when running from dolt sql-server, and not from dolt sql") 33 } 34 engine := server.Engine 35 36 mysqlDb := engine.Analyzer.Catalog.MySQLDb 37 ed := mysqlDb.Editor() 38 defer ed.Close() 39 ed.PutReplicaSourceInfo(replicaSourceInfo) 40 return mysqlDb.Persist(ctx, ed) 41 } 42 43 // loadReplicationConfiguration loads the replication configuration for default channel (""). 44 func loadReplicationConfiguration(_ *sql.Context) (*mysql_db.ReplicaSourceInfo, error) { 45 server := sqlserver.GetRunningServer() 46 if server == nil { 47 return nil, fmt.Errorf("no SQL server running; " + 48 "replication commands may only be used when running from dolt sql-server, and not from dolt sql") 49 } 50 engine := server.Engine 51 mysqlDb := engine.Analyzer.Catalog.MySQLDb 52 rd := mysqlDb.Reader() 53 defer rd.Close() 54 55 rsi, ok := rd.GetReplicaSourceInfo(mysql_db.ReplicaSourceInfoPrimaryKey{ 56 Channel: "", 57 }) 58 if ok { 59 return rsi, nil 60 } 61 62 return nil, nil 63 } 64 65 // deleteReplicationConfiguration deletes all replication configuration for the default channel (""). 66 func deleteReplicationConfiguration(ctx *sql.Context) error { 67 server := sqlserver.GetRunningServer() 68 if server == nil { 69 return fmt.Errorf("no SQL server running; " + 70 "replication commands may only be used when running from dolt sql-server, and not from dolt sql") 71 } 72 engine := server.Engine 73 mysqlDb := engine.Analyzer.Catalog.MySQLDb 74 ed := mysqlDb.Editor() 75 defer ed.Close() 76 77 ed.RemoveReplicaSourceInfo(mysql_db.ReplicaSourceInfoPrimaryKey{}) 78 79 return engine.Analyzer.Catalog.MySQLDb.Persist(ctx, ed) 80 } 81 82 // persistSourceUuid saves the specified |sourceUuid| to a persistent storage location. 83 func persistSourceUuid(ctx *sql.Context, sourceUuid string) error { 84 replicaSourceInfo, err := loadReplicationConfiguration(ctx) 85 if err != nil { 86 return err 87 } 88 89 replicaSourceInfo.Uuid = sourceUuid 90 return persistReplicationConfiguration(ctx, replicaSourceInfo) 91 }