forked from aws-amplify/amplify-category-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): handle attribute type change on gsi
- Loading branch information
Showing
5 changed files
with
156 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/amplify-graphql-api-construct-tests/src/__tests__/amplify-table-5.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as path from 'path'; | ||
import { createNewProjectDir, deleteProjectDir, getDDBTable } from 'amplify-category-api-e2e-core'; | ||
import { cdkDestroy, initCDKProject, cdkDeploy, updateCDKAppWithTemplate } from '../commands'; | ||
|
||
jest.setTimeout(1000 * 60 * 60 /* 1 hour */); | ||
|
||
describe('CDK amplify table 5', () => { | ||
let projRoot: string; | ||
let projFolderName: string; | ||
|
||
beforeEach(async () => { | ||
projFolderName = 'cdkamplifytable5'; | ||
projRoot = await createNewProjectDir(projFolderName); | ||
}); | ||
|
||
afterEach(async () => { | ||
try { | ||
await cdkDestroy(projRoot, '--all'); | ||
} catch (_) { | ||
/* No-op */ | ||
} | ||
|
||
deleteProjectDir(projRoot); | ||
}); | ||
|
||
test('datatype change on indexed field should replace the index', async () => { | ||
const templatePath = path.resolve(path.join(__dirname, 'backends', 'amplify-table', 'simple-todo')); | ||
const name = await initCDKProject(projRoot, templatePath); | ||
const outputs = await cdkDeploy(projRoot, '--all'); | ||
const { awsAppsyncApiId: apiId, awsAppsyncRegion: region } = outputs[name]; | ||
const tableName = `Todo-${apiId}-NONE`; | ||
const table = await getDDBTable(tableName, region); | ||
expect(table.Table.AttributeDefinitions).toHaveLength(2); | ||
const nameAttr = table.Table.AttributeDefinitions.find((attr) => attr.AttributeName === 'name'); | ||
expect(nameAttr.AttributeType).toEqual('S'); | ||
expect(table.Table.GlobalSecondaryIndexes).toHaveLength(1); | ||
expect(table.Table.GlobalSecondaryIndexes[0].IndexName).toEqual('byName'); | ||
|
||
// deploy with modified attribute type | ||
const updateTemplatePath = path.resolve(path.join(__dirname, 'backends', 'amplify-table', 'simple-todo', 'attributeTypeChange')); | ||
updateCDKAppWithTemplate(projRoot, updateTemplatePath); | ||
await expect(cdkDeploy(projRoot, '--all')).resolves.not.toThrow(); | ||
const modifiedTable = await getDDBTable(tableName, region); | ||
expect(modifiedTable.Table.AttributeDefinitions).toHaveLength(3); | ||
const modifiedNameAttr = modifiedTable.Table.AttributeDefinitions.find((attr) => attr.AttributeName === 'name'); | ||
expect(modifiedNameAttr.AttributeType).toEqual('N'); | ||
expect(modifiedTable.Table.GlobalSecondaryIndexes).toHaveLength(2); | ||
expect(modifiedTable.Table.GlobalSecondaryIndexes).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
IndexName: 'byNameDescription', | ||
}), | ||
expect.objectContaining({ | ||
IndexName: 'byName', | ||
}), | ||
]), | ||
); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...nstruct-tests/src/__tests__/backends/amplify-table/simple-todo/attributeTypeChange/app.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env node | ||
import 'source-map-support/register'; | ||
import { App, Stack, Duration } from 'aws-cdk-lib'; | ||
// @ts-ignore | ||
import { AmplifyGraphqlApi, AmplifyGraphqlDefinition } from '@aws-amplify/graphql-api-construct'; | ||
|
||
const packageJson = require('../package.json'); | ||
|
||
const app = new App(); | ||
const stack = new Stack(app, packageJson.name.replace(/_/g, '-'), { | ||
env: { region: process.env.CLI_REGION || 'us-west-2' }, | ||
}); | ||
|
||
new AmplifyGraphqlApi(stack, 'GraphqlApi', { | ||
apiName: 'MyGraphQLApi', | ||
definition: AmplifyGraphqlDefinition.fromString( | ||
/* GraphQL */ ` | ||
type Todo @model @auth(rules: [{ allow: public }]) { | ||
id: ID! | ||
description: String! | ||
name: Int! @index(name: "byName") @index(name: "byNameDescription", sortKeyFields: ["description"]) | ||
} | ||
`, | ||
{ | ||
dbType: 'DYNAMODB', | ||
provisionStrategy: 'AMPLIFY_TABLE', | ||
}, | ||
), | ||
authorizationModes: { | ||
apiKeyConfig: { expires: Duration.days(7) }, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters