Skip to content

Commit

Permalink
mix task added: mix paper_trail.install
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jun 19, 2016
1 parent 00f95dc commit 73eb8b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
36 changes: 34 additions & 2 deletions lib/mix/tasks/papertrail/install.ex
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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule PaperTrail.Mixfile do
maintainers: ["Izel Nakri"],
licenses: ["MIT License"],
links: %{
# "GitHub" => "https://github.com/ericmj/postgrex",
"GitHub" => "https://github.com/izelnakri/paper_trail"
# "Docs" => "http://ericmj.github.io/postgrex/"
}
]
Expand Down

0 comments on commit 73eb8b4

Please sign in to comment.