forked from vitessio/vitess
/
vschema.go
45 lines (40 loc) · 1.16 KB
/
vschema.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
package etcdtopo
import (
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtgate/planbuilder"
"golang.org/x/net/context"
// vindexes needs to be imported so that they register
// themselves against vtgate/planbuilder. This will allow
// us to sanity check the schema being uploaded.
_ "github.com/youtube/vitess/go/vt/vtgate/vindexes"
)
/*
This file contains the vschema management code for etcdtopo.Server
*/
// SaveVSchema saves the JSON vschema into the topo.
func (s *Server) SaveVSchema(ctx context.Context, vschema string) error {
_, err := planbuilder.NewSchema([]byte(vschema))
if err != nil {
return err
}
_, err = s.getGlobal().Set(vschemaPath, vschema, 0 /* ttl */)
if err != nil {
return convertError(err)
}
return nil
}
// GetVSchema fetches the JSON vschema from the topo.
func (s *Server) GetVSchema(ctx context.Context) (string, error) {
resp, err := s.getGlobal().Get(vschemaPath, false /* sort */, false /* recursive */)
if err != nil {
err = convertError(err)
if err == topo.ErrNoNode {
return "{}", nil
}
return "", err
}
if resp.Node == nil {
return "", ErrBadResponse
}
return resp.Node.Value, nil
}