Merge pull request #6062 from duplicati/feature/improve-sqlite-config

Improve SQLite loading
This commit is contained in:
Kenneth Skovhede
2025-03-20 16:06:56 +01:00
committed by GitHub
2 changed files with 39 additions and 31 deletions
@@ -921,10 +921,12 @@ ORDER BY ""A"".""TargetPath"", ""BB"".""Index""");
{
if (!m_connection_pool.TryTake(out var connection))
{
connection = (IDbConnection)Activator.CreateInstance(SQLiteHelper.SQLiteLoader.SQLiteConnectionType);
connection = SQLiteHelper.SQLiteLoader.LoadConnection();
connection.ConnectionString = m_connection.ConnectionString + ";Cache=Shared;";
connection.Open();
SQLiteHelper.SQLiteLoader.ApplyCustomPragmas(connection);
using var cmd = connection.CreateCommand();
cmd.ExecuteNonQuery("PRAGMA journal_mode = WAL;");
cmd.ExecuteNonQuery("PRAGMA read_uncommitted = true;");
+36 -30
View File
@@ -99,6 +99,34 @@ namespace Duplicati.Library.SQLiteHelper
return con ?? throw new InvalidOperationException("Failed to load connection");
}
/// <summary>
/// Applies user-supplied custom pragmas to the SQLite connection
/// </summary>
/// <param name="con">The connection to apply the pragmas to.</param>
/// <returns>The connection with the pragmas applied.</returns>
public static System.Data.IDbConnection ApplyCustomPragmas(System.Data.IDbConnection con)
{
var opts = Environment.GetEnvironmentVariable("CUSTOMSQLITEOPTIONS_DUPLICATI");
if (string.IsNullOrWhiteSpace(opts))
return con;
using (var cmd = con.CreateCommand())
foreach (var opt in opts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
Logging.Log.WriteVerboseMessage(LOGTAG, "CustomSQLiteOption", @"Setting custom SQLite option '{0}'.", opt);
try
{
cmd.CommandText = string.Format("PRAGMA {0}", opt);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Logging.Log.WriteWarningMessage(LOGTAG, "CustomSQLiteOption", ex, @"Error setting custom SQLite option '{0}'.", opt);
}
}
return con;
}
/// <summary>
/// Loads an SQLite connection instance and opening the database
/// </summary>
@@ -109,7 +137,7 @@ namespace Duplicati.Library.SQLiteHelper
if (string.IsNullOrWhiteSpace(targetpath))
throw new ArgumentNullException(nameof(targetpath));
System.Data.IDbConnection con = LoadConnection();
var con = LoadConnection();
try
{
@@ -124,32 +152,7 @@ namespace Duplicati.Library.SQLiteHelper
}
// set custom Sqlite options
var opts = Environment.GetEnvironmentVariable("CUSTOMSQLITEOPTIONS_DUPLICATI");
if (opts != null)
{
var topts = opts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (topts.Length > 0)
{
using (var cmd = con.CreateCommand())
{
foreach (var opt in topts)
{
Logging.Log.WriteVerboseMessage(LOGTAG, "CustomSQLiteOption", @"Setting custom SQLite option '{0}'.", opt);
try
{
cmd.CommandText = string.Format("pragma {0}", opt);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Logging.Log.WriteErrorMessage(LOGTAG, "CustomSQLiteOption", ex, @"Error setting custom SQLite option '{0}'.", opt);
}
}
}
}
}
return con;
return ApplyCustomPragmas(con);
}
/// <summary>
@@ -189,9 +192,12 @@ namespace Duplicati.Library.SQLiteHelper
/// </summary>
private static void SetEnvironmentVariablesForSQLiteTempDir()
{
System.Environment.SetEnvironmentVariable("SQLITE_TMPDIR", Library.Utility.TempFolder.SystemTempPath);
System.Environment.SetEnvironmentVariable("TMP", Library.Utility.TempFolder.SystemTempPath);
System.Environment.SetEnvironmentVariable("TEMP", Library.Utility.TempFolder.SystemTempPath);
// Allow the user to override the temp folder for SQLite
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("SQLITE_TMPDIR")))
Environment.SetEnvironmentVariable("SQLITE_TMPDIR", Utility.TempFolder.SystemTempPath);
Environment.SetEnvironmentVariable("TMPDIR", Utility.TempFolder.SystemTempPath);
Environment.SetEnvironmentVariable("TMP", Utility.TempFolder.SystemTempPath);
Environment.SetEnvironmentVariable("TEMP", Utility.TempFolder.SystemTempPath);
}
/// <summary>