简体   繁体   English

在 AWS Amplify 中更改我的 GraphQL 架构时如何防止丢失生产数据?

[英]How to prevent losing production data when changing my GraphQL schema in AWS Amplify?

In my AWS Amplify project I am using a GraphQL API with several @model directives.在我的 AWS Amplify 项目中,我使用带有多个@model指令的 GraphQL API。 Therefore, Amplify generated multiple DynamoDB tables in my AWS backend.因此,Amplify 在我的 AWS 后端生成了多个 DynamoDB 表。 Now, when removing such a @model or renaming it, the old DynamoDB table will be permanently deleted with all the contained data !现在,当删除这样的@model或重命名它时,旧的 DynamoDB 表将被永久删除,其中包含所有数据

How can this be prevented to avoid production data by mistake?如何防止这种情况发生,以避免错误地避免生产数据?

To prevent your DynamoDB tables from being deleted you can set the DeletionPolicy to Retain .为了防止您的 DynamoDB 表被删除,您可以将DeletionPolicy设置为Retain Unfortunately, Amplify does not do this by default.不幸的是,默认情况下 Amplify 不会这样做。

Therefore, you can use the custom GraphQL directive @retain like this:因此,您可以像这样使用自定义 GraphQL 指令@retain

  1. Install the transformer: npm install --save graphql-retain-transformer .安装转换器: npm install --save graphql-retain-transformer
  2. Edit amplify/backend/api/<YOUR_API>/transform.conf.json and append "graphql-retain-transformer" to the transformers field:编辑amplify/backend/api/<YOUR_API>/transform.conf.json并将"graphql-retain-transformer"附加到转换器字段:
"transformers": [
    "graphql-retain-transformer"
]
  1. In your schema.graphql file, append the @retain directive to all the @model types that you want to activate the Retain Deletion Policy for:在您的schema.graphql文件中,将@retain指令附加到您想要激活保留删除策略的所有@model类型:
type Todo @model @retain {
  id: ID!
  title: String!
  description: String
}

GitHub repository of the custom directive: https://github.com/flogy/graphql-retain-transformer (please leave a ⭐️ if you like it 🙂)自定义指令的 GitHub 仓库: https : //github.com/flogy/graphql-retain-transformer (喜欢请留下一个⭐️🙂)

A more detailed blog post about it:https://react-freelancer.ch/blog/amplify-retain-dynamodb-tables关于它的更详细的博客文章:https ://react-freelancer.ch/blog/amplify-retain-dynamodb-tables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM