Skip to content

Commit

Permalink
fix(graphql-transformer-core): fix gsi mutate san check (#6095)
Browse files Browse the repository at this point in the history
* fix(graphql-transformer-core): fix gsi mutate san check

fix gsi cant mutate multiple gsi to only look for gsi changes and not ks or attr changes

re #6013

* test(amplify-e2e-tests): add gsi san check e2e

add san check e2e to test adding an existing hash key also being used as a range key
  • Loading branch information
SwaySway committed Dec 11, 2020
1 parent 85b1ae3 commit 37d08d9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Task
@model
@key(name: "gsi0", fields: ["name"])
@key(name: "gsi1", fields: ["taskID", "createdAt"])
@key(name: "gsi3", fields: ["name", "taskID"])
@key(name: "gsi2", fields: ["version"]) {
id: ID!
taskID: ID!
name: String!
version: String!
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Task
@model
@key(name: "gsi0", fields: ["name"])
@key(name: "gsi1", fields: ["taskID", "createdAt"])
@key(name: "gsi2", fields: ["version"]) {
id: ID!
taskID: ID!
name: String!
version: String!
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ describe('amplify add api', () => {
deleteProjectDir(projRoot);
});

it('init project, allow adding key when keyschema of one key is used in another key', async () => {
const projectName = 'validksgsiupdate';
const initial_schema = 'migrations_key/three_gsi_model_schema.graphql';
const nextSchema = 'migrations_key/four_gsi_model_schema.graphql';
await initJSProjectWithProfile(projRoot, { name: projectName });
await addApiWithSchema(projRoot, initial_schema);
await amplifyPush(projRoot);

updateApiSchema(projRoot, projectName, nextSchema);
await amplifyPushUpdate(projRoot, /GraphQL endpoint:.*/);
});

it('init project, run invalid migration trying to add more than one gsi, and check for error', async () => {
const projectName = 'migratingkey';
const initialSchema = 'migrations_key/initial_schema.graphql';
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-transformer-core/src/util/sanity-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ export const cantMutateMultipleGSIAtUpdateTimeRule = (diffs: Diff[], currentBuil

for (const diff of diffs) {
if (
// implies a field was changed in a GSI after it was created.
// Path like:["stacks","Todo.json","Resources","TodoTable","Properties","GlobalSecondaryIndexes", ... ]
// implies a field was changed in a GSI after it was created if it ends in GSI
// Path like: ["stacks","Todo.json","Resources","TodoTable","Properties","GlobalSecondaryIndexes" ]
diff.kind === 'A' &&
diff.path.length >= 6 &&
diff.path[5] === 'GlobalSecondaryIndexes'
diff.path.slice(-1)[0] === 'GlobalSecondaryIndexes'
) {
const diffTableName = diff.path[3];

Expand Down

0 comments on commit 37d08d9

Please sign in to comment.