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

Clear all connection pools before attempting to delete SQLite file #25985

Merged
merged 1 commit into from
Sep 14, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public override void Delete()

if (!string.IsNullOrEmpty(path))
{
SqliteConnection.ClearPool(new SqliteConnection(Dependencies.Connection.ConnectionString));
SqliteConnection.ClearAllPools();
File.Delete(path);
}
}
Expand Down
49 changes: 49 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/SqliteDatabaseCreatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ public async Task Create_sets_journal_mode_to_wal(bool async)
Assert.Equal("wal", journalMode);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task Delete_works_even_when_different_connection_exists_to_same_file(bool async)
{
using (var context = new BathtubContext("DataSource=bathtub.db"))
{
if (async)
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
}
else
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
}

using (var context = new BathtubContext("Command Timeout=60;DataSource=bathtub.db"))
{
var creator = context.GetService<IRelationalDatabaseCreator>();

if (async)
{
await context.Database.EnsureDeletedAsync();
Assert.False(await creator.ExistsAsync());
}
else
{
context.Database.EnsureDeleted();
Assert.False(creator.Exists());
}
}
}

private class BathtubContext : DbContext
{
private readonly string _connectionString;

public BathtubContext(string connectionString)
{
_connectionString = connectionString;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite(_connectionString);
}

[ConditionalTheory]
[InlineData("Data Source=:memory:")]
[InlineData("Data Source=exists-memory;Mode=Memory;Cache=Shared")]
Expand Down