-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to revert back to anytime in history? How to merge versions? #15
Comments
Hi @jesseshieh, Yes PaperTrail can revert back to anytime in history because the data is already there. If you want to merge versions you should pay attention to your version meta_tags, ideally you wouldn't want to lose them. Our focus has been on building the foundation(the initial data storage) so far. Currently there isn't a single function for these that ships with the library. All features are heavily tested before release. Also I'm not reverting or merging versions in any of my applications currently. Therefore If you are willing to contribute by adding these functionalities I'll be happily review and collaborate. Thanks for your interest in the project! |
This is one way of doing it: def travel(post, time) do
post
|> PaperTrail.get_versions()
|> Enum.sort_by(& &1.id)
|> Enum.take_while(& &1.inserted < ^time)
|> Enum.map(& &1.item_changes)
|> Enum.reduce(%{}, &Map.merge(&2, &1))
end The result is a map with the attributes post had at a given time. This could be further piped into def travel(post, time) do
attrs =
post
|> PaperTrail.get_versions()
|> Enum.sort_by(& &1.id)
|> Enum.take_while(& &1.inserted < ^time)
|> Enum.map(& &1.item_changes)
|> Enum.reduce(%{}, &Map.merge(&2, &1))
%Post{}
|> Ecto.Changeset.cast(attrs, [Map.keys(attrs)])
|> Ecto.Changeset.apply_changes()
end |
Is there a story for tracking how the shape of a model might change over its lifetime? For example, if a field name is changed, or a new (required) field is added. Or a more complex example, a column is extracted to a new table, and replaced with a reference? In these cases, it seems difficult to revert back to an old version. Are there any examples of this? |
Yes, I thought of implementing something like this @tomconroy, then thought it should be left to the developer during their migration instead. Because checking such changes during each migration with PaperTrail would take too much time and work from PaperTrail side. |
Thanks @izelnakri, so do you imagine the developer would migrate the historical versions, rather than implement the different ways a historical version might be restored to the latest schema? Seems like either path could have many complications |
For the benefit of others; something I've been doing is stuffing a serialized representation of an object into the "meta" field on every update. That way, I've got the struct at a given point-in-time and don't have to reconstitute it. This is a naive approach, and I like it because its simple. I cheat a little by updating the record in the Versions table. Something like:
|
I'm considering to cut a v1 release after new years. So far I think it would be great if we describe this in the README.md, however if someone wants to create a PR for it, I'm willing to review it. |
@izelnakri What aspect of this would you like to see described? If its the naive approach I posted above, I'd be happy to whip up a PR. For the other approach of building a representation from history, I'm less stoked as any missing rows from the |
I think naive approach could add to the complexity in terms of maintainance, however I dont mind having a |
Hi, The documentation mentions that paper_trail can "revert back to anytime in history", but I can't figure out exactly how to revert or reconstitute an older version of a model. Is there documentation somewhere that describes this?
Also, it's mentioned that you "don't delete your paper_trail versions, instead you can merge them", but I can't figure out exactly how to merge them. Is there documentation somewhere that describes this?
The text was updated successfully, but these errors were encountered: