Skip to content

Commit

Permalink
Merge pull request #269 from MasoniteFramework/fix/268
Browse files Browse the repository at this point in the history
fixing issue with updating scope
  • Loading branch information
josephmancuso authored Dec 19, 2020
2 parents a4f02da + 0c2dd90 commit df88482
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/masoniteorm/query/grammars/BaseGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions src/masoniteorm/scopes/TimeStampsScope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .BaseScope import BaseScope

from ..expressions.expressions import UpdateQueryExpression


class TimeStampsScope(BaseScope):
"""Global scope class to add soft deleting to models."""
Expand All @@ -9,6 +11,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

Expand All @@ -18,9 +24,20 @@ 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()}
),
)
2 changes: 2 additions & 0 deletions tests/mysql/builder/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Articles(Model):


class User(Model):
__timestamps__ = False

@has_many("id", "user_id")
def articles(self):
return Articles
Expand Down
6 changes: 5 additions & 1 deletion tests/postgres/builder/test_postgres_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down

0 comments on commit df88482

Please sign in to comment.