Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
13f11442
Unverified
Commit
13f11442
authored
2 years ago
by
Cam Saul
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Toucan `:timestamped` property should not stomp explicit `:created_at` or `:updated_at` (#25918)
parent
ccfd9b93
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/models/interface.clj
+4
-2
4 additions, 2 deletions
src/metabase/models/interface.clj
test/metabase/models/interface_test.clj
+40
-8
40 additions, 8 deletions
test/metabase/models/interface_test.clj
with
44 additions
and
10 deletions
src/metabase/models/interface.clj
+
4
−
2
View file @
13f11442
...
...
@@ -228,10 +228,12 @@
((
resolve
'metabase.driver.sql.query-processor/current-datetime-honeysql-form
)
(
mdb.connection/db-type
)))
(
defn-
add-created-at-timestamp
[
obj
&
_
]
(
assoc
obj
:created_at
(
now
)))
(
cond->
obj
(
not
(
:created_at
obj
))
(
assoc
:created_at
(
now
))))
(
defn-
add-updated-at-timestamp
[
obj
&
_
]
(
assoc
obj
:updated_at
(
now
)))
(
cond->
obj
(
not
(
:updated_at
obj
))
(
assoc
:updated_at
(
now
))))
(
models/add-property!
:timestamped?
:insert
(
comp
add-created-at-timestamp
add-updated-at-timestamp
)
...
...
This diff is collapsed.
Click to expand it.
test/metabase/models/interface_test.clj
+
40
−
8
View file @
13f11442
(
ns
metabase.models.interface-test
(
:require
[
cheshire.core
:as
json
]
[
clojure.test
:refer
:all
]
[
metabase.mbql.normalize
:as
mbql.normalize
]
[
toucan.models
:as
models
]))
(
:require
[
cheshire.core
:as
json
]
[
clojure.test
:refer
:all
]
[
java-time
:as
t
]
[
metabase.db.connection
:as
mdb.connection
]
[
metabase.mbql.normalize
:as
mbql.normalize
]
[
metabase.models.field
:refer
[
Field
]]
[
metabase.test
:as
mt
]
[
schema.core
:as
s
]
[
toucan.models
:as
models
]))
;; let's make sure the `:metabase-query`/`:metric-segment-definition`/`::dashboard-card/parameter-mappings`
;; normalization functions respond gracefully to invalid stuff when pulling them out of the Database. See #8914
...
...
@@ -10,7 +16,7 @@
(
defn
type-fn
[
toucan-type
in-or-out
]
(
->
@@#
'models/type-fns
toucan-type
in-or-out
))
(
deftest
handle-bad-template-tags-test
(
deftest
^
:parallel
handle-bad-template-tags-test
(
testing
(
str
"an malformed template tags map like the one below is invalid. Rather than potentially destroy an entire API "
"response because of one malformed Card, dump the error to the logs and return nil."
)
(
is
(
=
nil
...
...
@@ -20,7 +26,7 @@
:type
:native
:native
{
:template-tags
1000
}}))))))
(
deftest
template-tag-validate-saves-test
(
deftest
^
:parallel
template-tag-validate-saves-test
(
testing
"on the other hand we should be a little more strict on the way and disallow you from saving the invalid stuff"
;; TODO -- we should make sure this returns a good error message so we don't have to dig thru the exception chain.
(
is
(
thrown?
...
...
@@ -30,14 +36,14 @@
:type
:native
:native
{
:template-tags
{
100
[
:field-id
"WOW"
]}}})))))
(
deftest
normalize-metric-segment-definition-test
(
deftest
^
:parallel
normalize-metric-segment-definition-test
(
testing
"Legacy Metric/Segment definitions should get normalized"
(
is
(
=
{
:filter
[
:=
[
:field
1
nil
]
[
:field
2
{
:temporal-unit
:month
}]]}
((
type-fn
:metric-segment-definition
:out
)
(
json/generate-string
{
:filter
[
:=
[
:field-id
1
]
[
:datetime-field
[
:field-id
2
]
:month
]]}))))))
(
deftest
dont-explode-on-way-out-from-db-test
(
deftest
^
:parallel
dont-explode-on-way-out-from-db-test
(
testing
"`metric-segment-definition`s should avoid explosions coming out of the DB..."
(
is
(
=
nil
((
type-fn
:metric-segment-definition
:out
)
...
...
@@ -66,3 +72,29 @@
(
with-redefs
[
mbql.normalize/normalize-tokens
(
fn
[
&
_
]
(
throw
(
Exception.
"BARF"
)))]
((
type-fn
:parameters-list
:in
)
[{
:target
[
:dimension
[
:field
"ABC"
nil
]]}]))))))
(
deftest
timestamped-property-do-not-stomp-on-explicit-values-test
(
testing
"The :timestamped property should not stomp on :created_at/:updated_at if they are explicitly specified"
(
mt/with-temp
Field
[
field
]
(
testing
"Nothing specified: use now() for both"
(
is
(
schema=
{
:created_at
java.time.temporal.Temporal
:updated_at
java.time.temporal.Temporal
s/Keyword
s/Any
}
field
))))
(
let
[
t
#
t
"2022-10-13T19:21:00Z"
t-schema
(
s/eq
(
case
(
mdb.connection/db-type
)
;; not sure why this is TIMESTAMP WITH TIME ZONE for Postgres but not for H2/MySQL. :shrug:
:postgres
(
t/offset-date-time
"2022-10-13T19:21:00Z"
)
(
:h2
:mysql
)
(
t/local-date-time
"2022-10-13T19:21:00"
)))]
(
testing
"Explicitly specify :created_at"
(
mt/with-temp
Field
[
field
{
:created_at
t
}]
(
is
(
schema=
{
:created_at
t-schema
:updated_at
java.time.temporal.Temporal
s/Keyword
s/Any
}
field
))))
(
testing
"Explicitly specify :updated_at"
(
mt/with-temp
Field
[
field
{
:updated_at
t
}]
(
is
(
schema=
{
:created_at
java.time.temporal.Temporal
:updated_at
t-schema
s/Keyword
s/Any
}
field
)))))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment