Skip to content

Commit 84fe263

Browse files
Add execute_file/1 and /2 (#470)
1 parent f4fa96c commit 84fe263

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/ecto/migration.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,31 @@ defmodule Ecto.Migration do
929929
Runner.execute %Command{up: up, down: down}
930930
end
931931

932+
@doc """
933+
Executes a SQL command from a file.
934+
935+
The argument must be a path to a file containing a SQL command.
936+
937+
Reversible commands can be defined by calling `execute_file/2`.
938+
"""
939+
def execute_file(path) when is_binary(path) do
940+
command = File.read!(path)
941+
Runner.execute command
942+
end
943+
944+
@doc """
945+
Executes reversible SQL commands from files.
946+
947+
Each argument must be a path to a file containing a SQL command.
948+
949+
See `execute/2` for more information on executing SQL commands.
950+
"""
951+
def execute_file(up_path, down_path) when is_binary(up_path) and is_binary(down_path) do
952+
up = File.read!(up_path)
953+
down = File.read!(down_path)
954+
Runner.execute %Command{up: up, down: down}
955+
end
956+
932957
@doc """
933958
Gets the migrator direction.
934959
"""

test/ecto/migration_test.exs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ defmodule Ecto.MigrationTest do
33
# is global state, we can run it async as long as this is
44
# the only test case that uses the Runner in async mode.
55
use ExUnit.Case, async: true
6-
76
use Ecto.Migration
87

8+
import Support.FileHelpers
9+
910
alias EctoSQL.TestRepo
1011
alias Ecto.Migration.{Table, Index, Reference, Constraint}
1112
alias Ecto.Migration.Runner
@@ -694,6 +695,16 @@ defmodule Ecto.MigrationTest do
694695
assert "SELECT 1" = last_command()
695696
end
696697

698+
test "forward: executes a command from a file" do
699+
in_tmp fn _path ->
700+
up_sql = ~s(CREATE TABLE IF NOT EXISTS "execute_file_table" \(i integer\))
701+
File.write!("up.sql", up_sql)
702+
execute_file "up.sql"
703+
flush()
704+
assert up_sql == last_command()
705+
end
706+
end
707+
697708
test "fails gracefully with nested create" do
698709
assert_raise Ecto.MigrationError, "cannot execute nested commands", fn ->
699710
create table(:posts) do
@@ -857,5 +868,19 @@ defmodule Ecto.MigrationTest do
857868
assert "SELECT 2" = last_command()
858869
end
859870

871+
test "backward: reverses a command from a file" do
872+
in_tmp fn _path ->
873+
up_sql = ~s(CREATE TABLE IF NOT EXISTS "execute_file_table" \(i integer\))
874+
File.write!("up.sql", up_sql)
875+
876+
down_sql = ~s(DROP TABLE IF EXISTS "execute_file_table")
877+
File.write!("down.sql", down_sql)
878+
879+
execute_file "up.sql", "down.sql"
880+
flush()
881+
assert down_sql == last_command()
882+
end
883+
end
884+
860885
defp last_command(), do: Process.get(:last_command)
861886
end

0 commit comments

Comments
 (0)