-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
50 lines (42 loc) · 1.45 KB
/
schema.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
package cus
import (
"context"
"fmt"
"github.com/gocql/gocql"
)
const createDataTableCQL = `CREATE TABLE IF NOT EXISTS cus_data(
obj_id TEXT PRIMARY KEY,
checksum TEXT,
data BLOB)`
const dropDataTableCQL = `DROP TABLE IF EXISTS cus_data;`
const createChecksumTableCQL = `CREATE TABLE IF NOT EXISTS cus_checksum(
checksum TEXT PRIMARY KEY,
obj_id TEXT)`
const dropChecksumTableCQL = `DROP TABLE IF EXISTS cus_checksum;`
func (s *CassandraUniqueStore) CreateSchema(force bool) error {
ctx := context.TODO()
if force {
if err := s.execCreateSQL(ctx, dropDataTableCQL); err != nil {
return fmt.Errorf("failed to drop table : %v", err)
}
if err := s.execCreateSQL(ctx, dropChecksumTableCQL); err != nil {
return fmt.Errorf("failed to drop table : %v", err)
}
}
if err := s.execCreateSQL(ctx, createDataTableCQL); err != nil {
return fmt.Errorf("failed to create table : %v", err)
}
if err := s.execCreateSQL(ctx, createChecksumTableCQL); err != nil {
return fmt.Errorf("failed to create table : %v", err)
}
return nil
}
func (s *CassandraUniqueStore) execCreateSQL(ctx context.Context, cqlStatement string) error {
if err := s.session.AwaitSchemaAgreement(ctx); err != nil {
return fmt.Errorf("failed to wait for schema agreement : %v", err)
}
if err := s.session.Query(cqlStatement).RetryPolicy(&gocql.SimpleRetryPolicy{}).Exec(); err != nil {
return fmt.Errorf("error executing statement %s err=%v\n", cqlStatement, err)
}
return nil
}