Testing migrations using SQLite in-memory database
See original GitHub issueProblem to solve
In my organization, loads of migrations using EF Core are generated over the same database. Thanks to the use of Git branches, when branches are merged to master sometimes occurs that one migration cannot be applied. The reason is most of the time a programmer fault. For example, when a field of an entity has been added in master but no migration was done, and then from different branches a new migration is generated, they generate a similar migration where the new column is added. When this occurs, the second time that the ‘Add column’ instruction is executed will fail.
Our decision was to add some tests in order to guarantee: first, that a migration is created when the model differs from the model snapshot and second, that the existing migrations can be executed in order from the first one.
For the second mentioned test, a database is created and the migrations are applied in order. The code used for this is similar to the one that can be found in this repo.
In the majority of our tests we use an SQLite in-memory database. However, at this test we can not use it because an error appears (the provided repo reproduces the error when the Program class is executed):
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'table "__EFMigrationsHistory" already exists'.'
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.<PrepareAndEnumerateStatements>d__64.MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.<GetStatements>d__54.MoveNext()
at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at MigrationsSqlite.Program.Main(String[] args) in MigrationsSqlite\MigrationsSqlite\Program.cs:line 39
Our solution relies on using a LocalDB, generating the MDF file in a controlled location and remove the generated files after the test execution, but the performance is not as good as desired.
We have checked similar issues related with testing migrations using the InMemory provider, like this. However, our case is different because we would like to use SQLite, despite all the limitations related to the supported migrations.
Desired solution
After this background, our question is:
¿Are there plans to support testing migrations using a SQLite in-memory database? ¿Are there any other possible approaches for testing migrations?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:10 (6 by maintainers)
Unlikely. But 3.1 is scheduled to release in December (the same month 2.2 goes out of support)
Thanks! I look forward to https://github.com/aspnet/EntityFrameworkCore/pull/14122, it is a bit easier just to run a few commands as part of our CI.
Will the fix be backported to 2.2?