question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing migrations using SQLite in-memory database

See original GitHub issue

Problem 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
bricelamcommented, Oct 29, 2019

Will the fix be backported to 2.2?

Unlikely. But 3.1 is scheduled to release in December (the same month 2.2 goes out of support)

0reactions
klausenbuskcommented, Oct 29, 2019

https://github.com/aspnet/AspNetCore/blob/d34dc80e02161369908d8560f550e6e2822807b3/src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseErrorPageMiddleware.cs#L138-L147

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.

Investigated; this is a bug. Mode=Memory isn’t being handled here:

Will the fix be backported to 2.2?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing EF Core in Memory using SQLite
A good practice is to test your software. EF Core provides an In-Memory provider to easily test your code without an actual database....
Read more >
SQLite in-memory database and migrations... : r/django
The project's testing framework is able to automatically perform migrations after the in-memory database is created. My question is, when ...
Read more >
EF Core 3.1 Creating Views in Sqlite in-memory database ...
I do have manually modified migration file that will create the View using raw sql. And this works on SqlServer, but it appears...
Read more >
Using EntityFramework with Sqlite InMemory, ...
Using EntityFramework with Sqlite in-memory and concurrency support, that is fully unit-testable and supports migrations.
Read more >
Unit test - Run migrations on in memory DB - Discussion
Anyone has an idea how I can open a sqlite3 in memory db and run the migrations? Alternatives for a proper test setup...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found