Skip to content

Commit

Permalink
Merge pull request #839 from stoutput/stoutput/use-model-attr-for-tim…
Browse files Browse the repository at this point in the history
…estamps

Use dynamic model variables for timestamp column names
  • Loading branch information
josephmancuso authored Sep 30, 2023
2 parents f2d57f7 + e261a07 commit d9c8e13
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/masoniteorm/scopes/TimeStampsScope.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .BaseScope import BaseScope

from ..expressions.expressions import UpdateQueryExpression
from .BaseScope import BaseScope


class TimeStampsScope(BaseScope):
Expand All @@ -27,8 +26,8 @@ def set_timestamp_create(self, builder):

builder._creates.update(
{
"updated_at": builder._model.get_new_date().to_datetime_string(),
"created_at": builder._model.get_new_date().to_datetime_string(),
builder._model.date_updated_at: builder._model.get_new_date().to_datetime_string(),
builder._model.date_created_at: builder._model.get_new_date().to_datetime_string(),
}
)

Expand All @@ -38,6 +37,8 @@ def set_timestamp_update(self, builder):

builder._updates += (
UpdateQueryExpression(
{"updated_at": builder._model.get_new_date().to_datetime_string()}
{
builder._model.date_updated_at: builder._model.get_new_date().to_datetime_string()
}
),
)
3 changes: 2 additions & 1 deletion tests/mysql/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import json
import os
import unittest

import pendulum

from src.masoniteorm.exceptions import ModelNotFound
from src.masoniteorm.collection import Collection
from src.masoniteorm.exceptions import ModelNotFound
from src.masoniteorm.models import Model
from tests.User import User

Expand Down
27 changes: 27 additions & 0 deletions tests/scopes/test_default_global_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class UserWithTimeStamps(Model, TimeStampsMixin):
__dry__ = True


class UserWithCustomTimeStamps(Model, TimeStampsMixin):
__dry__ = True
date_updated_at = "updated_ts"
date_created_at = "created_ts"


class UserSoft(Model, SoftDeletesMixin):
__dry__ = True

Expand Down Expand Up @@ -112,3 +118,24 @@ def test_timestamps_can_be_disabled(self):
self.scope.set_timestamp_create(self.builder)
self.assertNotIn("created_at", self.builder._creates)
self.assertNotIn("updated_at", self.builder._creates)

def test_uses_custom_timestamp_columns_on_create(self):
self.builder = MockBuilder(UserWithCustomTimeStamps)
self.scope.set_timestamp_create(self.builder)
created_column = UserWithCustomTimeStamps.date_created_at
updated_column = UserWithCustomTimeStamps.date_updated_at
self.assertNotIn("created_at", self.builder._creates)
self.assertNotIn("updated_at", self.builder._creates)
self.assertIn(created_column, self.builder._creates)
self.assertIn(updated_column, self.builder._creates)
self.assertIsInstance(
pendulum.parse(self.builder._creates[created_column]), pendulum.DateTime
)
self.assertIsInstance(
pendulum.parse(self.builder._creates[updated_column]), pendulum.DateTime
)

def test_uses_custom_updated_column_on_update(self):
user = UserWithCustomTimeStamps.hydrate({"id": 1})
sql = user.update({"id": 2}).to_sql()
self.assertTrue(UserWithCustomTimeStamps.date_updated_at in sql)

0 comments on commit d9c8e13

Please sign in to comment.