Create a gist now

Instantly share code, notes, and snippets.

# TODO:
# Runner like https://github.com/elixir-lang/ecto/blob/master/lib/ecto/migration.ex (does it have state?)
defmodule Ecto.Runner do
def start_command({:create, table}) do
IO.puts "create table: #{table}"
end
def subcommand({:add, column, type}) do
IO.puts "add column: #{column}(#{type})"
end
def end_command do
IO.puts "done with something"
end
end
defmodule Ecto.Migration do
alias Ecto.Runner
defmodule Create do
def add(column, type) do
Runner.subcommand({:add, column, type})
end
end
defmacro __using__(_opts) do
quote do
import unquote(__MODULE__)
end
end
defmacro create(object, do: block) do
{:table, _, [name]} = object
quote do
fn ->
import Create
Runner.start_command({:create, unquote(name)})
unquote(block)
Runner.end_command
end.()
end
end
end
defmodule MyMigration do
use Ecto.Migration
def change do
create table(:foo) do
add :name, :string
end
end
end
MyMigration.change
@henrik
Owner
henrik commented May 30, 2015

Changed to make "add" only available within "create" based on this discussion: https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/elixir-lang-talk/J5j0t_UYEnI/EvvFFer0pR4J

@henrik
Owner
henrik commented Nov 6, 2015

Actually, "add" was available within the entire "change" function, so I fixed that with a fn -> … end.() to scope the import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment