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: auto increment support #2883

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

feat: auto increment support #2883

wants to merge 18 commits into from

Conversation

p5quared
Copy link
Member

@p5quared p5quared commented Sep 18, 2024

Description of changes

Transformer changes to support auto increment (serial) fields on Postgres datasources. Accomplished by making the 'create' input types optional when @default is applied.

  1. Definition of @default has changed, its value: String parameter is now optional.
  2. default-value-transformer allows no value on Int fields if the datasource is Postgres
  3. Unit tests for the above
CDK / CloudFormation Parameters Changed

n/a

Description of how you validated changes

Unit tests, existing e2e tests, testTransform. Will write e2e test after implementing changes to Lambda layer.

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

Codebuild Tests

Ran against beta via yarn split-e2e-tests-beta.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@p5quared p5quared force-pushed the auto-increment-support branch 2 times, most recently from 9922afd to c0bc7ff Compare September 18, 2024 18:30
@p5quared p5quared changed the title Auto increment support feat: auto increment support Sep 18, 2024
@p5quared p5quared marked this pull request as ready for review September 18, 2024 18:49
@p5quared p5quared requested review from a team as code owners September 18, 2024 18:49
@p5quared p5quared force-pushed the auto-increment-support branch 2 times, most recently from 7ddef03 to 376fa27 Compare September 18, 2024 21:55
sundersc
sundersc previously approved these changes Sep 23, 2024
@p5quared p5quared force-pushed the auto-increment-support branch 2 times, most recently from 29a8c20 to 3896d20 Compare September 30, 2024 16:45
@p5quared p5quared force-pushed the auto-increment-support branch 4 times, most recently from 5235728 to 9708662 Compare October 1, 2024 18:08
@p5quared p5quared requested a review from sundersc October 1, 2024 18:12
@p5quared p5quared force-pushed the auto-increment-support branch 2 times, most recently from 9d796c6 to ead5e20 Compare October 1, 2024 19:20
`;

const { projFolderName, region, connectionConfigName, dbController, resourceNames } = options;
const templatePath = path.resolve(path.join(__dirname, '..', '__tests__', 'backends', 'sql-models'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now or in the next PR: Can we migrate this test to use the configurable stack so we can (eventually) remove the sql-models stack? Eventually I'd like us to be using just one stack to reduce the number of test fixtures we have to maintain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's okay I'd like to defer this migration to a separate PR and migrate a number of tests at once.

describe(`${testBlockDescription} - ${engine}`, () => {
const amplifyGraphqlSchema = `
type CoffeeQueue @model @refersTo(name: "${getRDSTableNamePrefix()}coffee_queue") {
orderNumber: Int! @primaryKey @default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth nothing in a comment here the important characteristics of this tests:

  1. It's an Int field, backed by a SERIAL type in PG
  2. It's a required field, which would normally mean that customers would have to supply a value in the GraphQL mutation input
  3. It has a @default annotation with no arguments, meaning that the only behavior our transformer should do is to suppress the input requirement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted in comment

coffeeQueueTableCRUDLHelper.checkGenericError(error?.message);
}

const invalidOrderNumber = 99999999;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is invalid because it exceeds the maximum value of a SERIAL field? If so, worth noting that in a comment, in case we start supporting PG versions that have a higher limit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it is invalid because it is greater than any existing record's orderNumber. That purpose is definitely not clear since even the number 4 should be invalid. I wrote some comments but then opted to use a more descriptive variable name:

const biggerThanAnyExistingOrderNumber = 99999999;

transformers: [new ModelTransformer(), new DefaultValueTransformer(), new PrimaryKeyTransformer()],
dataSourceStrategies: constructDataSourceStrategies(schema, strategy),
});
}).toThrow('The @default directive requires a value property on non Postgres datasources.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Can you confirm that a @default(value: ...) works as expected on a mysql data source? (That is, it passes specified value in the input if the mutation input doesn't include it; and it allows overrides of the default if it IS specified). Best way to confirm that would be a test (possibly an already existing one!)
  2. Do we have a test (possibly an already existing one!) to assert that @default(value:...) works for postgres?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only e2e usage of @default I could find only used DDB. I modified the general sql models test to utilize @default(value) which should verify behavior on both sources.

packages/amplify-graphql-transformer-core/src/index.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants