Fork me on GitHub
#duct
<
2020-04-16
>
Michel A.14:04:59

Hi guys, I’d like to use a different db (duct.database/sql) when running my tests (via: lein test, or the repl : dev&gt; (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 !

kelveden14:04:28

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.

kelveden14:04:25

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

kelveden14:04:52

(Bill is a colleague of mine.)

kelveden14:04:33

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

erwinrooijakkers10:04:20

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

erwinrooijakkers10:04:17

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

erwinrooijakkers10:04:41

Instead of writing to postgres you write to h2

erwinrooijakkers10:04:50

I think it is a good practice.

erwinrooijakkers10:04:31

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.13:04:57

hello @U2PGHFU5U and @UJF10JP8A, 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.13:04:29

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

Michel A.13:04:49

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.13:04:53

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.13:04:44

thanks again for your help ! 👍

🙂 4