JonPSmith / EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
https://www.thereformedprogrammer.net/new-features-for-unit-testing-your-entity-framework-core-5-code/
Other
351 stars 53 forks source link

EnsureClean fails for PostgreSQL database if the schemas depend on each other #61

Closed AWehrhahn closed 3 months ago

AWehrhahn commented 3 months ago

When using EnsureClean() to create a empty database I get the following error: Npgsql.PostgresException: 3F000: schema "_timescaledb_debug" does not exist It does exist before EnsureClean, but gets dropped when the public schema gets dropped, but EnsureClean still tries to delete it. This happens e.g. when using the timescaledb extension. Maybe a simple IF EXISTS in the DROP SCHEMA will prevent this error?

Here is a simple example:

var context = new DataContext(options);
context.Database.ExecuteSqlRaw("CREATE EXTENSION IF NOT EXISTS timescaledb;");
context.Database.EnsureClean();
JonPSmith commented 3 months ago

Hi @AWehrhahn,

I'm not an expert on Postgres and the code inside the Postgres EnsureClean was provided Shay Rojansky from the Microsoft EF Core team, so I can't provide a solution myself. It looks like the Postgres code in PostgreSqlDropSchemaEnsureClean doesn't support schemas.

The Postgres EnsureClean is the fastest way to create an empty Postgres database, but using EnsureDeleted + EnsureCreated is still under a second, so I would suggest you use that instead.