Versioned.Schema (Versioned v0.2.1) View Source
Enhances Ecto.Schema modules to track a full history of changes.
The versioned_schema macro works just like schema in Ecto.Schema but it
also builds an OriginalModule.Version schema module as well to represent a
version at a particular point in time.
In addition to options allowed by Ecto.Schema, new ones are also allowed.
Additional belongs_to Options
:versioned- Iftrue, the version schema will include an extra field of the same name but with_versionappended. The parent record which existed when this version was created can be loaded into this field viaVersioned.preload/2.
Example:
versioned_schema "people" do
belongs_to :car, Car, type: :binary_id, versioned: true
end
Additional has_many Options
:versioned- Iftrue, the version schema will include an extra field of the same name but with_versionappended. If defined as another truthy atom, then that field name will be used instead. The child records which existed when this version was created can be loaded into this field viaVersioned.preload/2.
Example:
versioned_schema "cars" do
has_many :people, Person, on_replace: :delete, versioned: :person_versions
endI already have an integer primary key.
While Versioned generally operates with binary_ids, it is possible to adopt it
for an existing table which uses integers. First, you'll need to create a
migration to create the versions table. Then, define your own @primary_key
attribute. Versioned will preserve it, but note that the versions table must
still use UUIDs.
@primary_key {:id, :id, autogenerate: true}
versioned_schema "cars" do
...
endExample
defmodule MyApp.Car do
use Versioned.Schema
versioned_schema "cars" do
field :name, :string
has_many :people, MyApp.Person, versioned: true
end
end
Link to this section Summary
Functions
Convert a list of ast lines from the main schema into ast lines to be used for the version schema.
Create a versioned schema.
Link to this section Functions
Convert a list of ast lines from the main schema into ast lines to be used for the version schema.
Create a versioned schema.