amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

Amplify API: Can't modify @primaryKey field (Resource is not in the state stackUpdateComplete)

Open 3xc3pt1oN opened this issue 2 years ago • 4 comments

Amplify CLI Version

10.7.3

Question

It's not possible to push changes if the schema has been modified several times, e.g. by adding/removing an index or extending the PrimaryKey. We consider this could be a serious risk when building our app on the Amplify platform. What are the proposed ways from your end to work with this data migration case?

Steps to Reproduce

Step 1: Create a Model

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String!
    issues: [String!]
}

Step 2: Add data via AppSync

createToDo(input: {id: "1", issues: ["car", "tv", "meal"], name: "important"}) {
    issues
    id
    name
    createdAt
}

Step 3: Add index to Model

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String! @index(name: "byName", sortKeyFields: ["id"], queryField: "toDoByName")
    issues: [String!]
}

Step 4: Add data via AppSync

createToDo(input: {id: "2", issues: ["meal", "work"], name: "very important"}) {
    issues
    id
    name
    createdAt
}

Step 5: Remove Index and add a PrimaryKey to Model

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID! @primaryKey(sortKeyFields: ["name"])
    name: String!
    issues: [String!]
}

Required command for pushing changes: amplify push --allow-destructive-graphql-schema-updates

Result

Deployment completed.
Deploying api create360graphqlapi [ ===------------------------------------- ] 1/13
        GraphQLAPITransformerSchema3C… AWS::AppSync::GraphQLSchema    UPDATE_COMPLETE                Thu Apr 27 2023 14:09:30…

Rolled back (2 of 1)
šŸ›‘ ["Index: 0 State: {\"deploy\":\"waitingForDeployment\"} Message: Resource is not in the state stackUpdateComplete"]

Operating system

Linux/Debian

3xc3pt1oN avatar Apr 28 '23 07:04 3xc3pt1oN

Hey @3xc3pt1oN, Thanks for bringing this up! I've designated it as a duplicate of the issue located at https://github.com/aws-amplify/amplify-category-api/issues/940 to ensure we're tracking the original item.

It appears that you are currently using version 10.7.3, so I recommend upgrading the CLI to the latest version. In the meantime, would you mind trying the following steps as a workaround?

First, remove the index by executing amplify push after modifying your code to:


type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String! 
    issues: [String!]
}

Next, add the PrimaryKey to the Model by executing amplify push after modifying your code to:


type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID! @primaryKey(sortKeyFields: ["name"])
    name: String!
    issues: [String!]
}

Once you have finished carrying out these instructions, you should be able to successfully execute a push. If you encounter any further difficulties, please do not hesitate to inform us.

AnilMaktala avatar Apr 28 '23 13:04 AnilMaktala

Hey @AnilMaktala

thanks for the quick response. The steps you described did not solve the issue. We also looked at the linked issue. This does not update the schema in terms of indices and keys, but renames an existing model. Removing the file deployment-state.json did not fix the deployment deadlock either. We notice both problems would require a data migration task. Does Amplify support any kind of data migration or define best practices? If the migration job is not supported is there still a way to fix the deployment without having to delete the updated environment and recreate it?

Steps to Reproduce

Step 1: Create a Model

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String!
    issues: [String!]
}

Step 2: Add data via AppSync

createToDo(input: {id: "1", issues: ["car", "tv", "meal"], name: "important"}) {
    issues
    id
    name
    createdAt
}

Step 3: Add index to Model

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String! @index(name: "byName", sortKeyFields: ["id"], queryField: "toDoByName")
    issues: [String!]
}

Step 4: Add data via AppSync

createToDo(input: {id: "2", issues: ["meal", "work"], name: "very important"}) {
    issues
    id
    name
    createdAt
}

Step 5: Remove Index

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String! 
    issues: [String!]
}

Step 6: Add data via AppSync

createToDo(input: {id: "5", issues: ["hobby"], name: "lazy"}) {
    issues
    id
    name
    createdAt
}

Step 7: Add PrimaryKey to Model

type ToDo @model @auth(rules: [{ allow: public }]) {
    id: ID! @primaryKey(sortKeyFields: ["name"])
    name: String!
    issues: [String!]
}

Result

Deployment completed.
Deploying api create360graphqlapi [ ===------------------------------------- ] 1/13
        GraphQLAPITransformerSchema3C… AWS::AppSync::GraphQLSchema    UPDATE_COMPLETE                Tue May 02 2023 07:51:43…

Rolled back (2 of 1)
šŸ›‘ ["Index: 0 State: {\"deploy\":\"waitingForDeployment\"} Message: Resource is not in the state stackUpdateComplete"]

3xc3pt1oN avatar May 02 '23 07:05 3xc3pt1oN

Hi @AnilMaktala thanks for the initial reply.

We checked the referenced issue but this does not solve the second half of the reported issue. Maybe to reiterate on the original issue, since it basically contained two questions:

  1. Are there any guides, best-practices in the "Amplify-land" on how to deal with data migrations (e.g. renaming tables, fields, indexes, sort-key, etc.)?
  2. Our unguided schema changes - which certainly would require a data migration - put us in a deadlock situation. How can we resolve that deadlock without re-creating the environment?

Regarding 1. we found some answers here.

But for 2. we're still blank. None of the referenced suggestions worked for us. Basically, we're unable to undo an "invalid" schema change once we tried to deploy it. To rephrase the situation: Even when pushing the same initial schema after that bad attempt again, we're stuck with the same error message Resource is not in the state stackUpdateComplete.

There should be a way out of this deadlock situation since the "bad" deployment was rolled-back and the schema was "fixed" locally, no?

cbastuck avatar May 30 '23 09:05 cbastuck