Go client Alter()

Hi all,

I have a question regarding go client’s alter method.

    dc := api.NewDgraphClient(conn)
	dg := dgo.NewDgraphClient(dc)

	op := &api.Operation{}
	op.Schema = `
	sid: string @index(hash) . 
	key: string @index(hash) . 
	value: string . 
	type SessionEntry {
		sid
		key
		value
	}
	`

	ctx := context.Background()
	err := dg.Alter(ctx, op)
	if err != nil {
		...
	}

if I run the above code and the schema doesn’t exist it’ll get created.
But if the schema already exists will it stop or anyway apply the schema (consume resources for an unnecessary operation)?

Should I include a check operation prior to alter? schema(type: SessionEntry) {}
Would such check be redundant?

thanks in advance
Byron

Dgraph will check if there is any change in the schema and decide what to do.

You should use Alter only when necessary. It doesn’t make sense to use it all the time. Unless your scheme is so malleable that you need changes per minute. What is technically unfeasible.

I am writing a Dgraph store implementation for a sessions library so there is a point to make it as automagic for users. This is why I opted to include an Alter() on each database connection when users start a new session.

though your answer perplexed me a bit.
If I understand correct altering an existing schema will not produce overhead as Dgraph will understand it is unnecessary to do so BUT I should anyway refrain from doing so thus include a simple check to see if the schema already exists.

for example:

    dc := api.NewDgraphClient(conn)
	dg := dgo.NewDgraphClient(dc)

	// check if schema already exists
	ctx := context.Background()
	query := `schema(type: SessionEntry) {}`
	response, _ := dg.NewTxn().Query(ctx, query)

	var r struct {
		Schema []struct {
			Name   string `json:"name"`
			Fields []struct {
				Name string `json:"name"`
			} `json:"fields"`
		} `json:"types"`
	}

	json.Unmarshal(response.Json, &r)

	// if not then set schema
	if len(r.Schema) == 0 {
		op := &api.Operation{}
		op.Schema = `
	sid: string @index(hash) . 
	key: string @index(hash) . 
	value: string . 
	type SessionEntry {
		sid
		key
		value
	}
	`
		ctx := context.Background()
		err := dg.Alter(ctx, op)
	}
  
...

something like this would be the best approach?

Well, feels okay. It is an unusual way of using Dgraph. It is up to you do like this or not. But Dgraph will ignore the schema if it isn’t new. If you wanna check in your end, best.

1 Like

my decision is to follow the norm as much as possible and since the rest of store implementations for the specific library I am using are abstracting such decisions away from the users I try to do the same. Although the difficulty and somehow abnormal use of dgraph as you pointed out, arises from the fact that the rest of the stores are pretty much key/value or file-based but in any case schema-less.

I am anyway going to document what is going on and communicate it to library’s devs on my PR but I too think that with the check included everything should be fine!

thanks a lot!