Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Flag to allow schema changes that require table replacement #8144

Merged
merged 17 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1299,14 +1299,22 @@ jobs:
environment:
TEST_SUITE: src/__tests__/auth_6.test.ts
CLI_REGION: eu-west-2
api_5-amplify_e2e_tests:
working_directory: ~/repo
docker: *ref_1
resource_class: large
steps: *ref_5
environment:
TEST_SUITE: src/__tests__/api_5.test.ts
CLI_REGION: eu-central-1
api_4-amplify_e2e_tests:
working_directory: ~/repo
docker: *ref_1
resource_class: large
steps: *ref_5
environment:
TEST_SUITE: src/__tests__/api_4.test.ts
CLI_REGION: eu-central-1
CLI_REGION: ap-northeast-1
schema-iterative-update-4-amplify_e2e_tests_pkg_linux:
working_directory: ~/repo
docker: *ref_1
Expand Down Expand Up @@ -2107,6 +2115,16 @@ jobs:
TEST_SUITE: src/__tests__/auth_6.test.ts
CLI_REGION: eu-west-2
steps: *ref_6
api_5-amplify_e2e_tests_pkg_linux:
working_directory: ~/repo
docker: *ref_1
resource_class: large
environment:
AMPLIFY_DIR: /home/circleci/repo/out
AMPLIFY_PATH: /home/circleci/repo/out/amplify-pkg-linux
TEST_SUITE: src/__tests__/api_5.test.ts
CLI_REGION: eu-central-1
steps: *ref_6
api_4-amplify_e2e_tests_pkg_linux:
working_directory: ~/repo
docker: *ref_1
Expand All @@ -2115,7 +2133,7 @@ jobs:
AMPLIFY_DIR: /home/circleci/repo/out
AMPLIFY_PATH: /home/circleci/repo/out/amplify-pkg-linux
TEST_SUITE: src/__tests__/api_4.test.ts
CLI_REGION: eu-central-1
CLI_REGION: ap-northeast-1
steps: *ref_6
workflows:
version: 2
Expand Down Expand Up @@ -2234,11 +2252,11 @@ workflows:
- notifications-amplify_e2e_tests
- schema-iterative-update-locking-amplify_e2e_tests
- function_7-amplify_e2e_tests
- api_4-amplify_e2e_tests
- hosting-amplify_e2e_tests
- api_5-amplify_e2e_tests
- tags-amplify_e2e_tests
- s3-sse-amplify_e2e_tests
- function_6-amplify_e2e_tests
- api_4-amplify_e2e_tests
- amplify-app-amplify_e2e_tests
- init-amplify_e2e_tests
- pull-amplify_e2e_tests
Expand All @@ -2264,11 +2282,11 @@ workflows:
- notifications-amplify_e2e_tests_pkg_linux
- schema-iterative-update-locking-amplify_e2e_tests_pkg_linux
- function_7-amplify_e2e_tests_pkg_linux
- api_4-amplify_e2e_tests_pkg_linux
- hosting-amplify_e2e_tests_pkg_linux
- api_5-amplify_e2e_tests_pkg_linux
- tags-amplify_e2e_tests_pkg_linux
- s3-sse-amplify_e2e_tests_pkg_linux
- function_6-amplify_e2e_tests_pkg_linux
- api_4-amplify_e2e_tests_pkg_linux
- amplify-app-amplify_e2e_tests_pkg_linux
- init-amplify_e2e_tests_pkg_linux
- pull-amplify_e2e_tests_pkg_linux
Expand Down Expand Up @@ -2708,7 +2726,7 @@ workflows:
filters: *ref_10
requires:
- geo-remove-amplify_e2e_tests
- api_4-amplify_e2e_tests:
- api_5-amplify_e2e_tests:
context: *ref_8
post-steps: *ref_9
filters: *ref_10
Expand Down Expand Up @@ -2780,6 +2798,12 @@ workflows:
filters: *ref_10
requires:
- geo-update-amplify_e2e_tests
- api_4-amplify_e2e_tests:
context: *ref_8
post-steps: *ref_9
filters: *ref_10
requires:
- hosting-amplify_e2e_tests
- schema-auth-2-amplify_e2e_tests:
context: *ref_8
post-steps: *ref_9
Expand Down Expand Up @@ -3220,7 +3244,7 @@ workflows:
filters: *ref_13
requires:
- geo-remove-amplify_e2e_tests_pkg_linux
- api_4-amplify_e2e_tests_pkg_linux:
- api_5-amplify_e2e_tests_pkg_linux:
context: *ref_11
post-steps: *ref_12
filters: *ref_13
Expand Down Expand Up @@ -3296,6 +3320,12 @@ workflows:
filters: *ref_13
requires:
- geo-update-amplify_e2e_tests_pkg_linux
- api_4-amplify_e2e_tests_pkg_linux:
context: *ref_11
post-steps: *ref_12
filters: *ref_13
requires:
- hosting-amplify_e2e_tests_pkg_linux
- schema-auth-2-amplify_e2e_tests_pkg_linux:
context: *ref_11
post-steps: *ref_12
Expand Down
25 changes: 8 additions & 17 deletions packages/amplify-category-api/amplify-plugin.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
{
"name": "api",
"type": "category",
"commands": [
"add-graphql-datasource",
"add",
"console",
"gql-compile",
"push",
"remove",
"update",
"help"
],
"commandAliases":{
"configure": "update"
},
"eventHandlers": []
}
"name": "api",
"type": "category",
"commands": ["add-graphql-datasource", "add", "console", "gql-compile", "push", "rebuild", "remove", "update", "help"],
"commandAliases": {
"configure": "update"
},
"eventHandlers": []
}
1 change: 1 addition & 0 deletions packages/amplify-category-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@octokit/rest": "^18.0.9",
"amplify-cli-core": "1.29.0",
"amplify-headless-interface": "1.10.0",
"amplify-prompts": "1.1.2",
"amplify-util-headless-input": "1.5.4",
"chalk": "^4.1.1",
"constructs": "^3.3.125",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { $TSContext, FeatureFlags, stateManager } from 'amplify-cli-core';
import { printer, prompter } from 'amplify-prompts';
import { mocked } from 'ts-jest/utils';
import { run } from '../../../commands/api/rebuild';

jest.mock('amplify-cli-core');
jest.mock('amplify-prompts');

const FeatureFlags_mock = mocked(FeatureFlags);
const stateManager_mock = mocked(stateManager);
const printer_mock = mocked(printer);
const prompter_mock = mocked(prompter);

FeatureFlags_mock.getBoolean.mockReturnValue(true);

beforeEach(jest.clearAllMocks);

const pushResourcesMock = jest.fn();

const context_stub = {
amplify: {
constructExeInfo: jest.fn(),
pushResources: pushResourcesMock,
},
parameters: {
first: 'resourceName',
},
} as unknown as $TSContext;

it('prints error if iterative updates not enabled', async () => {
FeatureFlags_mock.getBoolean.mockReturnValueOnce(false);

await run(context_stub);

expect(printer_mock.error.mock.calls.length).toBe(1);
expect(pushResourcesMock.mock.calls.length).toBe(0);
});

it('exits early if no api in project', async () => {
stateManager_mock.getMeta.mockReturnValueOnce({
api: {},
});

await run(context_stub);

expect(printer_mock.info.mock.calls.length).toBe(1);
expect(pushResourcesMock.mock.calls.length).toBe(0);
});

it('asks for strong confirmation before continuing', async () => {
stateManager_mock.getMeta.mockReturnValueOnce({
api: {
testapiname: {
service: 'AppSync',
},
},
});

await run(context_stub);

expect(prompter_mock.input.mock.calls.length).toBe(1);
expect(pushResourcesMock.mock.calls.length).toBe(1);
expect(pushResourcesMock.mock.calls[0][4]).toBe(true); // rebuild flag is set
});
5 changes: 5 additions & 0 deletions packages/amplify-category-api/src/commands/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ module.exports = {
name: 'console',
description: 'Opens the web console for the selected api service',
},
{
name: 'rebuild',
description:
'Removes and recreates all DynamoDB tables backing a GraphQL API. Useful for resetting test data during the development phase of an app',
},
];

context.amplify.showHelp(header, commands);
Expand Down
40 changes: 40 additions & 0 deletions packages/amplify-category-api/src/commands/api/rebuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { $TSContext, FeatureFlags, stateManager } from 'amplify-cli-core';
import { printer, prompter, exact } from 'amplify-prompts';

const subcommand = 'rebuild';
const category = 'api';

export const name = subcommand;

const rebuild = true;

export const run = async (context: $TSContext) => {
if (!FeatureFlags.getBoolean('graphqlTransformer.enableIterativeGSIUpdates')) {
printer.error('Iterative GSI Updates must be enabled to rebuild an API. See https://docs.amplify.aws/cli/reference/feature-flags/');
return;
}
const apiNames = Object.entries(stateManager.getMeta()?.api || {})
.filter(([_, meta]) => (meta as any).service === 'AppSync')
.map(([name]) => name);
if (apiNames.length === 0) {
printer.info('No GraphQL API configured in the project. Only GraphQL APIs can be rebuilt. To add a GraphQL API run `amplify add api`.');
return;
}
if (apiNames.length > 1) {
// this condition should never hit as we have upstream defensive logic to prevent multiple GraphQL APIs. But just to cover all the bases
printer.error(
'You have multiple GraphQL APIs in the project. Only one GraphQL API is allowed per project. Run `amplify remove api` to remove an API.',
);
return;
}
const apiName = apiNames[0];
printer.warn(`This will recreate all tables backing models in your GraphQL API ${apiName}.`);
printer.warn('ALL EXISTING DATA IN THESE TABLES WILL BE LOST.');
await prompter.input('Type the name of the API to confirm you want to continue', {
validate: exact(apiName, 'Input does not match the GraphQL API name'),
});
const { amplify, parameters } = context;
const resourceName = parameters.first;
amplify.constructExeInfo(context);
return amplify.pushResources(context, category, resourceName, undefined, rebuild);
};
2 changes: 2 additions & 0 deletions packages/amplify-category-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"src/__tests__"
],
"references": [
{"path": "../amplify-cli-core"},
{"path": "../amplify-headless-interface"},
{"path": "../amplify-prompts"},
{"path": "../graphql-transformer-core"},
{"path": "../amplify-util-headless-input"},
]
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-category-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { hashLayerResource } from './provider-utils/awscloudformation/utils/laye
export { migrateLegacyLayer } from './provider-utils/awscloudformation/utils/layerMigrationUtils';
export { packageResource } from './provider-utils/awscloudformation/utils/package';
export { updateDependentFunctionsCfn } from './provider-utils/awscloudformation/utils/updateDependentFunctionCfn';
export { loadFunctionParameters } from './provider-utils/awscloudformation/utils/loadFunctionParameters';
edwardfoyle marked this conversation as resolved.
Show resolved Hide resolved

export async function add(context, providerName, service, parameters) {
const options = {
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-cli-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ interface AmplifyToolkit {
category?: string,
resourceName?: string,
filteredResources?: { category: string; resourceName: string }[],
rebuild?: boolean,
) => $TSAny;
storeCurrentCloudBackend: () => $TSAny;
readJsonFile: (fileName: string) => $TSAny;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function pushResources(
category?: string,
resourceName?: string,
filteredResources?: { category: string; resourceName: string }[],
rebuild: boolean = false,
) {
if (context.parameters.options['iterative-rollback']) {
// validate --iterative-rollback with --force
Expand Down Expand Up @@ -49,16 +50,21 @@ export async function pushResources(
}
}

const hasChanges = await showResourceTable(category, resourceName, filteredResources);
let hasChanges = false;
if (!rebuild) {
// status table does not have a way to show resource in "rebuild" state so skipping it to avoid confusion
hasChanges = await showResourceTable(category, resourceName, filteredResources);
}

// no changes detected
if (!hasChanges && !context.exeInfo.forcePush) {
if (!hasChanges && !context.exeInfo.forcePush && !rebuild) {
context.print.info('\nNo changes detected');

return context;
}

let continueToPush = context.exeInfo && context.exeInfo.inputParams && context.exeInfo.inputParams.yes;
// rebuild has an upstream confirmation prompt so no need to prompt again here
let continueToPush = (context.exeInfo && context.exeInfo.inputParams && context.exeInfo.inputParams.yes) || rebuild;

if (!continueToPush) {
if (context.exeInfo.iterativeRollback) {
Expand All @@ -68,18 +74,11 @@ export async function pushResources(
}

if (continueToPush) {
try {
// Get current-cloud-backend's amplify-meta
const currentAmplifyMeta = stateManager.getCurrentMeta();

await providersPush(context, category, resourceName, filteredResources);
await onCategoryOutputsChange(context, currentAmplifyMeta);
} catch (err) {
// Handle the errors and print them nicely for the user.
context.print.error(`\n${err.message}`);
// Get current-cloud-backend's amplify-meta
const currentAmplifyMeta = stateManager.getCurrentMeta();

throw err;
}
await providersPush(context, rebuild, category, resourceName, filteredResources);
await onCategoryOutputsChange(context, currentAmplifyMeta);
} else {
// there's currently no other mechanism to stop the execution of the postPush workflow in this case, so exiting here
exitOnNextTick(1);
Expand All @@ -88,15 +87,21 @@ export async function pushResources(
return continueToPush;
}

async function providersPush(context: $TSContext, category, resourceName, filteredResources) {
async function providersPush(
context: $TSContext,
rebuild: boolean = false,
category?: string,
resourceName?: string,
filteredResources?: { category: string; resourceName: string }[],
) {
const { providers } = getProjectConfig();
const providerPlugins = getProviderPlugins(context);
const providerPromises: (() => Promise<$TSAny>)[] = [];

for (const provider of providers) {
const providerModule = require(providerPlugins[provider]);
const resourceDefiniton = await context.amplify.getResourceStatus(category, resourceName, provider, filteredResources);
providerPromises.push(providerModule.pushResources(context, resourceDefiniton));
providerPromises.push(providerModule.pushResources(context, resourceDefiniton, rebuild));
}

await Promise.all(providerPromises);
Expand Down
10 changes: 10 additions & 0 deletions packages/amplify-e2e-core/src/categories/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,13 @@ export function addRestContainerApi(projectDir: string) {
});
});
}

export function rebuildApi(projDir: string, apiName: string) {
return new Promise<void>((resolve, reject) => {
spawn(getCLIPath(), ['rebuild', 'api'], { cwd: projDir, stripColors: true })
.wait('Type the name of the API to confirm you want to continue')
.sendLine(apiName)
.wait('All resources are updated in the cloud')
.run(err => (err ? reject(err) : resolve()));
});
}
Loading