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
de2261b5
Commit
de2261b5
authored
10 years ago
by
Cam Saül
Browse files
Options
Downloads
Plain Diff
Merge pull request #355 from metabase/additional_try_catch
extra try/catch blocks for Sync DB
parents
db8e5a80
890e7bce
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/driver/generic_sql/sync.clj
+36
-27
36 additions, 27 deletions
src/metabase/driver/generic_sql/sync.clj
with
36 additions
and
27 deletions
src/metabase/driver/generic_sql/sync.clj
+
36
−
27
View file @
de2261b5
...
...
@@ -91,38 +91,44 @@
"Create new Fields for any that don't exist; mark ones that no longer exist as `inactive`."
{
:arglists
'
([
korma-table
table
])}
[
korma-table
{
table-id
:id,
table-name
:name,
db
:db
}]
(
let
[
fields
(
jdbc-columns
db
table-name
)
field-names
(
set
(
map
:column_name
fields
))
field-name->id
(
sel
:many
:field->id
[
Field
:name
]
:table_id
table-id
:name
[
in
field-names
])]
;; Mark any existing `Field` objects not returned by jdbc-columns as inactive
(
dorun
(
map
(
fn
[[
field-name
field-id
]]
(
when-not
(
contains?
field-names
field-name
)
(
upd
Field
field-id
:active
false
)))
field-name->id
))
;; Create `Field` objects for any new Fields returned by jdbc-columns
(
dorun
(
map
(
fn
[{
field-name
:column_name
type-name
:type_name
}]
(
when-not
(
field-name->id
field-name
)
(
ins
Field
:table_id
table-id
:name
field-name
:base_type
(
or
(
*column->base-type*
(
keyword
type-name
))
(
throw
(
Exception.
(
str
"Column '"
field-name
"' has an unknown type: '"
type-name
"'. Please add the type mapping to corresponding driver (e.g. metabase.driver.postgres.sync)."
)))))))
fields
))))
(
try
(
let
[
fields
(
jdbc-columns
db
table-name
)
field-names
(
set
(
map
:column_name
fields
))
field-name->id
(
sel
:many
:field->id
[
Field
:name
]
:table_id
table-id
:name
[
in
field-names
])]
;; Mark any existing `Field` objects not returned by jdbc-columns as inactive
(
dorun
(
map
(
fn
[[
field-name
field-id
]]
(
when-not
(
contains?
field-names
field-name
)
(
upd
Field
field-id
:active
false
)))
field-name->id
))
;; Create `Field` objects for any new Fields returned by jdbc-columns
(
dorun
(
map
(
fn
[{
field-name
:column_name
type-name
:type_name
}]
(
when-not
(
field-name->id
field-name
)
(
ins
Field
:table_id
table-id
:name
field-name
:base_type
(
or
(
*column->base-type*
(
keyword
type-name
))
(
throw
(
Exception.
(
str
"Column '"
field-name
"' has an unknown type: '"
type-name
"'. Please add the type mapping to corresponding driver (e.g. metabase.driver.postgres.sync)."
)))))))
fields
)))
(
catch
Throwable
e
(
log/error
"Caught exception in sync-fields-create:"
e
))))
(
defn-
sync-fields-metadata
"Sync the metadata of all active fields for TABLE (in parallel)."
{
:arglists
'
([
korma-table
table
])}
[
korma-table
{
table-id
:id
table-name
:name
}]
(
->>
(
sel
:many
Field
:table_id
table-id
:active
true
)
(
pmap
(
fn
[
field
]
(
try
(
check-for-low-cardinality
korma-table
field
)
(
check-for-large-average-length
korma-table
field
)
(
check-for-urls
korma-table
field
)
(
catch
Throwable
e
(
log/warn
(
format
"Caught exception when syncing field '%s.%s':"
table-name
(
:name
field
))
e
)))))
dorun
))
(
try
(
->>
(
sel
:many
Field
:table_id
table-id
:active
true
)
(
pmap
(
fn
[
field
]
(
try
(
check-for-urls
korma-table
field
)
(
check-for-low-cardinality
korma-table
field
)
(
check-for-large-average-length
korma-table
field
)
(
catch
Throwable
e
(
log/warn
(
format
"Caught exception when syncing field '%s.%s':"
table-name
(
:name
field
))
e
)))))
dorun
)
(
catch
Throwable
e
(
log/error
"Caught exception in sync-fields-metadata:"
e
))))
;; ## Metadata -- Fetch Tables/Columns/PKs/FKs from DB
...
...
@@ -331,6 +337,9 @@
"Fields that have at least this percent of values that are valid URLs should be marked as `special_type = :url`."
0.95
)
;; TODO - this fails for postgres tables that we consider TextFields but don't work for char_length, such as UUID fields
;; This is not a big deal since we wouldn't want to mark those as URLs any way, but we should do casting here to avoid
;; that issue in the first place
(
defn-
field-percent-urls
"Return the percentage of non-null values of FIELD that are valid URLS."
{
:arglists
'
([
korma-table
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