 (defn migrate [& args]
   (doseq [plugin (or args (plugins))]
     (log/info "running migrations for plugin" (name plugin))
-    (migratus/migrate (migratus-config (dev-ds) plugin)))
+    (migratus/migrate (migratus-config (dev-config) plugin)))
   (System/exit 0))
 
 (defn rollback [plugin]
-  (when-let [id (last (applied-migrations (dev-ds) plugin))]
+  (when-let [id (last (applied-migrations (dev-config) plugin))]
     (log/info "rolling back latest migration for plugin" (name plugin))
-    (run-down dev-ds plugin [id]))
+    (run-down (dev-config) plugin [id]))
   (System/exit 0))
 
 (defn remove-plugin [plugin]
   (log/info "rolling back all migrations for plugin" (name plugin))
-  (let [applied (reverse (applied-migrations (dev-ds) plugin))]
-    (run-down (dev-ds) plugin applied))
+  (let [applied (reverse (applied-migrations (dev-config) plugin))]
+    (run-down (dev-config) plugin applied))
   (System/exit 0))
 
 (defn seed [& args]
   (doseq [plugin (or args (plugins))]
-    (run-seed (dev-ds) plugin))
+    (run-seed (dev-config) plugin))
   (System/exit 0))
 
 (defn reset [& args]
   (doseq [plugin (or args (plugins))]
-    (run-reset (dev-ds) plugin))
+    (run-reset (dev-config) plugin))
   (System/exit 0))

    (io/copy (io/file source-path) (io/file dest-path))))


(defn seed-copy [plugin]
  (when-let [data-dir (io/resource (str (seed-dir plugin) "/public"))]
    (doseq [source-file (-> data-dir io/as-file file-seq rest)]
      (copy-file source-file (str "resources/public/" (public-path source-file))))))


(defn migratus-config [ds plugin]
  {:store                :database
   :db                   (jdbc-uri ds)
   :migration-dir        (migration-dir plugin)
   :migration-table-name schema-table})


(defn applied-migrations
  ([ds]
   (try
     (map :id (jdbc/query (jdbc-uri ds) [(str "SELECT id from " schema-table " ORDER BY id ASC")]))
     (catch Exception e)))

  ([ds plugin]
   (let [applied (applied-migrations ds)
        available (->> (migration-dir plugin) find-migrations keys (map str) set)]
    (filter available (map str applied)))))


(defn run-down [ds plugin applied]
  (doseq [id applied]
    (migratus/down (migratus-config ds plugin) (parse-migration-id id))))


(defn run-seed [ds plugin]
  (when-let [data-file (io/resource (str (seed-dir plugin) "/data.edn"))]
    (log/info "running seed functions for plugin" (name plugin))
    (seed-insert ds data-file)
    (seed-copy plugin)))


(defn run-reset [ds plugin]
  (let [applied (reverse (applied-migrations ds plugin))]
    (do
      (log/info "running reset functions for plugin" (name plugin))
      (run-down ds plugin applied)
      (migratus/migrate (migratus-config ds plugin))
      (run-seed ds plugin))))


(defn run-test-reset []
  (doseq [plugin (plugins)]
    (run-reset (test-ds) plugin)))


(defn migrate-all [config-path]
  (let [config (read-config config-path)]
    (doseq [plugin (:plugins config)]
      (migratus/migrate (migratus-config (:datasource config) plugin)))))

;;;



(defn plugins [] (cons :core (:plugins (dev-config))))


(defn jdbc-uri [conf]
  (let [source (:datasource conf)]
    (str "jdbc:" (:adapter source "mysql") "://"
         (:server-name source "localhost")  "/" (:database-name source)
         "?user=" (:username source)
         (if (not-empty (:password source)) (str "&password=" (:password source))))))


(def schema-table "schema_migrations")
