Skip to content

feat: Implement Default Values DateTime field type in Models - #2383

Merged
Isakdl merged 55 commits into
serverpod:mainfrom
klkucaj:feature/1140
Jul 11, 2024
Merged

Isakdl merged 55 commits into
serverpod:mainfrom
klkucaj:feature/1140

Conversation

@klkucaj

@klkucaj klkucaj commented Jun 14, 2024 •

Copy link
Copy Markdown
Contributor

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:

  1. defaultDatabase: Adds a default value to the database table column.
  2. defaultModel: Adds a default value to the model field.
  3. 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:

  1. now: Sets the value to the current date and time.
    • Dart Side: DateTime.now()
    • Database Side: CURRENT_TIMESTAMP
  2. UTC DateTime String: For example, "2024-05-01T22:00:00.000Z". The format must be in UTC.

Example:

class: Example
table: example
fields:
  # default
  dateTimeDefaultNow: DateTime, default=now
  dateTimeDefaultStr: DateTime, default=2024-05-01T22:00:00.000Z
  
  # defaultModel
  dateTimeDefaultModelNow: DateTime, defaultModel=now
  dateTimeDefaultModelStr: DateTime, defaultModel=2024-05-01T22:00:00.000Z
  
  # defaultDatabase
  dateTimeDefaultDatabaseNow: DateTime?, defaultDatabase=now
  dateTimeDefaultDatabaseStr: DateTime?, defaultDatabase=2024-05-01T22:00:00.000Z

Migrations

When adding a default value to a field, only default or defaultDatabase will affect the database, as defaultModel is ignored since it is only applicable on the model side.

The process involves the following steps:

  1. Setting the Default Value:

    • The default value is assigned to the columnDefault property in the ColumnDefinition class.
    • This columnDefault value is then incorporated into the SQL code as ... DEFAULT $columnDefault.
  2. Removing the Default Value:

    • If the default value needs to be removed, a new migration is required.
    • The migration SQL code will include ... DROP DEFAULT to eliminate the previously set default value from the column.

Restrictions

  • relation keyword cannot be used with defaults.
  • !persist keyword cannot be used when defaultDatabase is specified.
  • when using only defaultDatabase in a DateTime field, this field is required to be nullable.

Related issue: #1140 #236

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.

@klkucaj klkucaj changed the title feat: Implement Default Values in Models feat: Implement Default Values DateTime field type in Models Jun 20, 2024

@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 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 Isakdl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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!

@Isakdl Isakdl left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 Isakdl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please check the comments, last thing on the force flag!

@Isakdl
Isakdl self-requested a review July 11, 2024 11:22

@Isakdl Isakdl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM 🚀

Well done @klkucaj

@Isakdl
Isakdl merged commit cf8b243 into serverpod:main Jul 11, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 11, 2024
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.

3 participants