izelnakri / paper_trail

Track and record all the changes in your database with Ecto. Revert back to anytime in history.
https://hex.pm/packages/paper_trail
MIT License
565 stars 92 forks source link

Allow custom ecto schema @primary_key #227

Closed rodrigues closed 1 year ago

rodrigues commented 1 year ago

Paper trail fails to create a new version when the application has UUID as migration primary key type:

config :xyz, XYZ.Repo, migration_primary_key: [type: :binary_id]
 ** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "id" of relation "versions" violates not-null constraint

         table: versions
         column: id

     Failing row contains (null, insert, XYZ, 9a955fe1-74db-4038-aaa5-d3a6390036cf, {"id": "9a955fe1-74db-4038-aaa5-d3a6390036cf", "etc": null, "..., null, xyz, {}, 2023-09-07 09:23:50).

With this PR, we can store versions with autogenerated UUIDs as ids:

config :paper_trail,
  repo: XYZ.Repo,
  version_primary_key: {:id, Ecto.UUID, autogenerate: true}
version #=> %PaperTrail.Version{
  __meta__: #Ecto.Schema.Metadata<:loaded, "versions">,
  id: "ab1bfacf-e2f7-4d7d-b1ba-fa8dbfefeace",
  event: "update",
  item_type: ...

Do you think this approach makes sense?

Cheers!