From 73eb8b4eac05ecef330f749fece023859c15bfed Mon Sep 17 00:00:00 2001 From: Izel Nakri Date: Sun, 19 Jun 2016 21:17:40 +0200 Subject: [PATCH] mix task added: mix paper_trail.install --- lib/mix/tasks/papertrail/install.ex | 36 +++++++++++++++++++++++++++-- mix.exs | 2 +- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/mix/tasks/papertrail/install.ex b/lib/mix/tasks/papertrail/install.ex index baedbccd..c1f1cb99 100644 --- a/lib/mix/tasks/papertrail/install.ex +++ b/lib/mix/tasks/papertrail/install.ex @@ -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 diff --git a/mix.exs b/mix.exs index d3ff3a3e..b7345c75 100644 --- a/mix.exs +++ b/mix.exs @@ -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/" } ]