Skip to content

Commit 02ca2ca

Browse files
authored
Merge pull request #4 from sqlrsync/matt
Key mgmt refactor, pull implementation, -sqlrsync improvements
2 parents 8b43df7 + 97aaf98 commit 02ca2ca

File tree

6 files changed

+194
-153
lines changed

6 files changed

+194
-153
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
**/.claude/
1111

1212
CLAUDE.md
13-
**/CLAUDE.md
13+
**/CLAUDE.md
14+
15+
tmp/

bridge/cgo_bridge.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ func go_local_read_callback(userData unsafe.Pointer, buffer *C.uint8_t, size C.i
174174
if err.Error() != "connection lost" && err.Error() != "sync completed" {
175175
client.Logger.Error("Connection to server had a failure. Are you online? Read callback error", zap.Error(err))
176176
}
177-
return -1
177+
// For sync completion errors, return 0 to signal EOF gracefully
178+
// This allows sqlite_rsync to finish processing any buffered data
179+
return 0
178180
}
179181

180182
client.Logger.Debug("Read callback", zap.Int("bytesRead", bytesRead))

bridge/sqlite_rsync_wrapper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,13 @@ static void *websocket_read_thread(void *arg)
277277
}
278278
else if (bytes_read == 0)
279279
{
280-
// No more data, close write end to signal EOF
280+
// 0 bytes indicates EOF from the Go callback (sync completed)
281+
// Close write end to signal EOF to sqlite_rsync
281282
break;
282283
}
283284
else
284285
{
285-
// Error occurred
286+
// Negative value indicates a real error
286287
break;
287288
}
288289
}

client/config.go

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,35 @@ import (
1111
"github.com/BurntSushi/toml"
1212
)
1313

14+
// .config/sqlrsync/defaults.toml
1415
type DefaultsConfig struct {
1516
Defaults struct {
1617
Server string `toml:"server"`
1718
} `toml:"defaults"`
1819
}
1920

21+
// .config/sqlrsync/local-secrets.toml
2022
type 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

2826
type 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
3737
type DashSQLRsync struct {
3838
DatabasePath string
3939
RemotePath string
4040
PullKey string
41+
Server string
42+
ReplicaID string
4143
}
4244

4345
func GetConfigDir() (string, error) {
@@ -176,7 +178,7 @@ func SaveLocalSecretsConfig(config *LocalSecretsConfig) error {
176178

177179
func (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

186188
func (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

198200
func (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
217211
func 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

Comments
 (0)