Quickstart for Go
Python · Node · Go · Rust · Swift · Dart · JVM · .NET · C++ · Objective-C
This program shows two devices synchronizing through one remote. It resolves a conflict so both devices finish with the same row. See Concepts for the database and synchronization terms.
| Term | Meaning in this program |
|---|---|
| Local store | The database files on one device. |
| Remote | The shared copy of a database that devices synchronize with. |
| Revision | One published database state that does not change. |
| Head | The remote’s newest published revision. |
| Push | Send local writes to the remote. |
| Pull | Receive writes from the remote. |
| Pending | Local writes that the device has not confirmed as published. |
| Replay | Run local statements again on the remote revision. |
| Quarantine | Preserve a transaction that cannot replay safely, for the application to handle. |
The two databases represent a laptop and a phone. Both change the same row before receiving the other’s change. The remote uses a temporary directory that the program removes when it exits.
- Complete the installation.
- Save this program as
main.goin a module that requires the SDK. - Run
go run ..
The program prints two lines:
| Output | Meaning |
|---|---|
Use query for this statement. | The program passes a read statement to Execute on purpose. The call fails with read_in_execute, and the program prints the error’s Action. |
quickstart: ok | The program completed all checks, including checking that both devices contain the same row. |
Without an accepted license key, a trial notice on standard error is expected. See license notices for the text. If the program fails or omits these lines, check the installation and any changes to the program.
// Two devices, one remote, one conflict, and its resolution.package main
import ( "errors" "fmt" "log" "os" "path/filepath" "slices" "time"
"github.com/potluckdb/potluckdb-go")
// Every sync attempt reports an outcome, UpToDate included; wait for the requested one.func waitFor[T potluckdb.SyncResult](events <-chan potluckdb.SyncResult) T { for { select { case outcome := <-events: if failed, ok := outcome.(potluckdb.Failed); ok { log.Fatal(failed.Err) } if wanted, ok := outcome.(T); ok { return wanted } case <-time.After(10 * time.Second): log.Fatal("no sync outcome within ten seconds") } }}
// One Pushed is one automatic cycle's work, not proof that everything written so far// is published; a device is finished when it has nothing pending.func waitUntilPublished(device *potluckdb.Database, events <-chan potluckdb.SyncResult) { for { status, err := device.Status() if err != nil { log.Fatal(err) } if status.PendingWrites == 0 { return } waitFor[potluckdb.Pushed](events) }}
func main() { scratch, err := os.MkdirTemp("", "potluckdb-") if err != nil { log.Fatal(err) } defer os.RemoveAll(scratch)
remote, err := potluckdb.RemoteFromDirectory(filepath.Join(scratch, "remote"), nil) // a bucket replaces this in production; see the installation page if err != nil { log.Fatal(err) }
// The laptop synchronizes on its own: automatic is the default policy. // OnSync delivers every outcome to the program; this program sends them down a channel. laptopEvents := make(chan potluckdb.SyncResult, 64) laptopOptions := potluckdb.DefaultOpenOptions() laptopOptions.OnSync = func(outcome potluckdb.SyncResult) { laptopEvents <- outcome } laptop, err := potluckdb.Open(filepath.Join(scratch, "laptop"), remote, laptopOptions) if err != nil { log.Fatal(err) } err = laptop.Transaction(func(setup *potluckdb.Transaction) error { // one transaction: both statements travel in one push if _, err := setup.Execute("CREATE TABLE note(id INTEGER PRIMARY KEY, body TEXT NOT NULL)"); err != nil { return err } _, err := setup.Execute("INSERT INTO note(body) VALUES(?)", "buy milk") return err }) if err != nil { log.Fatal(err) } waitUntilPublished(laptop, laptopEvents)
// The phone synchronizes only when instructed: a manual policy, and Sync() // calls that return the outcome as a value. It starts from the revision the // laptop published. phoneOptions := potluckdb.DefaultOpenOptions() phoneOptions.SyncPolicy = potluckdb.ManualSync() phone, err := potluckdb.Open(filepath.Join(scratch, "phone"), remote, phoneOptions) if err != nil { log.Fatal(err) } result, err := phone.Query("SELECT id, body FROM note ORDER BY id") if err != nil { log.Fatal(err) } if !slices.Equal(result.Columns, []string{"id", "body"}) { log.Fatalf("unexpected columns: %v", result.Columns) } if result.Rows[0].Get("body") != "buy milk" || result.Rows[0].At(0) != int64(1) { log.Fatalf("unexpected row: %v", result.Rows[0]) }
// Both devices modify the same row before either has seen the other's write. if _, err := laptop.Execute("UPDATE note SET body = ? WHERE id = ?", "buy oat milk", 1); err != nil { log.Fatal(err) } if _, err := phone.Execute("UPDATE note SET body = ? WHERE id = ?", "buy milk and eggs", 1); err != nil { log.Fatal(err) } waitUntilPublished(laptop, laptopEvents)
// The phone's push finds a remote head it has not seen: // a Conflict, returned rather than an error. outcome, err := phone.Sync() if err != nil { log.Fatal(err) } if _, conflict := outcome.(potluckdb.Conflict); !conflict { log.Fatalf("expected a conflict, got %T", outcome) } resolution, err := phone.ResolveConflict() // adopt the laptop's revision, replay the phone's statement on it if err != nil { log.Fatal(err) } if len(resolution.Quarantined) != 0 { log.Fatalf("statements were quarantined: %v", resolution.Quarantined) } if outcome, err = phone.Sync(); err != nil { log.Fatal(err) } else if _, pushed := outcome.(potluckdb.Pushed); !pushed { log.Fatalf("expected a push, got %T", outcome) }
// The laptop pulls the resolved revision on its own. Both devices hold the same row. waitFor[potluckdb.Pulled](laptopEvents) for _, db := range []*potluckdb.Database{laptop, phone} { current, err := db.Query("SELECT body FROM note WHERE id = ?", 1) if err != nil { log.Fatal(err) } if current.Rows[0].Get("body") != "buy milk and eggs" { log.Fatalf("the devices disagree: %v", current.Rows[0]) } }
// Query reads, Execute writes. The wrong call is refused before execution. var refused *potluckdb.Error if _, err := laptop.Execute("SELECT count(*) FROM note"); !errors.As(err, &refused) || refused.Code != "read_in_execute" { log.Fatalf("expected a refusal, got %v", err) } fmt.Println(refused.Action) // Use query for this statement.
for _, closer := range []func() error{laptop.Close, phone.Close, remote.Close} { if err := closer(); err != nil { log.Fatal(err) } } fmt.Println("quickstart: ok")}What the program demonstrates
Section titled “What the program demonstrates”The remote uses a directory. No server runs. RemoteFromDirectory uses a directory that stands in for a bucket in a storage service. The installation page shows the constructor for a storage service. These constructors support S3, Google Cloud Storage, and Azure Blob Storage. The rest of the program stays the same.
The laptop synchronizes automatically. Automatic synchronization is the default policy, which decides when to synchronize. The laptop never calls Sync(). Its OnSync hook, a function that receives synchronization results, reports every attempt. A failed result carries the error.
The hook runs on the synchronization thread. Transfer the outcome to your own thread, as shown with a channel.
| Result | Meaning |
|---|---|
UpToDate | The attempt found no pending writes and no remote revision to pull. |
Pushed | The attempt published its pending writes as a remote revision. |
Pulled | The attempt adopted a remote revision. It can also report replayed and quarantined transactions. |
Conflict | The device’s push met a remote revision that the device had not seen. |
Failed | An automatic attempt failed and carries the error. |
An automatic result covers one attempt. Pushed confirms the writes that the attempt published. Writes made after the attempt began can still be pending. If an earlier attempt failed, an explicit Sync() first publishes that attempt’s writes and leaves later writes pending. A program that must know its own writes are published reads Status().PendingWrites. waitUntilPublished does that here before the second device opens.
Rows include their column names. result.Columns lists the names. You can read a row by column name or position.
The phone receives a conflict result. Sync() returns Conflict when the remote has an unseen revision. This result is not an error. ResolveConflict() adopts the laptop’s revision and replays the phone’s statement. The phone’s text then appears on both devices. The resolution lists any transactions that cannot replay safely as quarantined.
Query reads and Execute writes. Each accepts exactly one statement of its own kind. Each refuses the other kind before execution, with *potluckdb.Error whose Action names the required call.
These calls promise different things. Query returns rows and uses the connection’s MaximumRows to bound the result. A different SQL engine can answer a read. Execute changes data that a revision publishes and other devices receive.
This execution-intent policy separates reads from writes. SQLite preparation can have effects, including under EXPLAIN.
Neither call accepts SQL transaction control, which starts, commits, or rolls back a transaction.
Use Transaction for transaction control, as the setup shows.
Reference
Section titled “Reference”Open and construct and Database describe every call. Options and policies describes the automatic policy and its parameters. The examples provide longer programs that the test suite runs.