# Go client Alter()

**URL:** <https://discuss.dgraph.io/t/go-client-alter/12045>\
**Category:** Dgraph\
**Created:** [December 23, 2020, 1:18pm UTC](https://discuss.dgraph.io/t/go-client-alter/12045 "2020-12-23T13:18:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bh90210](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/bh90210/32/2874_2.png) [@bh90210](https://discuss.dgraph.io/u/bh90210)\
**Post date:** [December 23, 2020, 1:18pm UTC](https://discuss.dgraph.io/t/go-client-alter/12045/1 "2020-12-23T13:18:03Z")

</div>

Hi all,

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

```auto
    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

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [December 23, 2020, 1:42pm UTC](https://discuss.dgraph.io/t/go-client-alter/12045/2 "2020-12-23T13:42:24Z")

</div>

> [@bh90210](#):
>
> But if the schema already exists will it stop or anyway apply the schema (consume resources for an unnecessary operation)?

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

> [@bh90210](#):
>
> Should I include a check operation prior to alter? `schema(type: SessionEntry) {}`  
> Would such check be redundant?

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.

---

<div class="post-metadata">

**Author:** ![bh90210](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/bh90210/32/2874_2.png) [@bh90210](https://discuss.dgraph.io/u/bh90210)\
**Post date:** [December 23, 2020, 1:54pm UTC](https://discuss.dgraph.io/t/go-client-alter/12045/3 "2020-12-23T13:54:40Z")

</div>

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:

```auto
    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?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [December 23, 2020, 1:58pm UTC](https://discuss.dgraph.io/t/go-client-alter/12045/4 "2020-12-23T13:58:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bh90210](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/bh90210/32/2874_2.png) [@bh90210](https://discuss.dgraph.io/u/bh90210)\
**Post date:** [December 23, 2020, 2:04pm UTC](https://discuss.dgraph.io/t/go-client-alter/12045/5 "2020-12-23T14:04:36Z")

</div>

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!
