-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_pg_token.go
53 lines (45 loc) · 1.33 KB
/
sync_pg_token.go
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
package gontentful
import (
"database/sql"
"fmt"
)
const (
createSyncTable = "CREATE TABLE IF NOT EXISTS %s_sync ( id int primary key, token text, created_at timestamp without time zone DEFAULT now() );"
insertSyncToken = "INSERT INTO %s_sync (id, token) VALUES (0, '%s') ON CONFLICT (id) DO UPDATE SET token = EXCLUDED.token, created_at=now();"
selectSyncToken = "SELECT token FROM %s_sync WHERE id = 0;"
)
func GetSyncToken(databaseURL string, schemaName string) (string, error) {
var syncToken string
db, _ := sql.Open("postgres", databaseURL)
schemaPrefix := ""
if schemaName != "" {
schemaPrefix = fmt.Sprintf("%s.", schemaName)
}
row := db.QueryRow(fmt.Sprintf(selectSyncToken, schemaPrefix))
err := row.Scan(&syncToken)
if err != nil {
return "", err
}
return syncToken, nil
}
func SaveSyncToken(databaseURL string, schemaName string, token string) error {
var err error
db, _ := sql.Open("postgres", databaseURL)
schemaPrefix := ""
if len(schemaName) > 0 {
_, err = db.Exec(fmt.Sprintf("SET search_path='%s'", schemaName))
if err != nil {
return err
}
schemaPrefix = fmt.Sprintf("%s.", schemaName)
}
_, err = db.Exec(fmt.Sprintf(createSyncTable, schemaPrefix))
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf(insertSyncToken, schemaPrefix, token))
if err != nil {
return err
}
return nil
}