Packages
paper_trail
0.8.2
1.1.2
1.1.1
1.1.0
1.0.0
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.0
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.3
0.11.2
0.11.1
0.11.0
0.10.1
0.10.0
0.9.0
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.9
0.7.8
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.3
0.5.2
0.5.0
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Track and record all the changes in your database. Revert back to anytime in history.
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/papertrail/install.ex
defmodule Mix.Tasks.Papertrail.Install do
@shortdoc "generates paper_trail migration file for your database"
use Mix.Task
import Macro, only: [underscore: 1]
import Mix.Generator
def run(_args) do
path = Path.relative_to("priv/repo/migrations", Mix.Project.app_path())
file = Path.join(path, "#{timestamp()}_#{underscore(AddVersions)}.exs")
create_directory(path)
create_file(file, """
defmodule Repo.Migrations.AddVersions do
use Ecto.Migration
def change do
create table(:versions) do
add :event, :string, null: false, size: 10
add :item_type, :string, null: false
add :item_id, :integer
add :item_changes, :map, null: false
add :originator_id, references(:users) # you can change :users to your own foreign key constraint
add :origin, :string, size: 50
add :meta, :map
add :inserted_at, :utc_datetime, null: false
end
create index(:versions, [:originator_id])
create index(:versions, [:item_id, :item_type])
# Uncomment if you want to add the following indexes to speed up special queries:
# create index(:versions, [:event, :item_type])
# create index(:versions, [:item_type, :inserted_at])
end
end
""")
end
defp timestamp do
{{y, m, d}, {hh, mm, ss}} = :calendar.universal_time()
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
end
defp pad(i) when i < 10, do: <<?0, ?0 + i>>
defp pad(i), do: to_string(i)
end