Skip to content

Commit d93fbd4

Browse files
handle process dying gracefully
1 parent 48c3488 commit d93fbd4

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

integration_test/myxql/storage_test.exs

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

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

lib/ecto/adapters/myxql.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ defmodule Ecto.Adapters.MyXQL do
587587
env: validate_env(env),
588588
args: args
589589
]
590-
590+
# Trap exits in case mysql dies in the middle of execution so that we can surface the error
591+
Process.flag(:trap_exit, true)
591592
port = Port.open({:spawn_executable, abs_cmd}, port_opts)
592593
Port.command(port, contents)
593594
# Use this as a signal to close the port since we cannot
@@ -653,6 +654,9 @@ defmodule Ecto.Adapters.MyXQL do
653654
collect_output(port, acc)
654655
end
655656

657+
{:EXIT, ^port, _reason} ->
658+
{acc, 1}
659+
656660
{^port, {:exit_status, status}} ->
657661
{acc, status}
658662
end

0 commit comments

Comments
 (0)