如何在回滚之后,使用没有新列的旧模式与具有新添加列的种类/表?

huangapple go评论95阅读模式
英文:

How to use old schema(with no new columns) with a kind/table having new columns added, after a rollback

问题

我在Google Cloud Datastore的一种数据中添加了一些新的列,并且Golang的新模式正在读取这些列。现在,如果由于某些问题需要回滚到旧的模式,其中这些新列不存在,那么最理想的策略是什么?我有两个选项:

1)编写一个脚本,在需要回滚时从GDS中删除新列。
2)添加一个类似标志的功能来禁用新列,以便该数据类型可以与旧的模式一起使用。我不确定是否存在这个解决方案。

哪个是最理想的选择,如何实现它们?

英文:

I have added some new columns in a kind in Google Cloud Datastore and new schema in Golang is reading that. Now if I want to rollback binaries to older schema because of some issue where these new columns do not exist, what is the ideal strategy for that? I have two options:

  1. Write a script to delete new column from GDS when I want to rollback
  2. Add a flag kind of feature to disable new columns so that the kind works with the older schema. I am not sure about this solution if is present.

Which is the ideal one and how to achieve them?

答案1

得分: 0

我能够找到一个名为omitempty的变量,可以在这些情况下帮助。我在这个博客上找到了一个很好的解释:https://www.sohamkamani.com/golang/omitempty/。

如果我在数据库中存储一个带有它的默认零值的列,那么在读取JSON时,omitempty将不会读取它。通过这种方式,我将能够在多个数据库模式中使用相同的Golang模式。

type User struct {
	ID string `json:"Id" datastore:"Id"`
}
英文:

I was able to figure a variable omitempty which can help in these situations. I found a good blog on it's explanation - https://www.sohamkamani.com/golang/omitempty/ .

If I store in DB a column with it's default zero value, then omitempty will not read it while reading json. In this way I will be able to use same golang schema with multiple DB schemas.

type User struct {
	ID           string        `json:"Id" datastore:"Id"`
}

huangapple
  • 本文由 发表于 2021年12月28日 17:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70505398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定