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
0858df4e
Commit
0858df4e
authored
9 years ago
by
Allen Gilliland
Browse files
Options
Downloads
Patches
Plain Diff
final unit tests for sync functions along with a couple fixes.
parent
fa767385
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/sync_database/sync.clj
+21
-16
21 additions, 16 deletions
src/metabase/sync_database/sync.clj
test/metabase/sync_database/sync_test.clj
+82
-8
82 additions, 8 deletions
test/metabase/sync_database/sync_test.clj
with
103 additions
and
24 deletions
src/metabase/sync_database/sync.clj
+
21
−
16
View file @
0858df4e
...
...
@@ -117,24 +117,29 @@
(
defn
update-data-models-for-table!
"Update the working `Table` and `Field` metadata for a given `Table` based on the latest raw schema information.
This function uses the data in `RawTable` and `RawColumn` to update the working data models as needed."
[{
raw-table-id
:raw_table_id,
:as
existing-table
}]
[{
raw-table-id
:raw_table_id,
table-id
:id,
:as
existing-table
}]
(
when-let
[{
database-id
:database_id,
:as
raw-tbl
}
(
db/sel
:one
raw-table/RawTable
:id
raw-table-id
)]
(
try
(
->
(
table/update-table
existing-table
raw-tbl
)
save-table-fields!
)
;; handle setting any fk relationships
(
when-let
[
table-fks
(
k/select
raw-column/RawColumn
(
k/fields
[
:id
:source-column
]
[
:fk_target_column_id
:target-column
])
;; NOTE: something really wrong happening with SQLKorma here which requires us
;; to be explicit about :metabase_table.raw_table_id in the join condition
;; without this it seems to want to join against metabase_field !?
(
k/join
raw-table/RawTable
(
=
:raw_table.id
:raw_column.raw_table_id
))
(
k/where
{
:raw_table.database_id
database-id
:raw_table.id
raw-table-id
})
(
k/where
(
not=
:raw_column.fk_target_column_id
nil
)))]
(
save-fks!
table-fks
))
(
if-not
(
:active
raw-tbl
)
;; looks like the table has been deactivated, so lets retire this Table and its fields
(
table/retire-tables
#
{
table-id
})
;; otherwise update based on the RawTable/RawColumn information
(
do
(
->
(
table/update-table
existing-table
raw-tbl
)
save-table-fields!
)
;; handle setting any fk relationships
(
when-let
[
table-fks
(
k/select
raw-column/RawColumn
(
k/fields
[
:id
:source-column
]
[
:fk_target_column_id
:target-column
])
;; NOTE: something really wrong happening with SQLKorma here which requires us
;; to be explicit about :metabase_table.raw_table_id in the join condition
;; without this it seems to want to join against metabase_field !?
(
k/join
raw-table/RawTable
(
=
:raw_table.id
:raw_column.raw_table_id
))
(
k/where
{
:raw_table.database_id
database-id
:raw_table.id
raw-table-id
})
(
k/where
(
not=
:raw_column.fk_target_column_id
nil
)))]
(
save-fks!
table-fks
))))
(
catch
Throwable
t
(
log/error
(
u/format-color
'red
"Unexpected error syncing table"
)
t
)))))
...
...
This diff is collapsed.
Click to expand it.
test/metabase/sync_database/sync_test.clj
+
82
−
8
View file @
0858df4e
...
...
@@ -11,13 +11,15 @@
[
metabase.models.table
:as
table
]
[
metabase.sync-database.introspect
:as
introspect
]
[
metabase.sync-database.sync
:refer
:all
]
[
metabase.test.util
:as
tu
]
[
metabase.sync-database.sync
:as
sync
]
[
clojure.tools.logging
:as
log
]))
[
metabase.test.util
:as
tu
]))
(
tu/resolve-private-fns
metabase.sync-database.sync
save-fks!
save-table-fields!
)
(
defn-
get-tables
[
database-id
]
(
->>
(
hydrate/hydrate
(
db/sel
:many
table/Table
:db_id
database-id
(
k/order
:id
))
:fields
)
(
mapv
tu/boolean-ids-and-timestamps
)))
;; save-fks!
(
expect
...
...
@@ -297,7 +299,7 @@
(
tu/with-temp*
[
database/Database
[{
database-id
:id,
:as
db
}
{
:engine
:moviedb
}]]
;; setup a couple things we'll use in the test
(
introspect/introspect-database-and-update-raw-tables!
(
moviedb/->MovieDbDriver
)
db
)
(
sync/
update-data-models-from-raw-tables!
db
)
(
update-data-models-from-raw-tables!
db
)
(
let
[
get-tables
#
(
->>
(
hydrate/hydrate
(
db/sel
:many
table/Table
:db_id
database-id
)
:fields
)
(
mapv
tu/boolean-ids-and-timestamps
))]
;; here we go
...
...
@@ -308,12 +310,84 @@
(
k/set-fields
{
:active
false
})
(
k/where
{
:database_id
database-id,
:name
"movies"
}))
;; run our retires function
(
sync/
retire-tables!
db
)
(
retire-tables!
db
)
;; now we should see the table and its fields disabled
(
get-tables
))])))
;; TODO: update-data-models-for-table!
;; update-data-models-for-table!
(
expect
(
let
[
disable-fks
#
(
map
(
fn
[
fld
]
(
if
(
=
:fk
(
:special_type
fld
))
(
assoc
fld
:special_type
nil
:fk_target_field_id
false
)
fld
))
%
)]
[[(
->
(
last
moviedb/moviedb-tables-and-fields
)
(
update
:fields
disable-fks
))]
[(
->
(
last
moviedb/moviedb-tables-and-fields
)
(
update
:fields
disable-fks
))]
[(
->
(
last
moviedb/moviedb-tables-and-fields
)
(
assoc
:active
false
:fields
[]))]])
(
tu/with-temp*
[
database/Database
[{
database-id
:id,
:as
db
}
{
:engine
:moviedb
}]]
(
let
[
driver
(
moviedb/->MovieDbDriver
)]
;; do a quick introspection to add the RawTables to the db
(
introspect/introspect-database-and-update-raw-tables!
driver
db
)
;; stub out the Table we are going to sync for real below
(
let
[
raw-table-id
(
db/sel
:one
:field
[
raw-table/RawTable
:id
]
:database_id
database-id,
:name
"roles"
)
tbl
(
db/ins
table/Table
:db_id
database-id
:raw_table_id
raw-table-id
:name
"roles"
:active
true
)]
[
;; now lets run a sync and check what we got
(
do
(
update-data-models-for-table!
tbl
)
(
get-tables
database-id
))
;; run the sync a second time to see how we respond to repeat syncing (should be same since nothing changed)
(
do
(
update-data-models-for-table!
tbl
)
(
get-tables
database-id
))
;; one more time, but lets disable the table this time and ensure that's handled properly
(
do
(
k/update
raw-table/RawTable
(
k/set-fields
{
:active
false
})
(
k/where
{
:database_id
database-id,
:name
"roles"
}))
(
update-data-models-for-table!
tbl
)
(
get-tables
database-id
))]))))
;; update-data-models-from-raw-tables!
(
expect
[
moviedb/moviedb-raw-tables
moviedb/moviedb-tables-and-fields
moviedb/moviedb-tables-and-fields
(
conj
(
vec
(
drop-last
moviedb/moviedb-tables-and-fields
))
(
->
(
last
moviedb/moviedb-tables-and-fields
)
(
assoc
:active
false
:fields
[])))]
(
tu/with-temp*
[
database/Database
[{
database-id
:id,
:as
db
}
{
:engine
:moviedb
}]]
(
let
[
driver
(
moviedb/->MovieDbDriver
)]
;; do a quick introspection to add the RawTables to the db
(
introspect/introspect-database-and-update-raw-tables!
driver
db
)
;; TODO: update-data-models-from-raw-tables!
;; make sure to test case where FK relationship tables are out of order
[
;; first check that the raw tables stack up as expected
(
->>
(
hydrate/hydrate
(
db/sel
:many
raw-table/RawTable
:database_id
database-id
(
k/order
:id
))
:columns
)
(
mapv
tu/boolean-ids-and-timestamps
))
;; now lets run a sync and check what we got
(
do
(
update-data-models-from-raw-tables!
db
)
(
get-tables
database-id
))
;; run the sync a second time to see how we respond to repeat syncing (should be same since nothing changed)
(
do
(
update-data-models-from-raw-tables!
db
)
(
get-tables
database-id
))
;; one more time, but lets disable a table this time and ensure that's handled properly
(
do
(
k/update
raw-table/RawTable
(
k/set-fields
{
:active
false
})
(
k/where
{
:database_id
database-id,
:name
"roles"
}))
(
update-data-models-from-raw-tables!
db
)
(
get-tables
database-id
))])))
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