I’m working to deploy a Phoenix application where I would really like to use :mnesia
for some data. I’m using Memento right now, though that’s not a hard dependency.
I was wondering if anyone had a public example of an application that uses Mnesia/Memento in fly with a small number of nodes. How are you starting it, creating tables, and joining/re-joining nodes to your cluster?
Right now I have two machines for this application, with min_machines_running = 1
. I have created a volume with a count of 2, mounted it in my machines, and configured :mnesia, :dir
to point to that volume.
I find that with my current setup, with 1-2 nodes running on localhost, I can start and stop nodes and data is retained. I do see that on the first node, my table is included in :local_tables
, whereas on my second node it is in :tables
but not in :local_tables
.
I find that in my fly deployment, every deployment kills all my data.
Right now I’m setting up my tables as follows. I’m not attached to any of this, so if there’s something else that actually works I’m happy to throw any or all of it away.
@tables [Schema.MyTable]
@impl GenServer
def init(_opts \\ []) do
Logger.info("[#{inspect(__MODULE__)}] init")
disc_nodes = [node() | Node.list()]
setup!(disc_nodes)
Memento.wait(@tables, :infinity)
Memento.add_nodes(Node.list() -- Memento.system()[:running_db_nodes])
{:ok, nil}
end
def setup!(disc_nodes \\ [node()]) do
local_disc = node() in disc_nodes
if local_disc, do: File.mkdir_p!(Application.get_env(:mnesia, :dir))
if local_disc, do: Memento.Schema.set_storage_type(node(), :disc_copies)
for table <- @tables -- Memento.system()[:local_tables],
do: Memento.Table.create(table)
for table <- @tables do
if local_disc, do: Memento.Table.set_storage_type(table, node(), :disc_copies)
end
end