pierrefourgeaud

pierrefourgeaud

Ecto version-controlled data and form - what is the recommended practice for this?

I have been working on a platform where we have to manage some kind of documents and particularly the versioning. I have the rest of the platform up and running with most of the stuff done but here come the central piece: The document management.

For the POC I did something simple for testing in the lights of:

  schema "documents" do
    field :title, :string
    field :body, :string
    belongs_to :organisation, MyApp.Organisations.Organisation

    timestamps(type: :utc_datetime)
  end

It works well - we validated our idea and are now going to the next step.

But now comes the part where I will need to manage versions and state of the documents.

I came up with something like this:

  schema "documents" do
    # more fields
    field :draft_version, :integer
    field :published_version, :integer
    belongs_to :organisation, MyApp.Organisations.Organisation

    timestamps(type: :utc_datetime)
  end

  schema "document_versions" do
    field :title, :string
    field :body, :string
    field :version, :integer

    # this has_one doesn't work and I think I will change it to belongs_to
    has_one :author, MyApp.Accounts.User
    belongs_to :document, MyApp.Documents.Document

    timestamps(type: :utc_datetime)
  end

It looks OK I guess. We will not have millions of documents and with index on the version with version/document I guess that will work.

But now comes the time to have the forms. A “new” document will basically create a document and the first version. “Edit” a document will create a new version (loading the content from the previous version) for a document.

I have been struggling with that side. I am using live_view from the frontend and I started to look for embedded forms to get that going.
That’s where I stumbled upon the embedded_schemas… I was wondering if this would be actually a fit for my use-case. I am still relatively new in Elixir/Phoenix/Ecto and learning around that. What would be the recommendation for a case like this?

Most Liked

mayel

mayel

Not really an answer to your question, but have you considered using a library for the versioning, such as GitHub - izelnakri/paper_trail: Track and record all the changes in your database with Ecto. Revert back to anytime in history. · GitHub ?

Last Post!

mayel

mayel

I’m using it quite happily (well a forked repo, since the maintainer seems slow on merges but has been occasionally responsive: a few things by mayel · Pull Request #224 · izelnakri/paper_trail · GitHub )

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement