-
Notifications
You must be signed in to change notification settings - Fork 15
/
schema_executor.go
76 lines (62 loc) · 2.01 KB
/
schema_executor.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package dqlx
import (
"context"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
)
// SchemaExecutor executes schema operations
type SchemaExecutor struct {
client *dgo.Dgraph
dropAll bool
runInBackground bool
}
// SchemaExecutorOptionFn represents an function modifier
// for the SchemaExecutor options
type SchemaExecutorOptionFn func(*SchemaExecutor)
func WithDropAllSchema(dropAll bool) SchemaExecutorOptionFn {
return func(schema *SchemaExecutor) {
schema.dropAll = dropAll
}
}
// WithRunInBackground instructs Dgraph to run indexes in the background
func WithRunInBackground(runInBackground bool) SchemaExecutorOptionFn {
return func(schema *SchemaExecutor) {
schema.runInBackground = runInBackground
}
}
// NewSchemaExecutor creates a new schema executor
func NewSchemaExecutor(client *dgo.Dgraph) *SchemaExecutor {
return &SchemaExecutor{
client: client,
runInBackground: true,
}
}
// AlterSchema alters the schema with new predicates or types
// No drop operation would occur if not specifying (DropAll)
func (executor SchemaExecutor) AlterSchema(ctx context.Context, schema *SchemaBuilder, options ...SchemaExecutorOptionFn) error {
schemaDefinition, err := schema.ToDQL()
if err != nil {
return err
}
return executor.client.Alter(ctx, &api.Operation{
Schema: schemaDefinition,
DropAll: executor.dropAll,
RunInBackground: executor.runInBackground,
})
}
// DropType drops a type
func (executor SchemaExecutor) DropType(ctx context.Context, typeName string) error {
return executor.client.Alter(ctx, &api.Operation{
DropOp: api.Operation_TYPE,
DropValue: typeName,
RunInBackground: executor.runInBackground,
})
}
// DropPredicate drops a predicate
func (executor SchemaExecutor) DropPredicate(ctx context.Context, predicateName string) error {
return executor.client.Alter(ctx, &api.Operation{
DropOp: api.Operation_ATTR,
DropValue: predicateName,
RunInBackground: executor.runInBackground,
})
}