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

Make Sqlite database name configurable #15209

Merged
merged 5 commits into from
Feb 1, 2024
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
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class SetupConstants
public const string DatabaseProvider = "DatabaseProvider";
public const string DatabaseConnectionString = "DatabaseConnectionString";
public const string DatabaseTablePrefix = "DatabaseTablePrefix";
public const string DatabaseName = "DatabaseName";
public const string DatabaseSchema = "DatabaseSchema";
public const string SiteTimeZone = "SiteTimeZone";
public const string FeatureProfile = "FeatureProfile";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)
var databaseFolder = SqliteHelper.GetDatabaseFolder(shellOptions, shellSettings.Name);
Directory.CreateDirectory(databaseFolder);

var connectionString = SqliteHelper.GetConnectionString(sqliteOptions, databaseFolder);
var connectionString = SqliteHelper.GetConnectionString(sqliteOptions, databaseFolder, shellSettings["DatabaseName"]);
storeConfiguration
.UseSqLite(connectionString, IsolationLevel.ReadUncommitted)
.UseDefaultIdGenerator();
Expand Down
21 changes: 15 additions & 6 deletions src/OrchardCore/OrchardCore.Data.YesSql/SqliteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ namespace OrchardCore.Data;

public static class SqliteHelper
{
public static string GetConnectionString(SqliteOptions sqliteOptions, ShellOptions shellOptions, string shellName) =>
GetConnectionString(sqliteOptions, GetDatabaseFolder(shellOptions, shellName));
public static string GetConnectionString(SqliteOptions sqliteOptions, ShellOptions shellOptions, string shellName, string databaseName = "")
=> GetConnectionString(sqliteOptions, GetDatabaseFolder(shellOptions, shellName), databaseName);

public static string GetConnectionString(SqliteOptions sqliteOptions, string databaseFolder) =>
new SqliteConnectionStringBuilder
public static string GetConnectionString(SqliteOptions sqliteOptions, string databaseFolder, string databaseName = "")
{
if (string.IsNullOrEmpty(databaseName))
{
DataSource = Path.Combine(databaseFolder, "yessql.db"),
// For backward compatibility, we assume that the database name is 'yessql.db'.
databaseName = "yessql.db";
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}

return new SqliteConnectionStringBuilder
{
DataSource = string.IsNullOrEmpty(databaseFolder)
? databaseName
: Path.Combine(databaseFolder, databaseName),
Cache = SqliteCacheMode.Shared,
Pooling = sqliteOptions.UseConnectionPooling
}
.ToString();

}
public static string GetDatabaseFolder(ShellOptions shellOptions, string shellName) =>
Path.Combine(shellOptions.ShellsApplicationDataPath, shellOptions.ShellsContainerName, shellName);
}
5 changes: 5 additions & 0 deletions src/OrchardCore/OrchardCore.Setup.Core/SetupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ private async Task<string> SetupInternalAsync(SetupContext context)
shellSettings["Schema"] = context.Properties.TryGetValue(SetupConstants.DatabaseSchema, out var schema) ? schema?.ToString() : null;
}

if (shellSettings["DatabaseProvider"] == DatabaseProviderValue.Sqlite && string.IsNullOrEmpty(shellSettings["DatabaseName"]))
{
shellSettings["DatabaseName"] = context.Properties.TryGetValue(SetupConstants.DatabaseName, out var dbName) ? dbName?.ToString() : "OrchardCore.db";
}

var validationContext = new DbConnectionValidatorContext(shellSettings);
switch (await _dbConnectionValidator.ValidateAsync(validationContext))
{
Expand Down
Loading