Skip to content

Commit

Permalink
silent mode for joplin outputs, see #60
Browse files Browse the repository at this point in the history
  • Loading branch information
martintrojer committed Aug 19, 2015
1 parent bf0e6c1 commit ce9c474
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
47 changes: 32 additions & 15 deletions joplin.core/src/joplin/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ The first argument must be the name of the migration to create"
;; ==========================================================================
;; Helpers

(def ^:dynamic *silent* nil)

(defmacro silently [& args]
`(binding [*silent* true]
~@args))

(defn println' [& args]
(when-not *silent*
(apply println args)))

(defn get-full-migrator-id
"Get a string with current date and time prepended"
[id]
Expand All @@ -56,13 +66,13 @@ The first argument must be the name of the migration to create"
res
(throw (Exception.))))
(catch Exception e
(printf "Function '%s' not found\n" var-name))))
(println' (format "Function '%s' not found" var-name)))))

(defn get-fn [var]
"Resolves a var"
(if (and var (bound? var))
(deref var)
(printf "Var '%s' couldn't be de-reffed, probably a compiler error\n" var)))
(println' (format "Var '%s' couldn't be de-reffed, probably a compiler error" var))))

(defn drop-first-part [path delimiter]
(->> (string/split path #"/")
Expand Down Expand Up @@ -126,7 +136,7 @@ or resource folders inside a jar on the classpath"
[path]
(let [migration-namespaces (get-migration-namespaces path)]
(when (empty? migration-namespaces)
(println "No migrators found"))
(println' "No migrators found"))
(for [[id ns] migration-namespaces]
(do
(require ns)
Expand All @@ -147,46 +157,53 @@ or resource folders inside a jar on the classpath"
" but " (first conflicts) " was applied."))))
(sort not-applied)))

(defn- joplin-reporter [op id]
(case op
:up (println' "Applying" id)
:down (println' "Rolling back" id)))

(defn do-migrate
"Perform migration on a database"
[migrations db & [opts]]
(println "Migrating" db)
(println' "Migrating" db)
(ragtime.repl/migrate (merge {:datastore db
:migrations migrations}
:migrations migrations
:reporter joplin-reporter}
opts)))

(defn do-rollback
"Perform rollback on a database"
[migrations db amount-or-id & [opts]]
(println "Rolling back" db)
(println' "Rolling back" db)
(ragtime.repl/rollback (merge {:datastore db
:migrations migrations}
:migrations migrations
:reporter joplin-reporter}
opts)
amount-or-id))

(defn do-seed-fn
"Run a seeder function with migration check"
[migrations db target & args]
(println "Seeding" db)
(println' "Seeding" db)
(when-let [seed-fn (and (:seed target) (get-fn (load-var (:seed target))))]
(let [pending-migrations (get-pending-migrations db migrations)]

(cond
(not-empty pending-migrations)
(do
(printf "There are %d pending migration(s)\n" (count pending-migrations))
(println pending-migrations))
(println' (format "There are %d pending migration(s)" (count pending-migrations)))
(println' pending-migrations))

seed-fn
(do
(printf "Applying seed function %s\n" seed-fn)
(println' (format "Applying seed function %s" seed-fn))
(apply seed-fn target args))

:else
(printf "Skipping %s\n" (:seed target))))))
(println' (format "Skipping %s" (:seed target)))))))

(defn do-pending-migrations [db migrations]
(println "Pending migrations" (get-pending-migrations db migrations)))
(println' "Pending migrations" (get-pending-migrations db migrations)))

(defn do-create-migration
"Create a scaffold migrator file"
Expand All @@ -197,7 +214,7 @@ or resource folders inside a jar on the classpath"
path (str (:migrator target) "/"
(string/replace migration-id "-" "_")
".clj")]
(println "creating" path)
(println' "creating" path)
(try
(spit path (format "(ns %s
(:use [%s]))
Expand All @@ -214,4 +231,4 @@ or resource folders inside a jar on the classpath"
(-> (:migrator target) (string/split #"/") rest)
[ns-name]))) ns))
(catch Exception e
(println "Error creating file %s" path))))))
(println' "Error creating file %s" path))))))
14 changes: 7 additions & 7 deletions joplin.core/src/joplin/repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[joplin.core :refer [migrate-db rollback-db seed-db pending-migrations
create-migration]])
create-migration println']])
(:import [java.io PushbackReader]))

(def ^:const libs
Expand Down Expand Up @@ -52,19 +52,19 @@
(require-joplin-ns conf)
(if-let [targets (get-targets conf env (first args))]
(run-op migrate-db targets (rest args))
(println "No targets found")))
(println' "No targets found")))

(defn seed [conf env & args]
(require-joplin-ns conf)
(if-let [targets (get-targets conf env (first args))]
(run-op seed-db targets (rest args))
(println "No targets found")))
(println' "No targets found")))

(defn rollback [conf env database amount-or-id & args]
(require-joplin-ns conf)
(if-let [[target] (get-targets conf env database)]
(apply rollback-db target amount-or-id args)
(println "No targets found")))
(println' "No targets found")))

(defn reset [conf env database & args]
(require-joplin-ns conf)
Expand All @@ -73,16 +73,16 @@
(apply rollback-db target Integer/MAX_VALUE args)
(apply migrate-db target args)
(apply seed-db target args))
(println "No targets found")))
(println' "No targets found")))

(defn pending [conf env database & args]
(require-joplin-ns conf)
(if-let [targets (get-targets conf env database)]
(run-op pending-migrations targets args)
(println "No targets found")))
(println' "No targets found")))

(defn create [conf env database id & args]
(require-joplin-ns conf)
(if-let [[target] (get-targets conf env database)]
(apply create-migration target id args)
(println "No targets found")))
(println' "No targets found")))
4 changes: 2 additions & 2 deletions joplin.elasticsearch/src/joplin/elasticsearch/database.clj
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@
result (esi/update-mapping es-client alias-name (name type) {:mapping new-mappings
:ignore_conflicts ignore-conflicts?})]
(when (:error result)
(println result))))
(println' result))))

(when (not-empty settings-updates)
(let [result (esi/update-settings es-client alias-name settings-updates)]
(when (:error result)
(println result))))))
(println' result))))))

(defn clone-index
"Create a new index with settings and mappings copied from source index,
Expand Down
4 changes: 2 additions & 2 deletions joplin.jdbc/src/joplin/jdbc/database.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
(let [migration-id (get-full-migrator-id id)
path-up (str (:migrator target) "/" migration-id ".up.sql")
path-down (str (:migrator target) "/" migration-id ".down.sql")]
(println "creating" path-up)
(println' "creating" path-up)
(spit path-up "SELECT 1")
(println "creating" path-down)
(println' "creating" path-down)
(spit path-down "SELECT 2")))

;; ============================================================================
Expand Down
6 changes: 3 additions & 3 deletions joplin.zookeeper/src/joplin/zookeeper/database.clj
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@
(defn delete-data [client path]
(if (raw? client)
(delete-data-raw client path)
(println "joplin.zookeeper: no delete implementation for curator connections")))
(println' "joplin.zookeeper: no delete implementation for curator connections")))

;; ==============================================================================

(defmethod seed-db :zk [target & args]
(println "Seeding #joplin.zookeeper.database.ZK" (select-keys (:db target) [:host :port :client]))
(println' "Seeding #joplin.zookeeper.database.ZK" (select-keys (:db target) [:host :port :client]))
(when-let [seed-fn (and (:seed target) (get-fn (load-var (:seed target))))]
(println "Applying seed function" seed-fn)
(println' "Applying seed function" seed-fn)
(apply seed-fn target args)))

;; Dummy fns for migrations, doesn't really make sense for a k/v stores
Expand Down

0 comments on commit ce9c474

Please sign in to comment.