Skip to content

Instantly share code, notes, and snippets.

@realgenekim
Last active February 16, 2021 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realgenekim/3fb9d2dccde9234743ec392b600055be to your computer and use it in GitHub Desktop.
Save realgenekim/3fb9d2dccde9234743ec392b600055be to your computer and use it in GitHub Desktop.
Example of getting UTF-8 working with next.jdbc with MySQL, to enable Unicode and emojis, along with using using "REPLACE" instead of "INSERT" to enable upserts.
(ns db.db2
(:require
[next.jdbc :as j]
[next.jdbc.sql :as jsql]
[next.jdbc.sql.builder :as jb]
[clojure.spec.alpha :as s]))
(def mysql-db {:dbtype "mysql"
:dbname "twitter"
:host "127.0.0.1"
:user "xxx"
:password "xxx"
:useUnicode "yes"
:characterEncoding "UTF-8"})
(def ds (j/get-datasource mysql-db))
(defn insert-tweets-with-conn!
[conn tweets]
(let [ks [:id :tweet :hash256]
rows (map #((apply juxt ks) %) tweets)
;_ (println rows)
q (jb/for-insert-multi :tweets ks rows {})
;_ (println q)
qr (clojure.string/replace (first q) #"^INSERT" "REPLACE")
;_ (println qr)
qnew (assoc-in q [0] qr)
_ (println qnew)]
(j/execute! conn qnew)))
(def insert-tweets! (partial insert-tweets-with-conn! ds))
(commment
(insert-tweets! [{:id 1235 :tweet "abc🎉" :hash256 "abcxxx"}])
)
@realgenekim
Copy link
Author

@seancorfield also wrote — I think this is what every dev (and DBA) should know about modern realities of strings and their storage in databases

(we have a couple of columns still on utf8 in our biggest tables because running the ALTER TABLE will lock the table long enough to cause disruption to our users -- at some point we'll probably tackle that during a scheduled maintenance window... but we very rarely have scheduled maintenance any more now that our processes are nearly all Clojure apps running in a (small) cluster!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment