smpallen99

smpallen99

Whatwasit - Track the changes to your Ecto models

Just released a new package on hex for tracking changes to your Ecto models.

Its functional, but in the early stages. Could use some additional features like restore, version limits, etc. Let me know what you think.

Most Liked

OvermindDL1

OvermindDL1

Huh, was actually thinking of making something like this myself. A question though, I notice that it seems to require a User schema of some form to mark whodoneit, however in my system the User is in an LDAP system (not exposed via Ecto since Ecto2 has no LDAP interface, as well as it would not work cross-repo anyway), and the unique ID from the LDAP database is a bigint integer in Ecto. How would I give it the bigint unique ID that does not have a corresponding ecto schema?

smpallen99

smpallen99

It does not need a user model if you don’t want to track who made the change. Please add an issue if want to see support for an LDAP based user model.
To suport your use case, I think we can add an option to the installer for this. Something that would set the user_id to a simple field with a provided type. For the user name, we could add support for a {module, fun} tuple and call the fun if its a tuple.

What do you think?
Steve

OvermindDL1

OvermindDL1

Just as a comparison, my original idea for a module like this was less generic, I was planning an interface to use PostgreSQL’s natural ability to mark rows as ‘deleted/invalid’ and they no longer appear after in SQL queries (unless you use a specific one to get those specifically deleted ones), thus you can version that way. Could store something like a user on the table itself for example. I am unsure which method is better actually…

OvermindDL1

OvermindDL1

There are a few ways. PostgreSQL used to have actual Time Travel functionality built in a long time ago but was removed for ANSI compliance over time and is now emulatable via: https://www.postgresql.org/docs/9.5/static/contrib-spi.html#AEN174541

Honestly that is overkill though, and database specific (which much of my code is anyway), and I am using such a row history as it is on one of my tables. On one of my tables I need to keep all history, and yet it is dynamic. I could have made a logging table but that is extra work so all I did was add a column of (in addition to the other primary keys):

field :invalidated_at, :datetime, primary_key: true

On initial write I set it far in the future (specific date of 294275 AD), on every query I add the 294275 AD date to the query via a helper on the schema. On write I update the :invalidated_at of the old row to now() and write out the new row with the new data with the 294275 AD future date. On delete I just set the :invalidated_at to now(). This is not a generic style that would be useful in most situations, not a good style for storing, say, who did it, although with minor modifications to some of the ordering it would be.

That table grew over a few versions to do that, I was thinking of redesigning it (would not need to be a primary key with an idea I have) with a set of helpers that I could pull out into a library to manage it for me, something as simple as just dropping another timestamps-like call into the migration and schema with a set of options it could take along. It would have been postgresql specific due to using its built in time handling functions though.

smpallen99

smpallen99

I just published 0.2.0 on hex. This version has a mix whatwasit.install —whodoneit-map option that will save your user struct in the database as a map. This should work with any struct and not just an Ecto schema.

Steve

Where Next?

Popular in Libraries Top

OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
blatyo
https://www.conduitframework.com/ The best overview for how things are tied together is this presentation. Modules and functions are pre...
New
benlime
I created a new library GitHub - benvp/ex_cva: Class Variance Authority for Elixir which aims to make it very easy to define different va...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
KallDrexx
For a good number of months I've been working on creating a very basic RTMP live video streaming server. Now that I have a very, very ba...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New

Other popular topics Top

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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Sub Categories:

We're in Beta

About us Mission Statement