Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use ecto migrator with_repo to avoid swallowing errors #60

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
)
)

{: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 @@
: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)
)
jarlah marked this conversation as resolved.
Show resolved Hide resolved
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
Loading