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
3da9a2da
Unverified
Commit
3da9a2da
authored
5 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
Code cleanup
parent
b564a5ad
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/cmd/load_from_h2.clj
+72
-56
72 additions, 56 deletions
src/metabase/cmd/load_from_h2.clj
with
72 additions
and
56 deletions
src/metabase/cmd/load_from_h2.clj
+
72
−
56
View file @
3da9a2da
...
...
@@ -57,11 +57,16 @@
[
table
:refer
[
Table
]]
[
task-history
:refer
[
TaskHistory
]]
[
user
:refer
[
User
]]
[
view-log
:refer
[
ViewLog
]]]))
[
view-log
:refer
[
ViewLog
]]]
[
clojure.set
:as
set
]
[
toucan.db
:as
db
])
(
:import
java.sql.SQLException
))
(
defn-
println-ok
[]
(
println
(
color/green
"[OK]"
)))
;;; -------------------------------------------------- Loading Data --------------------------------------------------
(
defn-
dispatch-on-db-type
[
&
_
]
(
mdb/db-type
))
;;; ------------------------------------------ Models to Migrate (in order) ------------------------------------------
(
def
^
:private
entities
"Entities in the order they should be serialized/deserialized. This is done so we make sure that we load load
...
...
@@ -102,6 +107,9 @@
;; above this line)
DataMigrations
])
;;; --------------------------------------------- H2 Connection Options ----------------------------------------------
(
defn-
add-file-prefix-if-needed
[
connection-string-or-filename
]
(
if
(
str/starts-with?
connection-string-or-filename
"file:"
)
connection-string-or-filename
...
...
@@ -112,31 +120,28 @@
(
mdb/jdbc-details
{
:type
:h2,
:db
(
str
h2-filename
";IFEXISTS=TRUE"
)})))
(
defn-
insert-entity!
[
target-db-conn
entity
objs
]
(
print
(
u/format-color
'blue
"Transfering %d instances of %s..."
(
count
objs
)
(
:name
entity
)))
; TODO - I don't think the print+flush is working as intended :/
;;; ------------------------------------------- Fetching & Inserting Rows --------------------------------------------
(
defn-
insert-entity!
[
target-db-conn
{
:keys
[
table
]
,
:as
entity
}
objs
]
;; TODO - I don't think the print+flush is working as intended :/
(
print
(
u/format-color
'blue
"Transfering %d instances of %s..."
(
count
objs
)
(
:name
entity
)))
(
flush
)
(
let
[
ks
(
keys
(
first
objs
))
;; 1) `:sizeX` and `:sizeY` come out of H2 as `:sizex` and `:sizey` because of automatic lowercasing; fix the
;; names of these before putting into the new DB
;; 2) Need to wrap the column names in quotes because Postgres automatically lowercases unquoted identifiers
quote-char
(
case
(
config/config-kw
:mb-db-type
)
:postgres
\"
:mysql
\`
)
cols
(
for
[
k
ks
]
(
str
quote-char
(
name
(
case
k
:sizex
:sizeX
:sizey
:sizeY
k
))
quote-char
))]
;; 1) `:sizeX` and `:sizeY` come out of H2 as `:sizex` and `:sizey` because of automatic lowercasing; fix the
;; names of these before putting into the new DB
;; 2) Need to wrap the column names in quotes because Postgres automatically lowercases unquoted identifiers
(
let
[
ks
(
keys
(
set/rename-keys
(
first
objs
)
{
:sizex
:sizeX,
:sizey
:sizeY
}))
cols
(
for
[
k
ks
]
((
db/quote-fn
)
(
name
k
)))]
;; The connection closes prematurely on occasion when we're inserting thousands of rows at once. Break into
;; smaller chunks so connection stays alive
(
doseq
[
chunk
(
partition-all
300
objs
)]
(
doseq
[
chunk
(
partition-all
300
objs
)
:let
[
vals
(
for
[
row
chunk
]
(
map
row
ks
))]]
(
print
(
color/blue
\.
))
(
flush
)
(
try
(
jdbc/insert-multi!
target-db-conn
(
:table
entity
)
cols
(
for
[
row
chunk
]
(
map
row
ks
)))
(
catch
java.sql.SQLException
e
(
jdbc/insert-multi!
target-db-conn
table
cols
vals
)
(
catch
SQLException
e
(
jdbc/print-sql-exception-chain
e
)
(
throw
e
)))))
(
println-ok
))
...
...
@@ -153,7 +158,16 @@
;;; ---------------------------------------- Enabling / Disabling Constraints ----------------------------------------
(
defn-
disable-db-constraints
:postgres!
[
target-db-conn
]
(
defmulti
^
:private
disable-db-constraints!
{
:arglists
'
([
target-db-conn
])}
dispatch-on-db-type
)
(
defmulti
^
:private
reenable-db-constraints!
{
:arglists
'
([
target-db-conn
])}
dispatch-on-db-type
)
(
defmethod
disable-db-constraints!
:postgres
[
target-db-conn
]
;; make all of our FK constraints deferrable. This only works on Postgres 9.4+ (December 2014)! (There's no pressing
;; reason to turn these back on at the conclusion of this script. It makes things more complicated since it doesn't
;; work if done inside the same transaction.)
...
...
@@ -166,49 +180,41 @@
;; now enable constraint deferring for the duration of the transaction
(
jdbc/execute!
target-db-conn
[
"SET CONSTRAINTS ALL DEFERRED"
]))
(
defmethod
reenable-db-constraints!
:postgres
[
_
])
; no-op
(
def
n-
disable-db-constraints
:mysql
!
[
target-db-conn
]
(
def
method
disable-db-constraints
!
:mysql
[
target-db-conn
]
(
jdbc/execute!
target-db-conn
[
"SET FOREIGN_KEY_CHECKS=0"
]))
;; For MySQL we need to re
ë
nable FK checks when we're done
(
def
n-
re
ë
nable-db-constraints
:mysql
!
[
target-db-conn
]
;; For MySQL we need to re
-e
nable FK checks when we're done
(
def
method
re
e
nable-db-constraints
!
:mysql
[
target-db-conn
]
(
jdbc/execute!
target-db-conn
[
"SET FOREIGN_KEY_CHECKS=1"
]))
(
defn-
disable-db-constraints!
[
target-db-conn
]
(
println
(
u/format-color
'blue
"Temporarily disabling DB constraints..."
))
((
case
(
mdb/db-type
)
:postgres
disable-db-constraints
:postgres!
:mysql
disable-db-constraints
:mysql!
)
target-db-conn
)
(
println-ok
))
(
defn-
re
ë
nable-db-constraints-if-needed!
[
target-db-conn
]
(
when
(
=
(
mdb/db-type
)
:mysql
)
(
println
(
u/format-color
'blue
"Reënabling DB constraints..."
))
(
re
ë
nable-db-constraints
:mysql!
target-db-conn
)
(
println-ok
)))
;;; ---------------------------------------- Fixing Postgres Sequence Values -----------------------------------------
;;; --------------------------------------------- Fixing Sequence Values ---------------------------------------------
(
def
^
:private
entities-without-autoinc-ids
"Entities that do NOT use an auto incrementing ID column."
#
{
Setting
Session
DataMigrations
})
(
defn-
set-postgres-sequence-values-if-needed!
"When loading data into a Postgres DB, update the sequence nextvals."
[]
(
when
(
=
(
mdb/db-type
)
:postgres
)
(
jdbc/with-db-transaction
[
target-db-conn
(
mdb/jdbc-details
)]
(
println
(
u/format-color
'blue
"Setting postgres sequence ids to proper values..."
))
(
doseq
[
e
entities
:when
(
not
(
contains?
entities-without-autoinc-ids
e
))
:let
[
table-name
(
name
(
:table
e
))
seq-name
(
str
table-name
"_id_seq"
)
sql
(
format
"SELECT setval('%s', COALESCE((SELECT MAX(id) FROM %s), 1), true) as val"
seq-name
(
name
table-name
))]]
(
jdbc/db-query-with-resultset
target-db-conn
[
sql
]
:val
))
(
println-ok
))))
(
defmulti
^
:private
update-sequence-values!
{
:arglists
'
([])}
dispatch-on-db-type
)
(
defmethod
update-sequence-values!
:mysql
[])
; no-op
;; Update the sequence nextvals.
(
defmethod
update-sequence-values!
:postgres
[]
(
jdbc/with-db-transaction
[
target-db-conn
(
mdb/jdbc-details
)]
(
println
(
u/format-color
'blue
"Setting postgres sequence ids to proper values..."
))
(
doseq
[
e
entities
:when
(
not
(
contains?
entities-without-autoinc-ids
e
))
:let
[
table-name
(
name
(
:table
e
))
seq-name
(
str
table-name
"_id_seq"
)
sql
(
format
"SELECT setval('%s', COALESCE((SELECT MAX(id) FROM %s), 1), true) as val"
seq-name
(
name
table-name
))]]
(
jdbc/db-query-with-resultset
target-db-conn
[
sql
]
:val
))
(
println-ok
)))
;;; --------------------------------------------------- Public Fns ---------------------------------------------------
...
...
@@ -220,10 +226,20 @@
Defaults to using `@metabase.db/db-file` as the connection string."
[
h2-connection-string-or-nil
]
(
mdb/setup-db!
)
(
jdbc/with-db-transaction
[
target-db-conn
(
mdb/jdbc-details
)]
(
jdbc/db-set-rollback-only!
target-db-conn
)
(
println
(
u/format-color
'blue
"Temporarily disabling DB constraints..."
))
(
disable-db-constraints!
target-db-conn
)
(
println-ok
)
(
load-data!
target-db-conn
h2-connection-string-or-nil
)
(
re
ë
nable-db-constraints-if-needed!
(
mdb/jdbc-details
))
(
println
(
u/format-color
'blue
"Re-enabling DB constraints..."
))
(
reenable-db-constraints!
target-db-conn
)
(
println-ok
)
(
jdbc/db-unset-rollback-only!
target-db-conn
))
(
set-postgres-sequence-values-if-needed!
))
(
update-sequence-values!
))
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