Skip to content

feat: Allow changing a table id type between int and uuid - #3299

Merged
SandPod merged 27 commits into
serverpod:mainfrom
marcelomendoncasoares:change-id-type
Apr 8, 2025
Merged

SandPod merged 27 commits into
serverpod:mainfrom
marcelomendoncasoares:change-id-type

Conversation

@marcelomendoncasoares

@marcelomendoncasoares marcelomendoncasoares commented Mar 13, 2025 •

Copy link
Copy Markdown
Collaborator

This PR implements the first feature described at #3255: allow the user to declare the id field type in serverpod models, changing its type to one of the supported id types (with the correct default attribution).

The implementation changes the following:

  • There is now a new experimental feature flag named changeIdType. The user is only allowed to declare the id field and change its type if this experimental feature is enabled.
  • The id field is no longer reserved for table classes, but only the default key can be specified. For the int type, the default key is optional with the value serial; for all others, it is required.
  • All supported id types are declared in the new SupportedIdType class. Currently supports int (default) and uuid (with default=random to generate UUIDv4 values).

The missing test for the PR #3257 is implemented in this PR. It validates that the stored FK field type on the relation objects is being collected from the correct model. Its the file relation_changed_field_id_type_test.dart of the serverpod_cli model validation tests.

Most of the id type tests were implemented with a little abstraction to ensure that all supported id types (current and future) are working as expected - both in isolation and in relations. This way, new supported id types in the future will be already 99% covered by tests, pending only to add one single line in the migration_sql_test.dart file to complete the coverage.

Closes: #3255

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

No breaking changes.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work!

Had a couple of nits and suggestions in different places. But my main concern is around iterators in tests. Wrote some motivation around why these are generally not a great idea. But I'm up for exploring if we can alievate my pain points while still maintaining the simple way to add features.

It would be great to explore what the minimal set of tests that are required are as well. Then we could probably reduce what is run in these iterators and hopefully just type them out.

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator Author

Still pending to follow-up in the tests revision, but will get to them very soon!

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Specifically looked at the changes regarding the comments that have been addressed.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is looking good to me!

Had a last couple of questions then we are ready to go I think :)

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Big change and really nice improvement to the framework!

Thank you for all the great work here! 🙏

@SandPod
SandPod merged commit 6a7c5b4 into serverpod:main Apr 8, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 8, 2025
@marcelomendoncasoares
marcelomendoncasoares deleted the change-id-type branch April 10, 2025 03:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Option to use UUIDs as primary keys

2 participants