diff --git a/lib/tasks/isn.gen.migration.ex b/lib/tasks/isn.gen.migration.ex new file mode 100644 index 0000000..8d93b32 --- /dev/null +++ b/lib/tasks/isn.gen.migration.ex @@ -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