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
fb79343b
Unverified
Commit
fb79343b
authored
1 year ago
by
Tim Macdonald
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Handle BOMs in uploaded CSVs (#30392)
parent
d67f22d1
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
deps.edn
+1
-0
1 addition, 0 deletions
deps.edn
src/metabase/upload.clj
+3
-2
3 additions, 2 deletions
src/metabase/upload.clj
test/metabase/upload_test.clj
+27
-1
27 additions, 1 deletion
test/metabase/upload_test.clj
with
31 additions
and
3 deletions
deps.edn
+
1
−
0
View file @
fb79343b
...
...
@@ -16,6 +16,7 @@
buddy/buddy-sign
{
:mvn/version
"3.4.333"
}
; JSON Web Tokens; High-Level message signing library
camel-snake-kebab/camel-snake-kebab
{
:mvn/version
"0.4.3"
}
; util functions for converting between camel, snake, and kebob case
cheshire/cheshire
{
:mvn/version
"5.11.0"
}
; fast JSON encoding (used by Ring JSON middleware)
clj-bom/clj-bom
{
:mvn/version
"0.1.2"
}
; handle BOMs in imported CSVs
clj-commons/iapetos
{
:mvn/version
"0.1.13"
}
; prometheus metrics
clj-http/clj-http
{
:mvn/version
"3.12.3"
; HTTP client
:exclusions
[
commons-codec/commons-codec
...
...
This diff is collapsed.
Click to expand it.
src/metabase/upload.clj
+
3
−
2
View file @
fb79343b
(
ns
metabase.upload
(
:require
[
clj-bom.core
:as
bom
]
[
clojure.data.csv
:as
csv
]
[
clojure.java.io
:as
io
]
[
clojure.set
:as
set
]
...
...
@@ -194,7 +195,7 @@
(
str
truncated-name-without-time
(
t/format
time-format
(
t/local-date-time
)))))
(
def
max-sample-rows
"Maximum number of values to use for detecting a column's type"
1000
)
(
def
^
:private
max-sample-rows
"Maximum number of values to use for detecting a column's type"
1000
)
(
defn-
sample-rows
"Returns an improper subset of the rows no longer than [[max-sample-rows]]. Takes an evenly-distributed sample (not
...
...
@@ -216,7 +217,7 @@
A column that is completely blank is assumed to be of type ::text."
[
csv-file
]
(
with-open
[
reader
(
io/
reader
csv-file
)]
(
with-open
[
reader
(
bom/bom-
reader
csv-file
)]
(
let
[[
header
&
rows
]
(
csv/read-csv
reader
)]
(
rows->schema
header
(
sample-rows
rows
)))))
...
...
This diff is collapsed.
Click to expand it.
test/metabase/upload_test.clj
+
27
−
1
View file @
fb79343b
(
ns
metabase.upload-test
(
:require
[
clj-bom.core
:as
bom
]
[
clojure.java.io
:as
io
]
[
clojure.string
:as
str
]
[
clojure.test
:refer
:all
]
[
metabase.driver
:as
driver
]
...
...
@@ -86,10 +88,13 @@
([
rows
]
(
csv-file-with
rows
"test"
))
([
rows
filename
]
(
csv-file-with
rows
filename
io/writer
))
([
rows
filename
writer-fn
]
(
let
[
contents
(
str/join
"\n"
rows
)
csv-file
(
doto
(
File/createTempFile
filename
".csv"
)
(
.deleteOnExit
))]
(
spit
csv-file
contents
)
(
with-open
[
^
java.io.Writer
w
(
writer-fn
csv-file
)]
(
.write
w
contents
))
csv-file
)))
(
deftest
detect-schema-test
...
...
@@ -402,3 +407,24 @@
(
testing
"Check that the table isn't created if the upload fails"
(
sync/sync-database!
(
mt/db
))
(
is
(
nil?
(
t2/select-one
Table
:db_id
(
mt/id
))))))))
(
deftest
load-from-csv-BOM-test
(
testing
"Upload a CSV file with a byte-order mark (BOM)"
(
mt/test-drivers
(
mt/normal-drivers-with-feature
:uploads
)
(
mt/with-empty-db
(
upload/load-from-csv
driver/*driver*
(
mt/id
)
"upload_test"
(
csv-file-with
[
"id,ship,captain"
"1,Serenity,Malcolm Reynolds"
"2,Millennium Falcon, Han Solo"
]
"star-wars"
(
partial
bom/bom-writer
"UTF-8"
)))
(
testing
"Table and Fields exist after sync"
(
sync/sync-database!
(
mt/db
))
(
let
[
table
(
t2/select-one
Table
:db_id
(
mt/id
))]
(
is
(
=?
{
:name
#
"(?i)upload_test"
}
table
))
(
testing
"Check the data was uploaded into the table correctly"
(
is
(
=
[
"id"
,
"ship"
,
"captain"
]
(
column-names-for-table
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