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
299a0105
Commit
299a0105
authored
10 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
reorganize sync.clj a bit
parent
6a8d5968
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
+50
-29
50 additions, 29 deletions
src/metabase/driver/generic_sql/sync.clj
with
50 additions
and
29 deletions
src/metabase/driver/generic_sql/sync.clj
+
50
−
29
View file @
299a0105
...
...
@@ -8,6 +8,52 @@
(
metabase.models
[
field
:refer
[
Field
]]
[
table
:refer
[
Table
]])))
(
declare
sync-fields
table-names
update-table-row-count
)
;; # PUBLIC FUNCTIONS
(
defn
sync-database
[{
:keys
[
id
]
:as
database
}]
(
with-jdbc-metadata
database
; with-jdbc-metadata reuses *jdbc-metadata* in any call to it inside the fn passed to it
(
fn
[
_
]
; by wrapping the entire sync operation in this we can reuse the same connection throughout
(
->>
(
table-names
database
)
(
pmap
(
fn
[
table-name
]
(
binding
[
*entity-overrides*
{
:transforms
[
#
(
assoc
%
:db
(
delay
database
))]}]
; add a korma transform to Table that will assoc :db on results.
(
let
[
table
(
or
(
sel
:one
Table
:db_id
id
:name
table-name
)
; Table's post-select only sets :db if it's not already set.
(
ins
Table
; This way, we can reuse a single `database` instead of creating
:db_id
id
; a few dozen duplicate instances of it.
:name
table-name
; We can re-use one korma connection pool instead of
:active
true
))]
; creating dozens of them, which was causing issues with too
(
update-table-row-count
table
)
; many open connections.
(
sync-fields
table
)
(
log/debug
"Synced"
table-name
)))))
dorun
))))
(
defn
sync-table
[{
:keys
[
db
]
:as
table
}]
(
with-jdbc-metadata
@
db
(
fn
[
_
]
(
update-table-row-count
table
)
(
sync-fields
table
)
(
log/debug
"Synced"
(
:name
table
)))))
;; # IMPLEMENTATION
;; ## Variables
(
def
^
:const
^
:private
low-cardinality-threshold
"Fields with less than this many distinct values should automatically be marked with `special_type` `:category`."
40
)
(
def
^
:dynamic
*column->base-type*
"COLUMN->BASE-TYPE should be a map of column types returned by the DB to Field base types."
{})
;; ## Fetch Tables/Columns
(
defn
table-names
"Fetch a list of table names for DATABASE."
...
...
@@ -25,8 +71,9 @@
(
fn
[
md
]
(
->>
(
->
md
(
.getColumns
nil
nil
table-name
nil
)
; ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
jdbc/result-set-seq
)
(
mapv
#
(
select-keys
%
[
:column_name
:type_name
]))))))
(
mapv
#
(
select-keys
%
[
:column_name
:type_name
]))))))
;; ## Table Info
(
defn
get-table-row-count
"Get the number of rows in TABLE."
...
...
@@ -43,9 +90,8 @@
(
let
[
new-count
(
get-table-row-count
table
)]
(
upd
Table
id
:rows
new-count
)))
(
def
^
:dynamic
*column->base-type*
"COLUMN->BASE-TYPE should be a map of column types returned by the DB to Field base types."
{})
;; ## Field Info
(
defn
sync-fields
"Sync `Fields` for TABLE. "
...
...
@@ -59,28 +105,3 @@
(
throw
(
Exception.
(
str
"Column '"
column_name
"' has an unknown type: '"
type_name
"'. Please add the type mapping to corresponding driver (e.g. metabase.driver.postgres.sync)."
)))))))
(
jdbc-columns
db
name
))))
(
defn
sync-database
[{
:keys
[
id
]
:as
database
}]
(
with-jdbc-metadata
database
; with-jdbc-metadata reuses *jdbc-metadata* in any call to it inside the fn passed to it
(
fn
[
_
]
; by wrapping the entire sync operation in this we can reuse the same connection throughout
(
->>
(
table-names
database
)
(
pmap
(
fn
[
table-name
]
(
binding
[
*entity-overrides*
{
:transforms
[
#
(
assoc
%
:db
(
delay
database
))]}]
; add a korma transform to Table that will assoc :db on results.
(
let
[
table
(
or
(
sel
:one
Table
:db_id
id
:name
table-name
)
; Table's post-select only sets :db if it's not already set.
(
ins
Table
; This way, we can reuse a single `database` instead of creating
:db_id
id
; a few dozen duplicate instances of it.
:name
table-name
; We can re-use one korma connection pool instead of
:active
true
))]
; creating dozens of them, which was causing issues with too
(
update-table-row-count
table
)
; many open connections.
(
sync-fields
table
)
(
log/debug
"Synced"
table-name
)))))
dorun
))))
(
defn
sync-table
[{
:keys
[
db
]
:as
table
}]
(
with-jdbc-metadata
@
db
(
fn
[
_
]
(
update-table-row-count
table
)
(
sync-fields
table
)
(
log/debug
"Synced"
(
:name
table
)))))
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