update ragtime and migration db config. add mysql support

This commit is contained in:
Gered 2016-01-25 17:56:42 -05:00
parent 08f9d9e253
commit 22da47b997
10 changed files with 99 additions and 50 deletions

View file

@ -10,8 +10,13 @@
#{"webapp"
"webservice"
"postgresql"
"mysql"
"couchdb"})
(def meta-options
#{"sql"
"nosql"})
(def default-options
#{"webapp"})
@ -59,10 +64,8 @@
["dev/user.clj" (render "dev/user_webservice.clj" data)]])
(defn get-sql-files [data]
["migrations"])
(defn get-postgresql-files [data]
[["src/{{path}}/db.clj" (render "src/root_ns/db_postgresql.clj" data)]])
["migrations"
["src/{{path}}/db.clj" (render "src/root_ns/db_sql.clj" data)]])
(defn get-couchdb-files [data]
[["src/{{path}}/db.clj" (render "src/root_ns/db_couchdb.clj" data)]])
@ -75,14 +78,18 @@
(if (:webapp data) (get-webapp-files data))
(if (:webservice data) (get-webservice-files data))
(if (:sql data) (get-sql-files data))
(if (:postgresql data) (get-postgresql-files data))
(if (:couchdb data) (get-couchdb-files data)))))
(defn invalid-options? [options]
(or (seq (clojure.set/difference options supported-options))
; TODO: clean this up
(or (seq (clojure.set/difference options (set (concat supported-options meta-options))))
(and (some #{"webapp"} options)
(some #{"webservice"} options))
(and (some #{"mysql"} options)
(some #{"postgresql"} options))
(and (some #{"postgresql"} options)
(some #{"couchdb"} options))
(and (some #{"mysql"} options)
(some #{"couchdb"} options))))
(defn invalid-options-message! [user-options]
@ -90,7 +97,7 @@
(println "Valid options are:" (str/join ", " supported-options))
(println "*** Note that you can only specify at most ONE from each of:")
(println " - 'webapp' and 'webservice'")
(println " - 'postgresql' and 'couchdb'")
(println " - 'postgresql', 'mysql' and 'couchdb'")
(println "*** If neither 'webapp' or 'webservice' is specified, 'webapp' is assumed")
(println "*** Specifying no options will default to use only 'webapp'"))
@ -101,7 +108,8 @@
options))
(defn add-sql-option [options]
(if (some #{"postgresql"} options)
(if (or (some #{"postgresql"} options)
(some #{"mysql"} options))
(conj options "sql")
options))

View file

@ -2,9 +2,28 @@
(:require
weasel.repl.websocket
cemerick.piggieback
{{#sql}}
[ragtime.jdbc :as jdbc]
[ragtime.repl :as repl]
[{{root-ns}}.db :as db]
{{/sql}}
[edn-config.core :refer [env]])
(:use
{{root-ns}}.core))
{{#sql}}
(defn get-ragtime-config []
{:datastore (jdbc/sql-database db/db)
:migrations (jdbc/load-resources "migrations")})
(defn migrate []
(println "Running migrations on" (:subname db/db))
(repl/migrate (get-ragtime-config)))
(defn rollback []
(println "Rolling back migrations on" (:subname db/db))
(repl/rollback (get-ragtime-config)))
{{/sql}}
(defn cljs-repl
"after establishing a normal repl with leiningen (e.g. by running 'lein repl') *and* after

View file

@ -1,8 +1,27 @@
(ns user
(:require
{{#sql}}
[ragtime.jdbc :as jdbc]
[ragtime.repl :as repl]
[{{root-ns}}.db :as db]
{{/sql}}
[edn-config.core :refer [env]])
(:use
{{root-ns}}.core))
{{#sql}}
(defn get-ragtime-config []
{:datastore (jdbc/sql-database db/db)
:migrations (jdbc/load-resources "migrations")})
(defn migrate []
(println "Running migrations on" (:subname db/db))
(repl/migrate (get-ragtime-config)))
(defn rollback []
(println "Rolling back migrations on" (:subname db/db))
(repl/rollback (get-ragtime-config)))
{{/sql}}
(if (env :auto-start-server?)
(.start (Thread. #(start-server))))

View file

@ -2,7 +2,7 @@
{{#sql}}
:db {:host "localhost"
:name "db_name"
:port 5432
:port {{#postgresql}}5432{{/postgresql}}{{#mysql}}3306{{/mysql}}
:username "username"
:password "password"}
{{/sql}}

View file

@ -4,7 +4,7 @@
{{#sql}}
:db {:host "localhost"
:name "db_name"
:port 5432
:port {{#postgresql}}5432{{/postgresql}}{{#mysql}}3306{{/mysql}}
:username "username"
:password "password"}
{{/sql}}

View file

@ -2,7 +2,7 @@
{{#sql}}
:db {:host "localhost"
:name "db_name"
:port 5432
:port {{#postgresql}}5432{{/postgresql}}{{#mysql}}3306{{/mysql}}
:username "username"
:password "password"}
{{/sql}}

View file

@ -26,11 +26,15 @@
{{/webservice}}
{{#sql}}
[org.clojure/java.jdbc "0.4.2"]
[clojurewerkz/ragtime "0.4.0"]
[com.mchange/c3p0 "0.9.5.2"]
[ragtime "0.5.2"]
{{/sql}}
{{#postgresql}}
[org.postgresql/postgresql "9.4-1202-jdbc42"]
{{/postgresql}}
{{#mysql}}
[mysql/mysql-connector-java "5.1.36"]
{{/mysql}}
{{#couchdb}}
[com.ashafa/clutch "0.4.0"]
[com.cemerick/url "0.1.1"]
@ -45,9 +49,6 @@
{{#webapp}}
[lein-cljsbuild "1.1.2"]
{{/webapp}}
{{#sql}}
[clojurewerkz/ragtime.lein "0.4.0"]
{{/sql}}
[lein-pprint "1.1.1"]]
{{#webapp}}
@ -87,16 +88,15 @@
:stacktraces? false
:auto-reload? false}}
:dev {:resource-paths ["env-resources/dev"]
:source-paths ["dev"]
:dependencies [{{#webapp}}[com.cemerick/piggieback "0.2.1"]{{/webapp}}
[pjstadig/humane-test-output "0.7.1"]]
{{#sql}}
:ragtime {:migrations ragtime.sql.files/migrations
:database "jdbc:{{#postgresql}}postgresql{{/postgresql}}://localhost:5432/db_name?user=username&password=password"}
{{/sql}}
:injections [(require 'pjstadig.humane-test-output)
(pjstadig.humane-test-output/activate!)]}
:repl {:resource-paths ["env-resources/repl"]
:source-paths ["dev"]}}
:aliases {"uberjar" ["do" ["clean"] ["uberjar"]]{{#webapp}}
"cljsdev" ["do" ["cljsbuild" "once"] ["cljsbuild" "auto"]]{{/webapp}}})
"cljsdev" ["do" ["cljsbuild" "once"] ["cljsbuild" "auto"]]{{/webapp}}{{#sql}}
"migrate" ["run" "-m" "user/migrate"]
"rollback" ["run" "-m" "user/rollback"]{{/sql}}})

View file

@ -40,8 +40,8 @@
{{#sql}}
(try
(db/init!)
(info "Database access initialized.")
(db/verify-connection)
(info "Database connection verified.")
(catch Exception ex
(throw (Exception. "Database not available or bad connection information specified." ex))))
{{/sql}}

View file

@ -1,28 +0,0 @@
(ns {{root-ns}}.db
(:import
(org.postgresql.ds PGPoolingDataSource))
(:require
[clojure.java.jdbc :as sql]
[edn-config.core :refer [env]]))
(defonce db (atom nil))
(defn init!
"should be called once when the app starts up. intializes a PGPoolingDataSource
object so the rest of your app can execute database queries."
[]
(reset!
db
(let [db-config (env :db)]
{:datasource
(doto (new PGPoolingDataSource)
(.setServerName (:host db-config))
(.setDatabaseName (:name db-config))
(.setPortNumber (:port db-config))
(.setUser (:username db-config))
(.setPassword (:password db-config)))}))
; kind of a cheap way to verify the connection
; (executes a dummy query that will raise exceptions if the connection info is bad)
(sql/query @db "select 1"))
;; TODO: add database functions here

View file

@ -0,0 +1,31 @@
(ns {{root-ns}}.db
(:require
[clojure.java.jdbc :as sql]
[edn-config.core :refer [env]]))
(def db-config (env :db))
{{#postgresql}}
(def db
{:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname (str "//" (:host db-config) ":" (or (:port db-config) 5432) "/" (:name db-config))
:user (:username db-config)
:password (:password db-config)})
{{/postgresql}}
{{#mysql}}
(def db
{:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname (str "//" (:host db-config) ":" (or (:port db-config) 3306) "/" (:name db-config))
:user (:username db-config)
:password (:password db-config)})
{{/mysql}}
(defn verify-connection
"not really required, but can be used at app startup to verify that the database
configuration is correct. will throw an exception if the database is unreachable."
[]
(sql/query db "select 1"))
;; TODO: add database functions here