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
f0323abe
Unverified
Commit
f0323abe
authored
8 months ago
by
Chris Truter
Committed by
GitHub
8 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Decouple query analysis models from validation API (#46002)
parent
7369c056
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
enterprise/backend/src/metabase_enterprise/query_reference_validation/api.clj
+41
-72
41 additions, 72 deletions
...rc/metabase_enterprise/query_reference_validation/api.clj
src/metabase/models/query_field.clj
+46
-0
46 additions, 0 deletions
src/metabase/models/query_field.clj
with
87 additions
and
72 deletions
enterprise/backend/src/metabase_enterprise/query_reference_validation/api.clj
+
41
−
72
View file @
f0323abe
...
...
@@ -4,6 +4,7 @@
[
medley.core
:as
m
]
[
metabase.api.common
:as
api
]
[
metabase.api.routes.common
:refer
[
+auth
]]
[
metabase.models.query-field
:as
query-field
]
[
metabase.server.middleware.offset-paging
:as
mw.offset-paging
]
[
toucan2.core
:as
t2
]))
...
...
@@ -17,85 +18,53 @@
(
def
^
:private
default-sort-direction
"asc"
)
(
def
^
:private
card-joins
[[(
t2/table-name
:model/QueryField
)
:qf
]
[
:and
[
:=
:qf.card_id
:c.id
]
[
:=
:qf.explicit_reference
true
]]
[(
t2/table-name
:model/Field
)
:f
]
[
:and
[
:=
:qf.field_id
:f.id
]
[
:=
:f.active
false
]]])
(
defn-
inactive-fields
[
cards
]
(
when
(
seq
cards
)
(
t2/select
:model/QueryField
{
:select
[
:qf.card_id
[
:f.name
:field
]
[
:t.name
:table
]
[
:t.active
:table_active
]]
:from
[[(
t2/table-name
:model/QueryField
)
:qf
]]
:join
[[(
t2/table-name
:model/Field
)
:f
]
[
:=
:f.id
:qf.field_id
]
[(
t2/table-name
:model/Table
)
:t
]
[
:=
:t.id
:f.table_id
]]
:where
[
:and
[
:=
:qf.explicit_reference
true
]
[
:=
:f.active
false
]
[
:in
:card_id
(
map
:id
cards
)]]})))
(
defn-
inactive-errors
[
inactive-fields
]
(
mapv
(
fn
[{
:keys
[
field
table
table_active
]}]
{
:type
(
if
table_active
:inactive-field
:inactive-table
)
:table
table
:field
field
})
inactive-fields
))
(
defn-
cards-with-inactive-fields
[
sort-column
sort-direction
offset
limit
]
(
let
[
sort-dir-kw
(
keyword
sort-direction
)
additional
-joins
(
case
sort-column
"name"
[]
"collection"
[[(
t2/table-name
:model/Collection
)
:coll
]
[
:=
:coll.id
:c.collection_id
]]
"created_by"
[[(
t2/table-name
:model/User
)
:u
]
[
:=
:u.id
:c.creator_id
]]
"last_edited_at"
[])
extra
-selects
(
case
sort-column
"name"
[
:c.name
]
"collection"
[[[
:max
:coll.name
]]
;; ^^ All these `max`es are silly, but they're necessary since we
;; group by card
[[
:not=
nil
[
:max
:coll.name
]]
:is_child_collection
]]
"created_by"
[
:u.first_name
:u.last_name
:u.email
]
"last_edited_at"
[
:c.updated_at
])
order-by-c
olumn
(
condp
=
sort-column
"collection"
[[
:is_child_collection
sort-dir-kw
]
[[
:max
:coll.name
]
sort-dir-kw
]]
"created_by"
[[[
:coalesce
[
:||
:u.first_name
" "
:u.last_name
]
:u.first_name
:u.last_name
:u.email
]
sort-dir-kw
]]
[(
into
extra
-selects
[
sort-dir-kw
])])
card-query
(
m/assoc-some
{
:select
(
into
[
:c.*
]
extra-selects
)
:from
[[(
t2/table-name
:model/Card
)
:c
]]
:join
card-joins
:left-join
additional
-joins
:where
[
:=
:c.archived
false
]
:order-by
order-by-c
olumn
:group-by
:c.id
}
:limit
limit
:offset
offset
)
cards
(
t2/select
:model/Card
card-query
)
id->
inactive-fields
(
group-by
:card_id
(
inactive-field
s
cards
)
)
add-errors
(
fn
[{
:keys
[
id
]
:as
card
}]
(
assoc
card
:errors
(
i
nactive-
errors
(
id
->inactive-fields
id
)
)))]
(
let
[
sort-dir-kw
(
keyword
sort-direction
)
sorting
-joins
(
case
sort-column
"name"
[]
"collection"
[[(
t2/table-name
:model/Collection
)
:coll
]
[
:=
:coll.id
:c.collection_id
]]
"created_by"
[[(
t2/table-name
:model/User
)
:u
]
[
:=
:u.id
:c.creator_id
]]
"last_edited_at"
[])
sorting
-selects
(
case
sort-column
"name"
[
:c.name
]
"collection"
[[[
:max
:coll.name
]]
;; ^^ All these `max`es are silly, but they're necessary since we
;; group by card
[[
:not=
nil
[
:max
:coll.name
]]
:is_child_collection
]]
"created_by"
[
:u.first_name
:u.last_name
:u.email
]
"last_edited_at"
[
:c.updated_at
])
order-by-c
lause
(
condp
=
sort-column
"collection"
[[
:is_child_collection
sort-dir-kw
]
[[
:max
:coll.name
]
sort-dir-kw
]]
"created_by"
[[[
:coalesce
[
:||
:u.first_name
" "
:u.last_name
]
:u.first_name
:u.last_name
:u.email
]
sort-dir-kw
]]
[(
into
sorting
-selects
[
sort-dir-kw
])])
card-query
(
query-field/cards-with-reference-errors
(
m/assoc-some
{
:select
(
into
[
:c.*
]
sorting-selects
)
:from
[[(
t2/table-name
:model/Card
)
:c
]]
:left-join
sorting
-joins
:where
[
:=
:c.archived
false
]
:order-by
order-by-c
lause
:group-by
:c.id
}
:limit
limit
:offset
offset
)
)
cards
(
t2/select
:model/Card
card-query
)
id->
errors
(
query-field/reference-error
s
cards
)
add-errors
(
fn
[{
:keys
[
id
]
:as
card
}]
(
assoc
card
:errors
(
i
d->
errors
id
)))]
(
map
add-errors
(
t2/hydrate
cards
:collection
:creator
))))
(
defn-
invalid-card-count
[]
(
:count
(
t2/query-one
{
:select
[[[
:count
[
:distinct
:c.id
]]
:count
]]
:from
[[(
t2/table-name
:model/Card
)
:c
]]
:join
card-joins
:where
[
:=
:c.archived
false
]})))
(
t2/query-one
(
query-field/cards-with-reference-errors
{
:select
[[[
:count
[
:distinct
:c.id
]]
:count
]]
:from
[[(
t2/table-name
:model/Card
)
:c
]]
:where
[
:=
:c.archived
false
]}))))
(
api/defendpoint
GET
"/invalid-cards"
"List of cards that have an invalid reference in their query. Shape of each card is standard, with the addition of an
...
...
This diff is collapsed.
Click to expand it.
src/metabase/models/query_field.clj
+
46
−
0
View file @
f0323abe
...
...
@@ -9,6 +9,52 @@
(
doto
:model/QueryField
(
derive
:metabase/model
))
(
def
^
:private
reference-error-joins
[[(
t2/table-name
:model/QueryField
)
:qf
]
[
:and
[
:=
:qf.card_id
:c.id
]
[
:=
:qf.explicit_reference
true
]]
[(
t2/table-name
:model/Field
)
:f
]
[
:and
[
:=
:qf.field_id
:f.id
]
[
:=
:f.active
false
]]])
(
defn
cards-with-reference-errors
"Given some HoneySQL query map with :model/Card bound as :c, restrict this query to only return cards
with invalid references.
For now this only handles inactive references, but in future it will also handle unknown references too."
[
card-query-map
]
;; NOTE: We anticipate a schema change to support missing references that will result in left-joins and
;; where clauses being appended as well.
(
update
card-query-map
:join
concat
reference-error-joins
))
(
defn
reference-errors
"Given a seq of cards, return a map of card-id => reference errors"
[
cards
]
(
when
(
seq
cards
)
(
->>
(
t2/select
:model/QueryField
{
:select
[
:qf.card_id
[
:f.name
:field
]
[
:t.name
:table
]
[
:t.active
:table_active
]]
:from
[[(
t2/table-name
:model/QueryField
)
:qf
]]
:join
[[(
t2/table-name
:model/Field
)
:f
]
[
:=
:f.id
:qf.field_id
]
[(
t2/table-name
:model/Table
)
:t
]
[
:=
:t.id
:f.table_id
]]
:where
[
:and
[
:=
:qf.explicit_reference
true
]
[
:=
:f.active
false
]
[
:in
:card_id
(
map
:id
cards
)]]})
(
map
(
fn
[{
:keys
[
card_id
table
field
table_active
]}]
[
card_id
{
:type
(
if
table_active
:inactive-field
:inactive-table
)
:table
table
:field
field
}]))
(
reduce
(
fn
[
acc
[
id
error
]]
(
update
acc
id
u/conjv
error
))
{}))))
;;; Updating QueryField from card
(
defn
update-query-fields-for-card!
...
...
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