Skip to content

Commit

Permalink
fix: use ecto migrator with_repo to avoid swallowing errors (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah authored Dec 14, 2023
1 parent 7e3ffa3 commit 744d9dc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
22 changes: 15 additions & 7 deletions lib/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ defmodule Testcontainers.Ecto do
)
)

{:ok, pid} = repo.start_link()

absolute_migrations_path =
if Path.absname(migrations_path) != migrations_path,
do: Application.app_dir(app, migrations_path),
Expand All @@ -291,17 +289,27 @@ defmodule Testcontainers.Ecto do
:ok
end

Ecto.Migrator.run(repo, absolute_migrations_path, :up, all: true)

GenServer.stop(pid)

{:ok, container}
with {:ok, _, _} <- run_migrations(repo, absolute_migrations_path) do
{:ok, container}
end

{:error, reason} ->
{:error, reason}
end
end

defp run_migrations(repo, migrations_path) do
try do
Ecto.Migrator.with_repo(

Check warning on line 303 in lib/ecto.ex

View workflow job for this annotation

GitHub Actions / Test example projects

Ecto.Migrator.with_repo/2 is undefined (module Ecto.Migrator is not available or is yet to be defined)
repo,
&Ecto.Migrator.run(&1, migrations_path, :up, all: true)

Check warning on line 305 in lib/ecto.ex

View workflow job for this annotation

GitHub Actions / Test example projects

Ecto.Migrator.run/4 is undefined (module Ecto.Migrator is not available or is yet to be defined)
)
rescue
e ->
{:error, e}
end
end

defp camelize(string) do
string
|> String.split("_")
Expand Down
20 changes: 12 additions & 8 deletions test/ecto_errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ defmodule Testcontainers.EctoErrorsTest do
import Testcontainers.Ecto

test "repo cannot be nil" do
assert_raise UndefinedFunctionError,
"function Testcontainers.Repo.start_link/0 is undefined (module Testcontainers.Repo is not available)",
fn ->
mysql_container(
app: :testcontainers,
repo: nil
)
end
assert {:error,
%UndefinedFunctionError{
module: Testcontainers.Repo,
function: :config,
arity: 0,
reason: nil,
message: nil
}} =
mysql_container(
app: :testcontainers,
repo: nil
)
end

test "repo must be atom" do
Expand Down
9 changes: 9 additions & 0 deletions test/ecto_mysql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ defmodule Testcontainers.EctoMysqlTest do
assert Testcontainers.MysqlRepo.all(Testcontainers.TestUser) == []
Testcontainers.stop_container(container.container_id)
end

test "fails properly when migrations doesnt pass successfully" do
assert {:error, %MyXQL.Error{message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'stringa NOT NULL, `hashed_password` varchar(255) NOT NULL, `confirmed_at` dateti' at line 1"}} =
mysql_container(
app: :testcontainers,
migrations_path: "#{__DIR__}/support/bad_migrations",
repo: Testcontainers.MysqlRepo
)
end
end
9 changes: 9 additions & 0 deletions test/ecto_postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ defmodule Testcontainers.EctoPostgresTest do
assert Testcontainers.PostgresRepo.all(Testcontainers.TestUser) == []
Testcontainers.stop_container(container.container_id)
end

test "fails properly when migrations doesnt pass successfully" do
assert {:error, %Postgrex.Error{postgres: %{message: "type \"stringa\" does not exist"}}} =
postgres_container(
app: :testcontainers,
migrations_path: "#{__DIR__}/support/bad_migrations",
repo: Testcontainers.PostgresRepo
)
end
end
12 changes: 12 additions & 0 deletions test/support/bad_migrations/20231022122557_add_posts_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Testcontainers.Repo.Migrations.AddPostsTable do
use Ecto.Migration

def change do
create table(:users) do
add(:email, :stringa, null: false)
add(:hashed_password, :string, null: false)
add(:confirmed_at, :naive_datetime)
timestamps()
end
end
end

0 comments on commit 744d9dc

Please sign in to comment.