Skip to content

Commit

Permalink
Fix that removing tenant with SQLite DB failed due to the DB file bei…
Browse files Browse the repository at this point in the history
…ng locked (Lombiq Technologies: OCORE-203) (#16858)
  • Loading branch information
Piedone authored Oct 15, 2024
1 parent d468933 commit 8290cf7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public async Task<DbConnectionValidatorResult> ValidateAsync(DbConnectionValidat
connection is SqliteConnection sqliteConnection &&
!File.Exists(sqliteConnection.DataSource))
{
SqliteConnection.ClearPool(sqliteConnection);
return DbConnectionValidatorResult.DocumentTableNotFound;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)
var databaseFolder = SqliteHelper.GetDatabaseFolder(shellOptions, shellSettings.Name);
Directory.CreateDirectory(databaseFolder);
// Only allow creating a file DB when a tenant is in the Initializing state
var connectionString = SqliteHelper.GetConnectionString(sqliteOptions, databaseFolder, shellSettings);
storeConfiguration
Expand Down
1 change: 1 addition & 0 deletions src/OrchardCore/OrchardCore.Data.YesSql/SqliteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static string GetConnectionString(SqliteOptions sqliteOptions, string dat
databaseName = "yessql.db";
}

// Only allow creating a file DB when a tenant is in the Initializing state.
var sqliteOpenMode = shellSettings.IsInitializing() ? SqliteOpenMode.ReadWriteCreate : SqliteOpenMode.ReadWrite;

return GetSqliteConnectionStringBuilder(sqliteOptions, databaseFolder, databaseName, sqliteOpenMode).ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<ProjectReference Include="..\OrchardCore.Recipes.Abstractions\OrchardCore.Recipes.Abstractions.csproj" />
<ProjectReference Include="..\OrchardCore.Setup.Abstractions\OrchardCore.Setup.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="YesSql" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions src/OrchardCore/OrchardCore.Setup.Core/SetupService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
Expand All @@ -13,6 +14,7 @@
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;
using OrchardCore.Setup.Events;
using YesSql;

namespace OrchardCore.Setup.Services;

Expand Down Expand Up @@ -260,6 +262,19 @@ await scope.ServiceProvider.GetService<IShellDescriptorManager>()
return executionId;
}

// When using SQLite, clearing the connection pool with ReadWriteCreate SqliteOpenMode, used when the shell was
// still initializing, to unlock the database file (and thus also allow it to be deleted).
if (shellSettings["DatabaseProvider"] == DatabaseProviderValue.Sqlite)
{
await using var shellContext = await _shellContextFactory.CreateMinimumContextAsync(shellSettings);
var store = shellContext.ServiceProvider.GetRequiredService<IStore>();
await using var connection = store.Configuration.ConnectionFactory.CreateConnection();
if (connection is SqliteConnection sqliteConnection)
{
SqliteConnection.ClearPool(sqliteConnection);
}
}

// Update the shell state.
await _shellHost.UpdateShellSettingsAsync(shellSettings.AsRunning());

Expand Down

0 comments on commit 8290cf7

Please sign in to comment.