如何在 DEV 和 PROD 之间设置 Hasura 迁移而不丢失数据

发布于 2025-01-15 17:15:30 字数 439 浏览 1 评论 0原文

我正在使用 hasura 迁移指南来同步两个服务器 - DEV 和 PROD。 在我们手动传输更改之前(如“使用 UI 复制所有更改”),现在数据库有 90% 相似。

我们决定设置适当的迁移,但根据我的测试,进行初始同步需要“干净的状态”。

问题示例:

我们在 DEV 和 PROD 上都有用户表。 DEV 上有一个附加字段age。 我们执行

1 hasura migrate create --init (on dev)

2 hasura migrate apply --endpoint PRODUCTION

我们收到错误relation \"users\"已经存在< /代码>。

问题是 - 我们如何在不先清理 PROD 的情况下同步数据库?

I'm using hasura migration guide to sync two servers - DEV and PROD.
Before we manually transferred the changes (as in 'using UI to copy all the changes'), so now databases are 90% similar.

We decided to set up proper migrations, but based on my tests doing an initial sync requires a 'clean slate'.

Example of the problem:

We have users table on both DEV and PROD. On DEV there is additional field age.
We do

1 hasura migrate create --init (on dev)

2 hasura migrate apply --endpoint PRODUCTION

We get error relation \"users\" already exists.

The question is - how can we sync the DBs without cleaning PROD first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2025-01-22 17:15:30

您当前收到该问题,因为运行 migrate apply 尝试在已存在的表上执行。

如果您使用 --skip-execution 标志,您可以将所有相关迁移标记为在 PRODUCTION 环境中已完成,并将 migrate apply 标记为通常应用新的迁移。

CLI 文档中提供了更多信息:
https://hasura.io/docs/latest/graphql/ core/hasura-cli/hasura_migrate_apply.html


重新阅读问题以澄清之后 - 使用 create 创建初始迁移--init 将创建数据库现在的快照(STAGINGProduction 之间不会有差异)。

要在 STAGINGProduction 之间迁移此内容,您需要手动更改创建的初始迁移以匹配 staging 和 prod,然后手动创建增量迁移以引入 生产分期一致。

此后,如果您通过 CLI 使用 Hasura 控制台(使用 https://hasura.io/docs/latest/graphql/core/hasura-cli/hasura_console.html)——它将自动为您创建未来的增量迁移目录。


顺便说一句 - 您还可以使用 IF NOT EXISTS 手动创建弹性迁移(这些不是由 Hasura 自动创建的,但您可以编辑它们的 SQL 文件迁移)。

例如:

ALTER TABLE users
ADD COLUMN IF NOT EXISTS age INT

编辑 2:我遇到的另一个可能有用的工具是 Migra(用于 Postgres,在 Hasura 之外)。它可以帮助比较您的开发和生产数据库,以帮助创建初始迁移状态:https://github。 com/djrobstep/migra

You're currently receiving that issue since running migrate apply is trying to execute on tables which already exist.

If you use the --skip-execution flag you can mark all of your relevant migrations as completed in the PRODUCTION environment and the migrate apply as usual to apply the new migration.

More information is available in the CLI documentation:
https://hasura.io/docs/latest/graphql/core/hasura-cli/hasura_migrate_apply.html


After re-reading the question to clarify - creating the initial migration using create --init will create a snapshot of your database as it is now (won't diff between STAGING and PRODUCTION).

To migrate this between STAGING and PRODUCTION you'd need to manually change the initial migration created to match staging and prod, and then manually create an incremental migration to bring PRODUCTION in line with STAGING.

After this, if you're working with Hasura Console through the CLI (using https://hasura.io/docs/latest/graphql/core/hasura-cli/hasura_console.html) -- it will automatically create future incremental migrations for you in the directory.


As an aside - you can also create resilient migrations manually as well using IF NOT EXISTS (these aren't automatically created by Hasura, but you can edit their SQL file migrations).

For example:

ALTER TABLE users
ADD COLUMN IF NOT EXISTS age INT

Edit 2: One other tool which I came upon which may be helpful is Migra (for Postgres, outside of Hasura). It can help with diff-ing your dev and production databases to help create the initial migration state: https://github.com/djrobstep/migra

御守 2025-01-22 17:15:30

它有点隐藏,但有关迁移的部分涵盖了这种情况(您还没有跟踪/创建迁移,现在需要第一次初始化它们):

https://hasura.io/docs/latest/graphql/core/migrations/migrations-setup.html#step-3-initialize-the-migrations-and-metadata-as-per-your-current-state

希望这有帮助 =)

It's a bit buried, but the section on migrations covers this scenario (you haven't been tracking/creating migrations and now need to initialize them for the first time):

https://hasura.io/docs/latest/graphql/core/migrations/migrations-setup.html#step-3-initialize-the-migrations-and-metadata-as-per-your-current-state

Hope this helps =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文