forked from tursodatabase/go-libsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (123 loc) · 2.9 KB
/
Copy pathmain.go
File metadata and controls
134 lines (123 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"database/sql"
"fmt"
"os"
"strings"
"github.com/tursodatabase/go-libsql"
)
func run() (err error) {
primaryUrl := os.Getenv("TURSO_URL")
if primaryUrl == "" {
return fmt.Errorf("TURSO_URL environment variable not set")
}
authToken := os.Getenv("TURSO_AUTH_TOKEN")
remoteEncryptionKey := os.Getenv("TURSO_REMOTE_ENCRYPTION_KEY")
dir, err := os.MkdirTemp("", "libsql-*")
if err != nil {
return err
}
defer os.RemoveAll(dir)
opts := []libsql.Option{libsql.WithAuthToken(authToken)}
if remoteEncryptionKey != "" {
opts = append(opts, libsql.WithRemoteEncryption(remoteEncryptionKey))
}
connector, err := libsql.NewEmbeddedReplicaConnector(dir+"/test.db", primaryUrl, opts...)
if err != nil {
return err
}
defer func() {
if closeError := connector.Close(); closeError != nil {
fmt.Println("Error closing connector", closeError)
if err == nil {
err = closeError
}
}
}()
db := sql.OpenDB(connector)
defer func() {
if closeError := db.Close(); closeError != nil {
fmt.Println("Error closing database", closeError)
if err == nil {
err = closeError
}
}
}()
msg := "1. Sync with primary"
if remoteEncryptionKey != "" {
msg += " (encrypted db)"
}
for {
fmt.Println("What would you like to do?")
fmt.Println(msg)
fmt.Println("2. Select from test table")
fmt.Println("3. Insert row to test table")
fmt.Println("4. Exit")
var choice int
_, err := fmt.Scanln(&choice)
if err != nil {
return err
}
switch choice {
case 1:
replicated, err := connector.Sync()
if err != nil {
return err
}
fmt.Println("%d frames synced", replicated.FramesSynced)
case 2:
err = func() (err error) {
rows, err := db.Query("SELECT * FROM test")
if err != nil {
if strings.Contains(err.Error(), "`no such table: test`") {
fmt.Println("Table test not found. Please run `CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)` on primary first and then sync.")
return nil
}
return err
}
defer func() {
if closeError := rows.Close(); closeError != nil {
fmt.Println("Error closing rows", closeError)
if err == nil {
err = closeError
}
}
}()
count := 0
for rows.Next() {
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
return err
}
fmt.Println(id, name)
count++
}
if rows.Err() != nil {
return rows.Err()
}
if count == 0 {
fmt.Println("Empty table. Please run `INSERT INTO test (id, name) VALUES (random(), lower(hex(randomblob(16))))` on primary and then sync.")
}
return nil
}()
if err != nil {
return err
}
case 3:
_, err := db.Exec("INSERT INTO test (id, name) VALUES (random(), lower(hex(randomblob(16))))")
if err != nil {
return err
}
case 4:
return nil
}
}
return nil
}
func main() {
if err := run(); err != nil {
panic(err)
}
}