Handling test and dev mnesia databases in Elixir

I’m learning Elixir and need some mentoring :). Here is my question :

I have a simple app that uses mnesia (not Amnesia for now). When I start mnesia I do:

:mnesia.create_schema([node()])
:mnesia.create_table(Table, [attributes: [:id, :name]])
:mnesia.start

This create a Mnesia.nonode@nohost directory. What I wan’t is too have at least 2 kinds of databases, one for tests data (Mix.env == :test) and one for development data (Mix.env == :dev).

I plan to erase the test database everytime my application stop and create it everytime it starts (using the start and stop callbacks.

The data directory created by mnesia takes the name of the Erlang VM, that is why I guess I need to name my VMs according to the environment (:dev, :test or :prod).

Is this doable ? Is this the right way ?

1 Like

Can be done by adding this in the config.exs file:

config :mnesia,
dir: ‘apps/pki/mnesia/#{Mix.env}/Mnesia.#{node()}’ # use single quotes here !!!

2 Likes

config.exs is evaluated at compile time, so node will return the node name mix runs on during compilation. I’m not really sure if this is really the behaviour you want to have :wink:

1 Like