Skip to content

feat: inheritance for DB class - #2799

Merged
SandPod merged 11 commits into
serverpod:mainfrom
BenAuerDev:feat-extend-table-class
Oct 7, 2024
Merged

SandPod merged 11 commits into
serverpod:mainfrom
BenAuerDev:feat-extend-table-class

Conversation

@BenAuerDev

@BenAuerDev BenAuerDev commented Oct 2, 2024 •

Copy link
Copy Markdown
Contributor

This PR adds the functionality allowing a class with DB Table to be extended aswell.

Changes:

  1. Removed restrictions disallowing inheritance on DB classes
  2. Added restriction disallowing multiple DB classes in the same hierarchy
  3. Removed tests checking for removed restrictions
  4. Added tests checking for new restriction AND test checking if a child-class with table has both its own and its parents fields.
  5. Simplifies how fields/inheritedFields/fieldsIncludingInherited is derived by adding a getter to ClassDefinition this includes a refactor of model_library_generator.dart
  6. Passes fieldsIncludingInherited instead of fields to createDatabaseDefinitionFromModels
  7. Adds e2e test checking if the migration is created correctly and table works as intended
  8. fixed typo

related to: #1873 and #1618

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

removes `mutuallyExclusiveKeys` from both `table` and `extends`

removes the restriction disallowing a parentClass from having a table

added the restriction disallowing 2 table-classes in the same hierarchy

inheritance_test.dart was updated:
- removes tests checking for mutuallyExclusiveKeys between table and extends
- adds test checking restriction that only one table in hierarchy is allowed

inheritance_class_test.dart
- adds test checking if child-class TableClass inherits all fields of parent
adds comments to ClassDefinition getters and adds another getter for `allFields` merging fields and parentFields together and keeps the id field first

passes allFields to `createDatabaseDefinitionFromModels`

model_library_generator.dart:
- refactors the way fields is used, setting fields equal to allFields and only using `fields` and `parentFields` when they need to be distinguished

- adds a ternary statement changing the type of 'id' to be `Object?` in _buildAbstractCopyWithParameters
Seems like I wrote `validateExtendingClassName` right inbetween `validateTableNameKey` and `validateTableName`

meaning they were ordered as
- validateTableNameKey
- validateExtendingClassName
-validateTableName

and now
- validateTableNameKey
-validateTableName
- validateExtendingClassName

@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! 🌟

Getting closer!

I have a couple of comments and suggestions.

But I would also like to see some e2e tests that validate we can define models with the different hierarchies.

Say for example 3 tests where the table is defined in all different places in this hierarchy:
Grandparent -> Parent -> Child.

This would help us validate that the migrations are generated correctly.

changes ParentClass to have a table
renames fields
adds GrandParentClass

adds test checking if child can be inserted in parent-table

squashes new migration with previous migration
…ngInherited` from parent

rename:
- parentFields => inheritedFields
- allFields => fieldsIncludingInherited

also in inheritedFields now `fieldsIncludingInherited` are passed to accomodate multi level inheritance
rename private variable
remove cast and add guard checking if currentModel is ClassDefnition

improve wording of Exception thrown when multiple classes have inheritance
improve wording of test description

@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, last couple of comments and suggestions!

@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.

LGTM!

Once last thing to fix.

@SandPod
SandPod merged commit a19e166 into serverpod:main Oct 7, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 7, 2024
@BenAuerDev
BenAuerDev deleted the feat-extend-table-class branch October 11, 2024 12:52
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.

2 participants