Problems with compile time macro caching

Hey, I have really weird problem. I’m working on something which have task to create mnesia database and would require to call use few times. At compilation time (in __using__/1 macro) I’m saving some things into database. If database is not yet created I’m skipping database-specific work and simply use IO.warn/1.

The problem is that when (after previous compilation) I’m creating database and then I’m running mix compile --force then it looks like that macro is called, but there are no entries in database.

When I update macro by simply adding some IO.inspect or IO.puts (for debugging purposes) then after compilation it suddenly works, so it looks like that some piece of code is cached and not running on compile time even if I force project compilation. Of course calling task before final project compilation solves the problem, but again I can’t drop mnesia database and force recompile project.

Is there an option to fully recompile project or at least only every module which uses specific module?

Dear gods, it is absolutely terrible. Before going anywhere from there, let me just ask: WHY?

1 Like

No matter if mnesia or other solution I need to save some data on compile time. It could look weird, but it’s my private project assumption. I don’t want to describe everything now. In short I want to store some data and later use them dynamically.

For simplicity imagine that every time you call use Ecto.Repo module (as __MODULE__ in __using__/1 is saved as key in repositories table, so it could be later used to dynamically fetch all project repositories without setting them in config :my_app, :ecto_repos, […] manually. Of course my case is much more bigger than single key mnesia table - it’s just to show an example of storing some things on compile time which could be fetched dynamically.

To be honest it’s also not even a big priority for me. I just found that mistake like removing mnesia directory manually or by mix task causes problem, because I can’t recompile and re-fill database.

Have you tried replacing your compile-time inserting in a database with a mix task? That could help you with testing it as well.

@dimitarvp: Oh, I’m really sorry for delay. I was a bit busy last time. :slight_smile:

Even before you wrote this I have already found a solution. I made a stupid mistake with check if database is working. After updating code compilation does not skip important code which saves my information to mnesia database.

Answering your question. In my specific case I can’t moved code to mix task. In mix task I have only creating database. Inserting data to tables happens at compile time. Of course I could do it with some example data, but even doing that I would not copy copy which checks if database is working (because in this task I’m already creating it and ensuring its started), so I would not even reproduce problem. :smiley:

Anyway thank you all for help. I though that some code is seriously not called again using force argument. Fortunately I was wrong. :077: