Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
195a9e3
feat: push subscription and conflict detection. wip automerge via sq…
pnwmatt Oct 3, 2025
d15c051
feat: send wsID with client requests
pnwmatt Oct 3, 2025
dadb26b
fix: bugs with wsid on subscribe connections
pnwmatt Oct 3, 2025
0c88389
send client version
pnwmatt Oct 3, 2025
3c02f45
fix: rename token to key
pnwmatt Oct 4, 2025
aad11f7
feat: push subscription and conflict detection. wip automerge via sq…
pnwmatt Oct 3, 2025
4ffba2b
continue to resolve merge
pnwmatt Oct 6, 2025
5587d1c
fix: tweaks to usgs
pnwmatt Oct 6, 2025
91e3fe6
fix: tweaks to usgs
pnwmatt Oct 6, 2025
94a85be
allow subscribe when args=1 and local
pnwmatt Oct 6, 2025
0e8484b
rename to distinct between push and pull subscribe
pnwmatt Oct 7, 2025
8915b58
rename to distinct between push and pull subscribe, always subscribe …
pnwmatt Oct 7, 2025
7803038
minor line update
pnwmatt Oct 7, 2025
27ae098
fix: bugs with wsid on subscribe connections
pnwmatt Oct 3, 2025
f3a87d6
send client version
pnwmatt Oct 3, 2025
5929d38
fix: bugs with wsid on subscribe connections
pnwmatt Oct 3, 2025
c7001d5
send client version
pnwmatt Oct 3, 2025
9527f53
fix: rename token to key
pnwmatt Oct 4, 2025
ff5a0f2
use trackedwaitgroup
pnwmatt Oct 12, 2025
e12005e
feat: push subscription and conflict detection. wip automerge via sq…
pnwmatt Oct 3, 2025
2b022a0
feat: send wsID with client requests
pnwmatt Oct 3, 2025
4eb49b5
fix: bugs with wsid on subscribe connections
pnwmatt Oct 3, 2025
d7b9a67
send client version
pnwmatt Oct 3, 2025
e9e363d
fix: rename token to key
pnwmatt Oct 4, 2025
47ff260
feat: push subscription and conflict detection. wip automerge via sq…
pnwmatt Oct 3, 2025
b597a7d
continue to resolve merge
pnwmatt Oct 6, 2025
e3bb383
continue to resolve merge
pnwmatt Oct 6, 2025
51c7ace
fix: tweaks to usgs
pnwmatt Oct 6, 2025
259e2c4
rename to distinct between push and pull subscribe, always subscribe …
pnwmatt Oct 7, 2025
837eabf
merge
pnwmatt Oct 14, 2025
efdaf6e
resolve errors introduced in rebase
pnwmatt Oct 14, 2025
a94b016
fix: tweak how we handle key is not authorized errors
pnwmatt Oct 14, 2025
bd441e0
lint file
pnwmatt Oct 15, 2025
1d27c86
fix: change emoji
pnwmatt Oct 15, 2025
226a051
fix: bad rebase merge again, oye
pnwmatt Oct 15, 2025
59cda06
fix: don't redownload what we uploaded
pnwmatt Oct 15, 2025
3198d54
Merge pull request #17 from sqlrsync/pushSubscribe
pnwmatt Oct 15, 2025
e82aceb
fix: upgrade websocket package
pnwmatt Oct 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions bridge/cgo_sqldiff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package bridge

/*
#cgo CFLAGS: -I${SRCDIR}
#cgo LDFLAGS: -lsqlite3

#include <stdlib.h>
#include <string.h>
#include "sqldiff_wrapper.h"
*/
import "C"
import (
"fmt"
"unsafe"
)

// DiffResult contains the result of a diff operation
type DiffResult struct {
SQL string // SQL statements to transform db1 to db2
HasChanges bool // Whether there are any differences
Operations []DiffOperation // Parsed operations from the diff
Conflicts []PrimaryKeyConflict // Detected primary key conflicts
}

// DiffOperation represents a single SQL operation from the diff
type DiffOperation struct {
Type string // INSERT, UPDATE, DELETE
Table string // Table name
PrimaryKey map[string]interface{} // Primary key values
SQL string // The actual SQL statement
}

// PrimaryKeyConflict represents a conflict on the same primary key
type PrimaryKeyConflict struct {
Table string
PrimaryKey map[string]interface{}
Operation1 string // First operation (from db1)
Operation2 string // Second operation (from db2)
}

// RunSQLDiff compares two SQLite databases and returns the differences
// db1Path: path to first database (baseline/old version)
// db2Path: path to second database (new version with changes)
// Returns SQL statements that would transform db1 into db2
func RunSQLDiff(db1Path, db2Path string) (*DiffResult, error) {
cDb1 := C.CString(db1Path)
cDb2 := C.CString(db2Path)
defer C.free(unsafe.Pointer(cDb1))
defer C.free(unsafe.Pointer(cDb2))

var cResult *C.char
var cError *C.char

// Call the C wrapper function
rc := C.sqldiff_run(cDb1, cDb2, &cResult, &cError)

if rc != 0 {
if cError != nil {
errMsg := C.GoString(cError)
C.free(unsafe.Pointer(cError))
return nil, fmt.Errorf("sqldiff failed: %s", errMsg)
}
return nil, fmt.Errorf("sqldiff failed with code %d", rc)
}

result := &DiffResult{
HasChanges: false,
Operations: []DiffOperation{},
Conflicts: []PrimaryKeyConflict{},
}

if cResult != nil {
result.SQL = C.GoString(cResult)
result.HasChanges = len(result.SQL) > 0
C.free(unsafe.Pointer(cResult))

// Parse the SQL to extract operations
result.Operations = parseSQL(result.SQL)

// Detect primary key conflicts
result.Conflicts = detectConflicts(result.Operations)
}

return result, nil
}

// parseSQL parses SQL diff output into individual operations
func parseSQL(sql string) []DiffOperation {
// TODO: Implement SQL parsing
// For now, return empty slice
// This would parse INSERT, UPDATE, DELETE statements
// and extract table names and primary keys
return []DiffOperation{}
}

// detectConflicts finds operations that conflict on the same primary key
func detectConflicts(operations []DiffOperation) []PrimaryKeyConflict {
// TODO: Implement conflict detection
// This would find cases where:
// - Same PK has multiple UPDATE/DELETE operations
// - INSERT on existing PK
// etc.
return []PrimaryKeyConflict{}
}

// ApplyDiff applies a diff to a database
// This executes the SQL statements from a DiffResult
func ApplyDiff(dbPath string, diff *DiffResult) error {
if !diff.HasChanges {
return nil // Nothing to apply
}

if len(diff.Conflicts) > 0 {
return fmt.Errorf("cannot apply diff with %d conflicts", len(diff.Conflicts))
}

// TODO: Open database and execute SQL
// For now, we'll need to implement this using the bridge
return fmt.Errorf("ApplyDiff not yet implemented")
}
Loading
Loading