-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mix task added: mix paper_trail.install
- Loading branch information
Showing
2 changed files
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,41 @@ | ||
defmodule Mix.Tasks.Papertrail.Install do | ||
@shortdoc "my short doc" | ||
@shortdoc "generates paper_trail migration file for your database" | ||
|
||
import Macro, only: [camelize: 1, underscore: 1] | ||
import Mix.Generator | ||
|
||
use Mix.Task | ||
|
||
def run(args) do | ||
IO.puts "run called" | ||
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 | ||
add :item_type, :string | ||
add :item_id, :integer | ||
add :item_changes, :map | ||
add :meta, :map | ||
add :originator, :string | ||
add :inserted_at, :datetime, null: false | ||
end | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters