yogthos / migratus

MIGRATE ALL THE THINGS!
640 stars 95 forks source link

Return new file name from migratus/create instead of nil #238

Closed sirmspencer closed 1 year ago

sirmspencer commented 1 year ago

For code based migrations, other support files are needed which I can auto generate if I know the file name of the new migration file.

yogthos commented 1 year ago

That sounds reasonable, any chance you could do a quick PR for this?

sirmspencer commented 1 year ago

I can't pull and test on my work computer, but this should do it.

(defn create [config name migration-type]
  (let [migration-dir  (find-or-create-migration-dir
                         (utils/get-parent-migration-dir config)
                         (utils/get-migration-dir config))
        migration-name (->kebab-case (str (timestamp) name))]
    (for [mig-file (proto/migration-files* migration-type migration-name)]
      (let [file (io/file migration-dir mig-file)]
        (.createNewFile file)
        (.getName (io/file migration-dir mig-file))))))
yogthos commented 1 year ago

ok cool, I just pushed out 1.5.1 with the fix

sirmspencer commented 1 year ago

Woot, thanks!

sirmspencer commented 1 year ago

Confirmed working.

sirmspencer commented 1 year ago

If anyone else sees this, here is code that generates clj files per migration that match (except the NS can't start with a number so I prefixed with c for my use)

(defn new-consul-migration [title]
  (let [new-fname (-> (mig/create (migratus-config db/db) title :edn)
                      (first))
        new-ns    (str/replace new-fname #".edn" "")]
    (spit (str "resources/migrations/" new-fname)
          (tus/->pprint-str {:ns (symbol (str "c-" new-ns))
                             :up-fn 'migrate-up
                             :down-fn 'migrate-down
                             :transaction? true}))
    (spit (str "migrations/consul_migrations/c_"
               (str/replace new-ns #"-" "_")
               ".clj")
          (format "(ns c-%s)\n\n(defn migrate-up []\n)\n\n(defn migrate-down []\n)" new-ns))))