feat: add migration methods Morphs, Uuid and Ulid and morphs for each#1114
feat: add migration methods Morphs, Uuid and Ulid and morphs for each#1114hwbrzzl merged 23 commits intogoravel:masterfrom
Conversation
…ds to Blueprint for polymorphic relationships
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1114 +/- ##
=======================================
Coverage 66.82% 66.82%
=======================================
Files 214 214
Lines 14104 14104
=======================================
Hits 9425 9425
Misses 4303 4303
Partials 376 376 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR adds polymorphic morph column support and enhances UUID/ULID column handling in the schema blueprint, alongside configuration and test coverage updates.
- Introduced
Morphs,NullableMorphs,NumericMorphs,UuidMorphs,UlidMorphsmethods inBlueprint - Added global morph key type configuration with convenience functions in
morph_config.go - Expanded tests for new column types and morph behaviors
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| mocks/database/schema/Blueprint.go | Added mock implementations for new morph and ID methods |
| database/schema/morph_config.go | Introduced default morph key type config and utilities |
| database/schema/blueprint.go | Implemented morph and UUID/ULID column builder methods |
| contracts/database/schema/blueprint.go | Updated interface with new methods |
| database/schema/blueprint_test.go | Added tests for Uuid, Ulid, morph methods and config |
Comments suppressed due to low confidence (2)
database/schema/blueprint_test.go:807
- TestUuidMorphs does not include a scenario for custom index names. Consider adding a test similar to TestNumericMorphs to verify custom index naming for UuidMorphs.
func (s *BlueprintTestSuite) TestUuidMorphs() {
database/schema/morph_config.go:19
- [nitpick] The error message lists valid key types but doesn't include the invalid value passed. Including the actual keyType in the error could aid debugging.
return fmt.Errorf("morph key type must be '%s', '%s', or '%s'", MorphKeyTypeInt, MorphKeyTypeUuid, MorphKeyTypeUlid)
|
If you modify any file under the contracts folder, please run the |
database/schema/morph_config.go
Outdated
| if keyType != MorphKeyTypeInt && keyType != MorphKeyTypeUuid && keyType != MorphKeyTypeUlid { | ||
| return fmt.Errorf("morph key type must be '%s', '%s', or '%s'", MorphKeyTypeInt, MorphKeyTypeUuid, MorphKeyTypeUlid) | ||
| } |
There was a problem hiding this comment.
Given we define a new MorphKeyType type, this judgment is unnecessary.
database/schema/blueprint_test.go
Outdated
| }) | ||
| } | ||
|
|
||
| func (s *BlueprintTestSuite) TestMorphKeyTypeConfiguration() { |
There was a problem hiding this comment.
Could you move this test function to morph_config_test.go?
…actor MorphKeyType configuration tests
hwbrzzl
left a comment
There was a problem hiding this comment.
Left a few questions, and FYI, the merge process when adding new functions in schema will be:
- Define new interface functions and implement main logic in goravel/framework
- Implement new interface functions in every drivers
- Merge all drver PRs first
- Add test cases for the new functions in the goravel/framework::tests folder
- Merge goravel/framework's PR
database/schema/blueprint.go
Outdated
| func (r *Blueprint) Uuid(column string) driver.ColumnDefinition { | ||
| return r.createAndAddColumn("uuid", column) | ||
| } |
There was a problem hiding this comment.
Please sort the function based on the first letter.
There was a problem hiding this comment.
It seems not to be changed.
contracts/database/driver/grammar.go
Outdated
| // TypeUuid Create the column definition for a uuid type. | ||
| TypeUuid(column ColumnDefinition) string | ||
| // TypeUlid Create the column definition for a ulid type. | ||
| TypeUlid(column ColumnDefinition) string |
There was a problem hiding this comment.
Is TypeUlid required? Can TypeChat be used instead? I noticed that TypeUlid is char(26) in each driver.
There was a problem hiding this comment.
Please run go run github.com/vektra/mockery/v2 to regenerate this file.
… Ulid methods in Blueprint for column definitions
…eter for column definition
…ameters for column definitions
… Ulid method to use constant for default length
hwbrzzl
left a comment
There was a problem hiding this comment.
Great 👍 Please add test cases in tests/query_test.go for the new feature.
database/schema/blueprint.go
Outdated
| columnImpl := r.createAndAddColumn("char", column) | ||
| columnImpl.length = &defaultLength | ||
|
|
||
| return columnImpl |
There was a problem hiding this comment.
Char can be called here directly.
database/schema/blueprint.go
Outdated
| func (r *Blueprint) Uuid(column string) driver.ColumnDefinition { | ||
| return r.createAndAddColumn("uuid", column) | ||
| } |
There was a problem hiding this comment.
It seems not to be changed.
…nt.go for improved organization
…sistency and clarity
|
Hi @ahmed3mar Thanks for your great PR, and FYI, v1.16 is going to be released in about one/two weeks. I think this PR is almost finished, it only lacks test cases in the |
…ionships - Introduced UuidEntity and UlidEntity models for testing UUID and ULID columns. - Added MorphableEntity, UuidMorphableEntity, and UlidMorphableEntity models for testing polymorphic relationships. - Implemented comprehensive test cases for UUID and ULID column operations, including creation, retrieval, and format validation. - Enhanced tests for morphable relationships, ensuring correct functionality across different entity types.
hwbrzzl
left a comment
There was a problem hiding this comment.
Great 👍 but CI failed, you can only test postgres driver locally, the speed will be faster: https://github.com/goravel/framework/blob/master/tests/query.go#L131-L143
tests/query_test.go
Outdated
| } | ||
|
|
||
| s.Run(fmt.Sprintf("TestUuidColumn_%s", driver), func() { | ||
| query.CreateTable(TestTableUuidEntities) |
There was a problem hiding this comment.
It's unnecessary, the table will be created here https://github.com/goravel/framework/blob/master/tests/query_test.go#L44
There was a problem hiding this comment.
The test cases only covered the create and Find flows, we'd better add some test cases for the Relationship query: facades.Orm().Query().With({morphable_table}).Fine(&user).
There was a problem hiding this comment.
Bug: Race Condition in Global Morph Key Type
The global variable defaultMorphKeyType is subject to race conditions. Multiple functions (e.g., SetDefaultMorphKeyType, GetDefaultMorphKeyType, MorphUsingUuids) access and modify this variable without any synchronization. This lack of protection can lead to inconsistent reads and writes, causing data races when multiple goroutines interact with the morph key type configuration concurrently.
database/schema/morph_config.go#L11-L37
framework/database/schema/morph_config.go
Lines 11 to 37 in e444efa
BugBot free trial expires on July 22, 2025
You have used $0.00 of your $1.00 spend limit so far. Manage your spend limit in the Cursor dashboard.
Was this report helpful? Give feedback by reacting with 👍 or 👎
|
Hey @ahmed3mar, thanks for your amazing PRs. Congratulations on getting a v1.16 commemorative T-shirt. I sent a message on Discord, please contact me. |
📑 Description
This pull request introduces support for morph columns in polymorphic relationships, along with enhancements to UUID and ULID column handling in the database schema blueprint. It also includes the ability to configure the default morph key type and comprehensive test coverage for these new features.
Morph Column Enhancements:
Blueprintfor creating morph columns:Morphs,NullableMorphs,NumericMorphs,UuidMorphs, andUlidMorphs. These methods generate the necessary type and ID columns, along with optional indexing. (contracts/database/schema/blueprint.go,database/schema/blueprint.go- [1] [2]morph_config.goto manage the default morph key type (int,uuid, orulid) with utility functions likeSetDefaultMorphKeyType,GetDefaultMorphKeyType, and convenience methods (MorphUsingUuids,MorphUsingUlids,MorphUsingInts). (database/schema/morph_config.go- database/schema/morph_config.goR1-R43)UUID and ULID Column Support:
UuidandUlidmethods toBlueprintfor creating respective columns. (contracts/database/schema/blueprint.go,database/schema/blueprint.go- [1] [2]Testing Enhancements:
BlueprintTestSuiteto validate the behavior of morph columns, UUID/ULID columns, and morph key type configuration. Tests cover scenarios for default, nullable, and custom key types. (database/schema/blueprint_test.go- database/schema/blueprint_test.goR736-R1081)Mock Support for Morph Columns:
Morphs,NullableMorphs, andNumericMorphsmethods, enabling testing of these methods in isolation. (mocks/database/schema/Blueprint.go- mocks/database/schema/Blueprint.goR2266-R2409)Related PRs
goravel/sqlserver#30
goravel/sqlite#22
goravel/postgres#56
goravel/mysql#45
✅ Checks