Sam Houston
Sam Houston

Reputation: 3661

How to flush your database using Repo

More specifically I have a Phoenix Application using Ecto and Repo. I would like to flush my database after running each Exunit test which alters the database.

One way I could accomplish this would be to run all of the change functions in the migrations in priv/repo/migrations/ directory, but I feel that there should be a nicer way.

Perhaps something like running the flush function?

Upvotes: 0

Views: 692

Answers (2)

PatNowak
PatNowak

Reputation: 5812

If you would collect these side-effected tests into one file, you could use setup callback, like this:

setup do
  for model <- [list modules you want to clear], do: Repo.delete_all(model)
end

Upvotes: 1

bratsche
bratsche

Reputation: 2674

I believe this is already handled for you when running Ecto in sandbox mode, which is the way Phoenix generates the test cases.

Upvotes: 2

Related Questions