Include extension migration generation mix task

This module name contains `Isn` (not `ISN`), because otherwise mix will
not find it when running `mix isn.gen.migration`.
This commit is contained in:
Martin Frost 2016-01-10 12:29:30 +01:00
parent 0cef21e3da
commit cab8bca1d9
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
defmodule Mix.Tasks.Isn.Gen.Migration do
use Mix.Task
import Mix.Ecto
import Mix.Generator
@migration_name "CreateISNExtension"
@shortdoc "Generate an Ecto migration to add the `isn` extension"
@moduledoc """
Generate an Ecto migration to add the `isn` extension to your database.
## Example
mix isn.gen.migration
"""
@doc false
def run(args) do
Mix.Task.run "app.start", args
[repo] = parse_repo(args)
filename = "#{timestamp}_create_isn_extension.exs"
IO.inspect([repo, Mix.Project.app_path, filename])
path = Path.relative_to(migrations_path(repo), Mix.Project.app_path)
file = Path.join(path, filename)
create_directory(path)
mod = Module.concat([repo, Migrations, @migration_name])
create_file(file, migration_template(mod: mod))
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(s), do: to_string(s) |> String.rjust(2, ?0)
embed_template :migration, """
defmodule <%= inspect @mod %> do
use Ecto.Migration
def up do
execute "CREATE EXTENSION IF NOT EXISTS isn;"
end
def down do
execute "DROP EXTENSION IF EXISTS isn;"
end
end
"""
end