Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 4, 2020 05:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/25516815e6680e1c7a82 to your computer and use it in GitHub Desktop.
Save henrik/25516815e6680e1c7a82 to your computer and use it in GitHub Desktop.
# 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
Copy link
Author

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
Copy link
Author

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