Including :mnesia in release, without starting it

Initial Problem:

I want to use :mnesia in my application.

If I don’t explicitly include :mnesia in my release, it doesn’t exist.

example:

** (UndefinedFunctionError) function :mnesia.create_schema/1 is undefined
  (module :mnesia is not available)

However, if I add it to :extra_applications so that it gets included in my release, and let it start automatically and create the default schema, I get an error when trying to create tables:

:mnesia.create_table(MyRecord, 
  disc_copies: [node()],
  attributes: [:id, :foo, :bar]
)

with the error:

{:error, {:bad_type, MyRecord, :disc_copies, :nonode@nohost}}

I’m defining the directory in config.exs

config :mnesia, :dir, 'priv/data/mnesia'

And this works fine in dev when I don’t include :mnesia in my :extra_applications and I start it manually.

Solution I want to attempt:

I am trying to include :mnesia in my release, without it starting automatically, so I can start it myself.

I have tried adding it to :included_applications like so:

  def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger, :runtime_tools],
      included_applications: [:mnesia]
    ]
  end

But this gives me an error

** (Mix) Undefined applications: [mnesia]

Anybody have any insight on how I can get this working?

I’ve been trying to get this working for several hours now and really starting to regret choosing mnesia.

Yeah, I wish documentation and error messages were better in Mnesia.

If Mnesia has already started with :extra_applications, you should run this instead of :create_schema/1:

:mnesia.change_table_copy_type(:schema, node(), :disc_copies)

Otherwise if you only load :mnesia with :included_applications, then you should run this:

:mnesia.create_schema([node()])
:mnesia.start()

After that, :mnesia.create_table/2 should work. Remember to also run :mnesia.wait_for_tables/2 to ensure that the node/cluster is ready.

And as I mentioned in the other thread, I believe the error with Mnesia missing from the release is because of a bug in Elixir 1.9.0. 1.9.1 should resolve that.

1 Like