duct-framework / duct

Server-side application framework for Clojure
MIT License
1.13k stars 51 forks source link

Unsure how to run migrations on application startup #103

Closed paulspencerwilliams closed 5 years ago

paulspencerwilliams commented 5 years ago

I'm trying to configure a duct project to run migrations on startup. My understanding is that you just have to include :duct/migrator into main.clj but reviewing the logs, this isn’t working. The migration works find using (dev) (go) in the repl, and `lein

(ns film-ratings.main
  (:gen-class)
  (:require [duct.core :as duct]))

(duct/load-hierarchy)

(defn -main [& args]
  (let [keys     (or (duct/parse-keys args) [:duct/migrator :duct/daemon])
        profiles [:duct.profile/prod]]
    (-> (duct/resource "film_ratings/config.edn")
        (duct/read-config)
        (duct/exec-config profiles keys))))

Output from running this app via docker-compose shows app startup, without an attempt to migrate.

will@puter film-ratings % docker-compose up -d && docker-compose logs -f 
Creating network "film-ratings_default" with the default driver
Creating film-ratings_postgres_1 ... done
Creating film-ratings_filmapp_1  ... done
Attaching to film-ratings_filmapp_1, film-ratings_postgres_1
postgres_1  | 2019-11-20 20:24:53.348 UTC [1] LOG:  starting PostgreSQL 12.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.3.0) 8.3.0, 64-bit
postgres_1  | 2019-11-20 20:24:53.348 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2019-11-20 20:24:53.348 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2019-11-20 20:24:53.353 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2019-11-20 20:24:53.450 UTC [18] LOG:  database system was shut down at 2019-11-20 20:23:44 UTC
postgres_1  | 2019-11-20 20:24:53.481 UTC [1] LOG:  database system is ready to accept connections
filmapp_1   | 19-11-20 20:25:05 07de9dcec793 REPORT [duct.server.http.jetty:13] - :duct.server.http.jetty/starting-server {:port 3000}
filmapp_1   | 19-11-20 20:29:24 07de9dcec793 INFO [duct.middleware.web:16] - :duct.middleware.web/request {:request-method :get, :uri "/assets/normalize.css/normalize.css", :query-string nil}
filmapp_1   | 19-11-20 20:29:24 07de9dcec793 INFO [duct.middleware.web:16] - :duct.middleware.web/request {:request-method :get, :uri "/list-films", :query-string nil}
filmapp_1   | 19-11-20 20:29:24 07de9dcec793 INFO [duct.middleware.web:16] - :duct.middleware.web/request {:request-method :get, :uri "/css/site.css", :query-string nil}
postgres_1  | 2019-11-20 20:29:25.391 UTC [25] ERROR:  relation "film" does not exist at character 15

Config.edn:

{:duct.profile/base
                      {:duct.core/project-ns                  film-ratings

                       :duct.router/ataraxy
                                                              {:routes {[:get "/"]           [:film-ratings.handler/index]
                                                                        "/add-film"          {:get                             [:film-ratings.handler.film/show-create]
                                                                                              [:post {film-form :form-params}] [:film-ratings.handler.film/create film-form]}
                                                                        [:get "/list-films"] [:film-ratings.handler.film/list]}}

                       :film-ratings.handler/index            {}
                       :film-ratings.handler.film/show-create {}
                       :film-ratings.handler.film/create      {:db #ig/ref :duct.database/sql}
                       :film-ratings.handler.film/list        {:db #ig/ref :duct.database/sql}
                       :duct.migrator/ragtime                 {:migrations [#ig/ref :film-ratings.migrations/create-film]}
                       [:duct.migrator.ragtime/sql
                        :film-ratings.migrations/create-film]
                                                              {:up   ["create table film(id serial primary key, name text unique, description text, rating integer)"]
                                                               :down ["drop table film"]}}

 :duct.profile/dev    #duct/include "dev"
 :duct.profile/local  #duct/include "local"
 :duct.profile/prod   {}

 :duct.module/logging {}
 :duct.module.web/site
                      {}
 :duct.module/sql
                      {}}
weavejester commented 5 years ago

What happens if you (prn keys) in your main function?

paulspencerwilliams commented 5 years ago

It outputs nothing. Nothing because I was executing stale Docker images due to a mismatched expectations of Docker image names. Running the app using java -jar /target/app.jar correctly displayed the expected keys, that helped diagnose the problem. Thank you.

filmapp_1   | [:duct/migrator :duct/daemon]