diff --git a/README.md b/README.md index de278707..03f61861 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,98 @@ PaperTrail lets you record every change in your database in a seperate database table called ```versions```. Library generates a new version record with associated data every time you run ```PaperTrail.insert/1```, ```PaperTrail.update/1``` or ```PaperTrail.destroy/1``` functions. Simply these functions wrap your Repo insert, update or destroy actions in a database transaction, so if your database action fails you won't get a new version. -```elixir +## Example +```elixir + changeset = Post.changeset(%Post{}, %{ + title: "Word on the street is Elixir got its own database versioning library", + content: "You should try it now!" + }) + + PaperTrail.insert(changeset) + # => on success: + # {:ok, + # %{model: %Post{__meta__: #Ecto.Schema.Metadata<:loaded, "posts">, + # title: "Word on the street is Elixir got its own database versioning library", + # content: "You should try it now!", id: 1, inserted_at: #Ecto.DateTime<2016-09-15 21:42:38>, + # updated_at: #Ecto.DateTime<2016-09-15 21:42:38>}, + # version: %PaperTrail.Version{__meta__: #Ecto.Schema.Metadata<:loaded, "versions">, + # event: "create", id: 1, inserted_at: #Ecto.DateTime<2016-09-15 21:42:38>, + # item_changes: %{title: "Word on the street is Elixir got its own database versioning library", + # content: "You should try it now!", id: 1, inserted_at: #Ecto.DateTime<2016-09-15 21:42:38>, + # updated_at: #Ecto.DateTime<2016-09-15 21:42:38>}, + # item_id: 1, item_type: "Post", meta: nil}}} + + # => on error: + # {:error, :model, + # Ecto.Changeset, + # valid?: false>, %{}} + + post = Repo.get!(Post, 1) + edit_changeset = Post.changeset(post, %{ + title: "Elixir matures fast", + content: "Future is already here, you deserve to be awesome!" + }) + + PaperTrail.update(edit_changeset) + # => on success: + # {:ok, + # %{model: %Post{__meta__: #Ecto.Schema.Metadata<:loaded, "posts">, + # title: "Elixir matures fast", content: "Future is already here, you deserve to be awesome!", + # id: 1, inserted_at: #Ecto.DateTime<2016-09-15 21:42:38>, + # updated_at: #Ecto.DateTime<2016-09-15 22:00:59>}, + # version: %PaperTrail.Version{__meta__: #Ecto.Schema.Metadata<:loaded, "versions">, + # event: "update", id: 2, inserted_at: #Ecto.DateTime<2016-09-15 22:00:59>, + # item_changes: %{title: "Elixir matures fast", content: "Future is already here, you deserve to be awesome!"}, + # item_id: 1, item_type: "Post", + # meta: nil}}} + + # => on error: + # {:error, :model, + # Ecto.Changeset, + # valid?: false>, %{}} + + PaperTrail.get_version(post) + # %PaperTrail.Version{__meta__: #Ecto.Schema.Metadata<:loaded, "versions">, + # event: "update", id: 2, inserted_at: #Ecto.DateTime<2016-09-15 22:00:59>, + # item_changes: %{title: "Elixir matures fast", content: "Future is already here, you deserve to be awesome!"}, + # item_id: 1, item_type: "Post", meta: nil}}} + + updated_post = Repo.get!(Post, 1) + + PaperTrail.delete(updated_post) + # => on success: + # {:ok, + # %{model: %Testo.Post{__meta__: #Ecto.Schema.Metadata<:deleted, "posts">, + # title: "Elixir matures fast", content: "Future is already here, you deserve to be awesome!", + # id: 1, inserted_at: #Ecto.DateTime<2016-09-15 21:42:38>, + # updated_at: #Ecto.DateTime<2016-09-15 22:00:59>}, + # version: %PaperTrail.Version{__meta__: #Ecto.Schema.Metadata<:loaded, "versions">, + # event: "destroy", id: 3, inserted_at: #Ecto.DateTime<2016-09-15 22:22:12>, + # item_changes: %{title: "Elixir matures fast", content: "Future is already here, you deserve to be awesome!", + # id: 1, inserted_at: #Ecto.DateTime<2016-09-15 21:42:38>, + # updated_at: #Ecto.DateTime<2016-09-15 22:00:59>}, + # item_id: 1, item_type: "Post", meta: nil}}} + + Repo.aggregate(Post, :count, :id) # => 0 + Repo.aggregate(PaperTrail.Version, :count, :id) # => 3 + + last(PaperTrail.Version, :id) |> Repo.one + # %PaperTrail.Version{__meta__: #Ecto.Schema.Metadata<:loaded, "versions">, + # event: "destroy", id: 3, inserted_at: #Ecto.DateTime<2016-09-15 22:22:12>, + # item_changes: %{"title" => "Elixir matures fast", content: "Future is already here, you deserve to be awesome!", "id" => 1, + # "inserted_at" => "2016-09-15T21:42:38", + # "updated_at" => "2016-09-15T22:00:59"}, + # item_id: 1, item_type: "Post", meta: nil} ``` PaperTrail is inspired by the ruby gem ```paper_trail```. However, unlike the ```paper_trail``` gem this library actually results in less data duplication, faster and more explicit programming model to version your record changes. -The library source code is minimal and tested. It is highly suggested that you check it out, there is nothing magical really. +The library source code is minimal and tested. It is highly suggested that you check it out, it isn't rocket science. ## Installation @@ -32,24 +117,28 @@ The library source code is minimal and tested. It is highly suggested that you c Your application is now ready to collect some history! +## How to use paper_trail with phoenix? + +A guide needs to be written for this. Although it isn't that hard to implement for the brave. +One caveat: your application Repo has to be ```Repo``` instead of ```YourApplicationName.Repo``` TODO AREA: +** remove wrong Elixir compiler errors + ** explain the columns ## Storing version meta data give originator example - Your versions don't need a model lifecycle callbacks like before_create or before_update for any extra meta data, all your meta data could be stored in one object and that object could be passed as the second optional parameter to PaperTrail.create ## Suggestions -order matter, +PaperTrail.Version(s) order matter, don't delete your versions merge them - ## Examples PaperTrail.create/1, PaperTrail.update/1, PaperTrail.destroy/1 diff --git a/doc/404.html b/doc/404.html index a3f630cd..b9a5d6a1 100644 --- a/doc/404.html +++ b/doc/404.html @@ -5,7 +5,7 @@ - 404 – paper_trail v0.1.4 + 404 – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

diff --git a/doc/Mix.Tasks.Papertrail.Install.html b/doc/Mix.Tasks.Papertrail.Install.html index 09955c6a..ed22dea0 100644 --- a/doc/Mix.Tasks.Papertrail.Install.html +++ b/doc/Mix.Tasks.Papertrail.Install.html @@ -5,7 +5,7 @@ - Mix.Tasks.Papertrail.Install – paper_trail v0.1.4 + Mix.Tasks.Papertrail.Install – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

@@ -60,7 +60,7 @@

- paper_trail v0.1.4 + paper_trail v0.1.5 Mix.Tasks.Papertrail.Install diff --git a/doc/PaperTrail.Migration.html b/doc/PaperTrail.Migration.html index a9377ed2..3b89875a 100644 --- a/doc/PaperTrail.Migration.html +++ b/doc/PaperTrail.Migration.html @@ -5,7 +5,7 @@ - PaperTrail.Migration – paper_trail v0.1.4 + PaperTrail.Migration – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

@@ -60,7 +60,7 @@

- paper_trail v0.1.4 + paper_trail v0.1.5 PaperTrail.Migration diff --git a/doc/PaperTrail.Version.html b/doc/PaperTrail.Version.html index 057cdcf7..ef5250a4 100644 --- a/doc/PaperTrail.Version.html +++ b/doc/PaperTrail.Version.html @@ -5,7 +5,7 @@ - PaperTrail.Version – paper_trail v0.1.4 + PaperTrail.Version – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

@@ -60,7 +60,7 @@

- paper_trail v0.1.4 + paper_trail v0.1.5 PaperTrail.Version diff --git a/doc/PaperTrail.VersionQueries.html b/doc/PaperTrail.VersionQueries.html index a0523eb7..9f8d0526 100644 --- a/doc/PaperTrail.VersionQueries.html +++ b/doc/PaperTrail.VersionQueries.html @@ -5,7 +5,7 @@ - PaperTrail.VersionQueries – paper_trail v0.1.4 + PaperTrail.VersionQueries – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

@@ -60,7 +60,7 @@

- paper_trail v0.1.4 + paper_trail v0.1.5 PaperTrail.VersionQueries diff --git a/doc/PaperTrail.html b/doc/PaperTrail.html index d7e3cb2a..9988930c 100644 --- a/doc/PaperTrail.html +++ b/doc/PaperTrail.html @@ -5,7 +5,7 @@ - PaperTrail – paper_trail v0.1.4 + PaperTrail – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

@@ -60,7 +60,7 @@

- paper_trail v0.1.4 + paper_trail v0.1.5 PaperTrail diff --git a/doc/api-reference.html b/doc/api-reference.html index 7ba86c65..dee0b49d 100644 --- a/doc/api-reference.html +++ b/doc/api-reference.html @@ -5,7 +5,7 @@ - API Reference – paper_trail v0.1.4 + API Reference – paper_trail v0.1.5 @@ -27,7 +27,7 @@

paper_trail

@@ -59,7 +59,7 @@