Repository navigation
feat: Implement Default Values DateTime field type in Models - #2383
Conversation
SandPod
left a comment
There was a problem hiding this comment.
This is going to be really great to get in.
Before looking to deep into the implementation I wanted to ensure we have thought of all scenarios.
Here is a partial review with mostly a bunch of discussion points to ensure we have thought of everything with the implementation.
Isakdl
left a comment
There was a problem hiding this comment.
Great work on this huge feature! 😄
I got some change requests and there are a few test scenarios we need to add to make sure we cover all important situations! The tests them selfs also needs a refactor pass to focus on testing only one thing!
…ultDatabaseVal' to 'defaultDatabaseValue'
…efault' value tests
There was a problem hiding this comment.
A lot of nice tests! 🧪
I have added a few comments on some of the tests. The biggest problem here is that we have some bugs in the production code as the e2e migration tests are not correctly implemented and still pass.
Please carefully check the test scenarios I mentioned before and compare them to the tests. These are dangerous to get wrong as we could accidentally destroy user data if we do not give the correct warning.
- Add a none nullable column with a default value to an existing table (this should be possible without having to use --force)
- Modify an existing nullable column to none nullable column but with a default value (also without --force)
- Remove the default value of an existing none nullable column (should require --force)
- Remove the default value and convert the existing none nullable column to nullable (possible without --force)
- Removing a column with a default value (should require --force)
Isakdl
left a comment
There was a problem hiding this comment.
Please check the comments, last thing on the force flag!
Overview
This PR introduces support for default values in DateTime field types.
Default Types
There are three types of default values that can be specified:
defaultDatabase: Adds a default value to the database table column.defaultModel: Adds a default value to the model field.default: Adds default values to both the model field and the database table column.Default Value Options for DateTime Fields
Two types of default values can be assigned to DateTime fields:
now: Sets the value to the current date and time.DateTime.now()CURRENT_TIMESTAMP"2024-05-01T22:00:00.000Z". The format must be in UTC.Example:
Migrations
When adding a default value to a field, only
defaultordefaultDatabasewill affect the database, asdefaultModelis ignored since it is only applicable on the model side.The process involves the following steps:
Setting the Default Value:
columnDefaultproperty in theColumnDefinitionclass.columnDefaultvalue is then incorporated into the SQL code as... DEFAULT $columnDefault.Removing the Default Value:
... DROP DEFAULTto eliminate the previously set default value from the column.Restrictions
Related issue: #1140 #236
Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.