Shakarim

Shakarim

How to create and test with mnesia?

Hello everyone, I’m building an app using mnesia as storage, and I got a some questions:

  1. How to correctly test modules which using the mnesia?
    As example: I have an app with 3 genservers which contain callbacks for CRUD under specific supervisor. Every single genserver works with specific table. Im adding the tests for this 3 genserver, testing scenarios and the setup block (into every script file of course) with :mnesia.clear_table(:my_table) inside.

    • If Im trying to call this scripts one by one - everything is ok. Troubles begins when I’m calling all tests at once. As I understand, :mnesia works asyncroniously, and tables do not have time to change their state between two or more testing scenarios. As result: sometimes testing scripts works correctly, sometimes not.

    • Sometimes i got message like disk_log: repairing '__FILE__'. All appearance, I see this message because :mnesia are not stopped correctly after ExUnit complete (I think it is perceived like a sudden shutting down for mnesia, working ends off and formed the difference between data in file on disk and data in RAM).

      What can I do, for fix this message reason? Maybe exist some way for stop the :mnesia correctly when all test scenarios is complete?

    How do I clean/drop/change state properly the mnesia table(s)/schema(s) for making a tests wich gonna work stable and predictably?

  2. How to correctly create mnesia schema & start the mnesia in app?
    Basically, I have a 2 way, start it manually or add mnesia into extra_applications in mix.exs. I choosed first option. I’ve choosed this way, because if I add mnesia into extra_applications - I will have to stop it before calling :mnesia.create_schema (I’m creating the schema & starting mnesia inside init/1 of supervisor).

    Is it good way or not? I don’t know why, but I feel that it’s - anti-pattern.

    Is this practice if correct? Creating the schema inside supervisor’s init/1. Or I should apart the creating schema/tables from supervisor logic? If yes - how can I create schema/tables correctly in application deployment?

    If you have some advices, examples or something else, which can help me with this questions - I would be appreciative.

Most Liked

rogerweb

rogerweb

I won’t be able to address all your questions because my experience is with RAM-only tables, but I can share what works for me anyway in case it helps.

All Mnesia-related code are isolated in it’s own module (it’s called Store in my case). It exposes an init/1 function that creates the tables and a stop/0 function that is only used by the tests, which deletes the tables and stops Mnesia. Since all the tables are RAM-only, I don’t use create_schema.

In each test file, I have something like this:

  setup_all do
    Store.init()
    on_exit(&Store.stop/0)
    :ok
  end

If you are relying on the order of the tests in each test file, you can add the following line to your test_helper.exs file to make sure the tests will run in the given order:

ExUnit.configure(seed: 0)

So each test file starts with an empty database, then I use the functions provided by my Store module to insert, query, update and delete data and test the results.

For production, I have a GenServer that calls that init/1 function in its own init/1 and handles events from Mnesia like node up and node down, but that’s all. The users of the Store module call its functions directly from their processes and not by sending messages to a GenServer.

Finally, I do have :mnesia in my extra_applications.

Hope it helps,

Regards

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement