Last active
December 4, 2020 05:41
-
-
Save henrik/25516815e6680e1c7a82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, "add" was available within the entire "change" function, so I fixed that with a
fn -> … end.()
to scope theimport
.