Skip to content

Instantly share code, notes, and snippets.

@maxrothman
Created October 26, 2020 03:58
Show Gist options
  • Save maxrothman/64804518b0d72b56bd4468d5e55bc0c2 to your computer and use it in GitHub Desktop.
Save maxrothman/64804518b0d72b56bd4468d5e55bc0c2 to your computer and use it in GitHub Desktop.
Dump Microsoft SQL database as CSV
(ns help-dad.core
(:require [clojure.java.jdbc :as jdbc]
[clojure.java.io :as io]
[clojure.data.csv :as csv])
(:gen-class))
;; Assumed directory layout:
;; - table-names A text file with one table name per line. You have to produce this manually
;; - dumps/ An empty directory where the CSVs will go
;; - help-dad/ The project, as per the `lein new app` template
;; - project.clj
;; - src/
;; - help_dad
;; - core.clj
(def db-spec {:dbtype "mssql"
:dbname "TESTING"
:user "SA"
:password "<YourStrong@Passw0rd>"})
(defn -main
[& args]
(println "Hello World"))
(defn table->csv [table-name]
(let [rows (jdbc/query
db-spec
[(format "select * from [%s]" table-name)])
columns (keys (first rows))]
(println table-name)
(with-open [writer (io/writer (format "../dumps/%s.csv" table-name))]
(csv/write-csv
writer
(cons
(map name columns)
(map #(map (partial get %) columns) rows))))))
(defn go []
(with-open [r (io/reader "../table-names")]
(dorun (map table->csv (line-seq r)))))
(defproject help-dad "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/java.jdbc "0.7.11"]
[com.microsoft.sqlserver/mssql-jdbc "8.4.1.jre11"]
[org.clojure/data.csv "1.0.0"]]
:main ^:skip-aot help-dad.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment