From b35d2106476cbbaff9113f3e53fe4fef6f9d2e47 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Wed, 16 Dec 2020 22:23:40 -0500 Subject: [PATCH 1/3] fixing issue with updating scope --- setup.py | 2 +- src/masoniteorm/models/Model.py | 2 +- src/masoniteorm/query/grammars/BaseGrammar.py | 6 +++--- src/masoniteorm/scopes/TimeStampsScope.py | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 5a2cd986..44b1e385 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='0.9.1', + version='0.9.2', package_dir={'': 'src'}, description='The Official Masonite ORM', diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 1a632599..7cd4028c 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -225,7 +225,7 @@ def find(cls, record_id, query=False): if query: return builder.to_sql() else: - return builder.get() + return builder.first() def first_or_new(self): pass diff --git a/src/masoniteorm/query/grammars/BaseGrammar.py b/src/masoniteorm/query/grammars/BaseGrammar.py index 36fdd60b..8b6b13bd 100644 --- a/src/masoniteorm/query/grammars/BaseGrammar.py +++ b/src/masoniteorm/query/grammars/BaseGrammar.py @@ -214,12 +214,12 @@ def _compile_key_value_equals(self, qmark=False): sql += sql_string.format( column=self._table_column_string(column), value=value if not qmark else "?", - separator=",", + separator=", ", ) if qmark: self._bindings += (value,) - sql = sql.rstrip(", ") + sql = sql.rstrip(", ") return sql @@ -394,7 +394,7 @@ def process_wheres(self, qmark=False, strip_first_where=False): if not isinstance(where.bindings, (list, tuple)): raise ValueError( - f"Binings must be tuple or list. Received {type(where.bindings)}" + f"Bindings must be tuple or list. Received {type(where.bindings)}" ) if where.bindings: diff --git a/src/masoniteorm/scopes/TimeStampsScope.py b/src/masoniteorm/scopes/TimeStampsScope.py index 304119fd..fe5bf2cc 100644 --- a/src/masoniteorm/scopes/TimeStampsScope.py +++ b/src/masoniteorm/scopes/TimeStampsScope.py @@ -1,5 +1,9 @@ from .BaseScope import BaseScope +from ..expressions.expressions import ( + UpdateQueryExpression, +) + class TimeStampsScope(BaseScope): """Global scope class to add soft deleting to models.""" @@ -9,6 +13,10 @@ def on_boot(self, builder): "_timestamps", self.set_timestamp_create, action="insert" ) + builder.set_global_scope( + "_timestamp_update", self.set_timestamp_update, action="update" + ) + def on_remove(self, builder): pass @@ -18,9 +26,16 @@ def set_timestamp(owner_cls, query): def set_timestamp_create(self, builder): if not builder._model.__timestamps__: return builder + builder._creates.update( { "updated_at": builder._model.get_new_date().to_datetime_string(), "created_at": builder._model.get_new_date().to_datetime_string(), } ) + + def set_timestamp_update(self, builder): + if not builder._model.__timestamps__: + return builder + + builder._updates += (UpdateQueryExpression({"updated_at": builder._model.get_new_date().to_datetime_string()}),) From 465718bbc23b37f0a18610bd48a7bd3948605f02 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Wed, 16 Dec 2020 22:27:12 -0500 Subject: [PATCH 2/3] Fixed tests --- tests/mysql/builder/test_query_builder.py | 2 ++ tests/postgres/builder/test_postgres_query_builder.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/mysql/builder/test_query_builder.py b/tests/mysql/builder/test_query_builder.py index 4b7a4a25..dcce1a64 100644 --- a/tests/mysql/builder/test_query_builder.py +++ b/tests/mysql/builder/test_query_builder.py @@ -15,6 +15,8 @@ class Articles(Model): class User(Model): + __timestamps__ = False + @has_many("id", "user_id") def articles(self): return Articles diff --git a/tests/postgres/builder/test_postgres_query_builder.py b/tests/postgres/builder/test_postgres_query_builder.py index 6512d3c8..88418013 100644 --- a/tests/postgres/builder/test_postgres_query_builder.py +++ b/tests/postgres/builder/test_postgres_query_builder.py @@ -20,6 +20,10 @@ def get_default_query_grammar(cls): return +class ModelTest(Model): + __timestamps__ = False + + class BaseTestQueryBuilder: def get_builder(self, table="users"): connection = MockConnectionFactory().make("postgres") @@ -28,7 +32,7 @@ def get_builder(self, table="users"): connection_class=connection, connection="postgres", table=table, - model=Model(), + model=ModelTest(), ) def test_sum(self): From 0c2dd90d193f8822e51dbe585104fd154df4b9fc Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Wed, 16 Dec 2020 22:27:16 -0500 Subject: [PATCH 3/3] formatted --- src/masoniteorm/scopes/TimeStampsScope.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/masoniteorm/scopes/TimeStampsScope.py b/src/masoniteorm/scopes/TimeStampsScope.py index fe5bf2cc..d24ed957 100644 --- a/src/masoniteorm/scopes/TimeStampsScope.py +++ b/src/masoniteorm/scopes/TimeStampsScope.py @@ -1,8 +1,6 @@ from .BaseScope import BaseScope -from ..expressions.expressions import ( - UpdateQueryExpression, -) +from ..expressions.expressions import UpdateQueryExpression class TimeStampsScope(BaseScope): @@ -38,4 +36,8 @@ def set_timestamp_update(self, builder): if not builder._model.__timestamps__: return builder - builder._updates += (UpdateQueryExpression({"updated_at": builder._model.get_new_date().to_datetime_string()}),) + builder._updates += ( + UpdateQueryExpression( + {"updated_at": builder._model.get_new_date().to_datetime_string()} + ), + )