duct

Michel A. 2020-04-16T14:05:59.030700Z

Hi guys, I’d like to use a different db (duct.database/sql) when running my tests (via: lein test, or the repl : dev> (test) ) vs. the one I use in development and in production. The idea is to have a h2 in memory db (vs a postgres db in dev/prod) where the same migrations would be run (+ some lines inserted for the tests). Is this a good parctice? How would you achieve this ? Is creating a new lein profile the good way ? thx !

erwinrooijakkers 2020-04-17T10:52:41.031800Z

erwinrooijakkers 2020-04-17T10:53:20.032400Z

Asked a similar question, a boundary (defprotocol) we defined ourselves, but idea is the same I think

erwinrooijakkers 2020-04-17T10:54:17.032600Z

So create a test profile I guess with the specific config for test

erwinrooijakkers 2020-04-17T10:54:22.032800Z

duct profile

erwinrooijakkers 2020-04-17T10:54:41.033Z

Instead of writing to postgres you write to h2

erwinrooijakkers 2020-04-17T10:56:50.033200Z

I think it is a good practice.

erwinrooijakkers 2020-04-17T10:59:31.033400Z

You could also setup the environment with a local postgres instance (docker image) and then run integration tests against that. Then mark the tests with (deftest ^:integration and add test-selectors to project.clj

:test-selectors {:unit (complement :integration)
                 :integration :integration}
so you can run unit tests with lein test :unit and integration tests with lein test :integration

Michel A. 2020-04-19T13:09:57.043400Z

hello @erwinrooijakkers and @kelveden, thank you very much for your replies that helped me solve my issue. What I did is that : 1. I realized that in [duct/core “0.8.0”] a specific duct.profile/test was introduced (I was using 0.7.0), and upgraded to 0.8.0. This helped me avoid the creation of test lein profile 2. In my config.edn added a line (below the dev profile one) :

Michel A. 2020-04-19T13:10:29.043600Z

:duct.profile/test #duct/include "test.edn"

Michel A. 2020-04-19T13:12:49.043800Z

there is a small issue that caused me a crash in duct.core and this is why I had to introduce the “.edn” extension (https://github.com/duct-framework/core/issues/23)

Michel A. 2020-04-19T13:17:53.044100Z

and then : 3. added in duct_hierarchy.edn the code:

{:my-project.migration/h2 [:my-project.migration/sql]
 :my-project.migration/postgres [:my-project.migration/sql]}
4. referenced :my-project.migration/sql in the config.edn file :
:duct.migrator/ragtime
  {:migrations [#ig/ref :co.ytems.aeolus.migration/sql]}
5. defined the :my-project.migration/h2 in test.edn file and :my-project.migration/postgres in both dev.edn and prod profiles

Michel A. 2020-04-19T13:19:44.044400Z

thanks again for your help ! 👍

🙂 1
kelveden 2020-04-16T14:27:28.030800Z

You could define a :duct.database.sql/hikaricp value in your dev.edn which also included a key initialising your H2 db (`:dev.db/H2` or whatever). You'd need to make sure that the H2 db component initialised first though to make sure that the connection could be made - this could be achieved by just adding a reference to :dev.db/H2 to the hikaricp key.

kelveden 2020-04-16T14:28:25.031Z

We actually do something very similar to this except that we have postgres in memory instead of H2. We use this: https://github.com/Bigsy/pg-embedded-clj

kelveden 2020-04-16T14:28:52.031300Z

(Bill is a colleague of mine.)

kelveden 2020-04-16T14:32:33.031500Z

I'm not sure how easy it would be to add in extra ragtime migrations in a separate profile though.