Skip to content

Commit 9fbb80d

Browse files
Handle abnormal MySQL structure_load death gracefully (#723)
1 parent 48c3488 commit 9fbb80d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

integration_test/myxql/storage_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ defmodule Ecto.Integration.StorageTest do
121121
drop_database()
122122
end
123123

124+
test "structure load will fail on SQL errors" do
125+
create_database()
126+
File.mkdir_p!(tmp_path())
127+
error_path = Path.join(tmp_path(), "error.sql")
128+
File.write!(error_path, "SELECT 1; SELECKT 1; SELECT 2;")
129+
130+
{:error, message} =
131+
Ecto.Adapters.MyXQL.structure_load(tmp_path(), [dump_path: error_path] ++ params())
132+
133+
assert message =~ ~r/ERROR.*SELECKT/
134+
after
135+
drop_database()
136+
end
137+
124138
test "storage status is up when database is created" do
125139
create_database()
126140
assert :up == Ecto.Adapters.MyXQL.storage_status(params())

lib/ecto/adapters/myxql.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,16 @@ defmodule Ecto.Adapters.MyXQL do
588588
args: args
589589
]
590590

591+
# Trap exits in case mysql dies in the middle of execution so that we can surface the error
592+
old_trap_exit = Process.flag(:trap_exit, true)
591593
port = Port.open({:spawn_executable, abs_cmd}, port_opts)
592594
Port.command(port, contents)
593595
# Use this as a signal to close the port since we cannot
594596
# send an exit command to mysql in batch mode
595597
Port.command(port, ";SELECT '__ECTO_EOF__';\n")
596-
collect_output(port, "")
598+
result = collect_output(port, "")
599+
Process.flag(:trap_exit, old_trap_exit)
600+
result
597601
end
598602

599603
defp args_env(opts, opt_args) do
@@ -653,6 +657,9 @@ defmodule Ecto.Adapters.MyXQL do
653657
collect_output(port, acc)
654658
end
655659

660+
{:EXIT, ^port, _reason} ->
661+
{acc, 1}
662+
656663
{^port, {:exit_status, status}} ->
657664
{acc, status}
658665
end

0 commit comments

Comments
 (0)