@@ -11,33 +11,35 @@ import (
1111 "github.com/BurntSushi/toml"
1212)
1313
14+ // .config/sqlrsync/defaults.toml
1415type DefaultsConfig struct {
1516 Defaults struct {
1617 Server string `toml:"server"`
1718 } `toml:"defaults"`
1819}
1920
21+ // .config/sqlrsync/local-secrets.toml
2022type LocalSecretsConfig struct {
21- Local struct {
22- Hostname string `toml:"hostname"`
23- DefaultClientSideEncryptionKey string `toml:"defaultClientSideEncryptionKey"`
24- } `toml:"local"`
2523 SQLRsyncDatabases []SQLRsyncDatabase `toml:"sqlrsync-databases"`
2624}
2725
2826type SQLRsyncDatabase struct {
29- Path string `toml:"path"`
30- ReplicaID string `toml:"replicaID,omitempty"`
31- ClientSideEncryptionKey string `toml:"clientSideEncryptionKey,omitempty"`
32- LastUpdated time.Time `toml:"lastUpdated,omitempty"`
33- Server string `toml:"server,omitempty"`
27+ LocalPath string `toml:"path,omitempty"`
28+ Server string `toml:"server"`
29+ CustomerSuppliedEncryptionKey string `toml:"customerSuppliedEncryptionKey,omitempty"`
30+ ReplicaID string `toml:"replicaID"`
31+ RemotePath string `toml:"remotePath,omitempty"`
32+ PushKey string `toml:"pushKey,omitempty"`
33+ LastPush time.Time `toml:"lastPush,omitempty"`
3434}
3535
3636// DashSQLRsync manages the -sqlrsync file for a database
3737type DashSQLRsync struct {
3838 DatabasePath string
3939 RemotePath string
4040 PullKey string
41+ Server string
42+ ReplicaID string
4143}
4244
4345func GetConfigDir () (string , error ) {
@@ -176,7 +178,7 @@ func SaveLocalSecretsConfig(config *LocalSecretsConfig) error {
176178
177179func (c * LocalSecretsConfig ) FindDatabaseByPath (path string ) * SQLRsyncDatabase {
178180 for i := range c .SQLRsyncDatabases {
179- if c .SQLRsyncDatabases [i ].Path == path {
181+ if c .SQLRsyncDatabases [i ].LocalPath == path {
180182 return & c .SQLRsyncDatabases [i ]
181183 }
182184 }
@@ -185,7 +187,7 @@ func (c *LocalSecretsConfig) FindDatabaseByPath(path string) *SQLRsyncDatabase {
185187
186188func (c * LocalSecretsConfig ) UpdateOrAddDatabase (db SQLRsyncDatabase ) {
187189 for i := range c .SQLRsyncDatabases {
188- if c .SQLRsyncDatabases [i ].Path == db .Path {
190+ if c .SQLRsyncDatabases [i ].LocalPath == db .LocalPath {
189191 // Update existing database
190192 c .SQLRsyncDatabases [i ] = db
191193 return
@@ -197,24 +199,20 @@ func (c *LocalSecretsConfig) UpdateOrAddDatabase(db SQLRsyncDatabase) {
197199
198200func (c * LocalSecretsConfig ) RemoveDatabase (path string ) {
199201 for i , db := range c .SQLRsyncDatabases {
200- if db .Path == path {
202+ if db .LocalPath == path {
201203 // Remove database from slice
202204 c .SQLRsyncDatabases = append (c .SQLRsyncDatabases [:i ], c .SQLRsyncDatabases [i + 1 :]... )
203205 return
204206 }
205207 }
206208}
207209
208- func (c * LocalSecretsConfig ) SetHostname (hostname string ) {
209- c .Local .Hostname = hostname
210- }
211-
212- func (c * LocalSecretsConfig ) SetDefaultEncryptionKey (key string ) {
213- c .Local .DefaultClientSideEncryptionKey = key
214- }
215-
216210// NewDashSQLRsync creates a new DashSQLRsync instance for the given database path
217211func NewDashSQLRsync (databasePath string ) * DashSQLRsync {
212+ if (strings .Contains (databasePath , "@" )) {
213+ databasePath = strings .Split (databasePath , "@" )[0 ]
214+ }
215+
218216 return & DashSQLRsync {
219217 DatabasePath : databasePath ,
220218 }
@@ -246,21 +244,27 @@ func (d *DashSQLRsync) Read() error {
246244 scanner := bufio .NewScanner (file )
247245 for scanner .Scan () {
248246 line := strings .TrimSpace (scanner .Text ())
249-
247+
250248 if strings .HasPrefix (line , "#" ) || line == "" {
251249 continue
252250 }
253-
251+
254252 if strings .HasPrefix (line , "sqlrsync " ) {
255253 parts := strings .Fields (line )
256254 if len (parts ) >= 2 {
257255 d .RemotePath = parts [1 ]
258256 }
259-
257+
260258 for _ , part := range parts {
261259 if strings .HasPrefix (part , "--pullKey=" ) {
262260 d .PullKey = strings .TrimPrefix (part , "--pullKey=" )
263261 }
262+ if strings .HasPrefix (part , "--replicaID=" ) {
263+ d .ReplicaID = strings .TrimPrefix (part , "--replicaID=" )
264+ }
265+ if strings .HasPrefix (part , "--server=" ) {
266+ d .Server = strings .TrimPrefix (part , "--server=" )
267+ }
264268 }
265269 break
266270 }
@@ -270,14 +274,17 @@ func (d *DashSQLRsync) Read() error {
270274}
271275
272276// Write writes the -sqlrsync file with the given remote path and pull key
273- func (d * DashSQLRsync ) Write (remotePath string , pullKey string ) error {
277+ func (d * DashSQLRsync ) Write (remotePath string , localName string , replicaID string , pullKey string , serverURL string ) error {
274278 d .RemotePath = remotePath
275279 d .PullKey = pullKey
276280
281+ localNameTree := strings .Split (localName , "/" )
282+ localName = localNameTree [len (localNameTree )- 1 ]
283+
277284 content := fmt .Sprintf (`#!/bin/bash
278- # https://sqlrsync.com/docs/pullfile
279- sqlrsync %s -- pullKey=%s
280- ` , remotePath , pullKey )
285+ # https://sqlrsync.com/docs/-sqlrsync
286+ sqlrsync %s %s --replicaID=%s -- pullKey=%s --server =%s
287+ ` , remotePath , localName , replicaID , pullKey , serverURL )
281288
282289 if err := os .WriteFile (d .FilePath (), []byte (content ), 0755 ); err != nil {
283290 return fmt .Errorf ("failed to write -sqlrsync file: %w" , err )
@@ -292,4 +299,4 @@ func (d *DashSQLRsync) Remove() error {
292299 return nil
293300 }
294301 return os .Remove (d .FilePath ())
295- }
302+ }
0 commit comments