diff --git a/GitTrends/Database/BaseDatabase.cs b/GitTrends/Database/BaseDatabase.cs index acb8468ad..79e1a916d 100644 --- a/GitTrends/Database/BaseDatabase.cs +++ b/GitTrends/Database/BaseDatabase.cs @@ -11,20 +11,20 @@ namespace GitTrends { public abstract class BaseDatabase { + readonly SQLiteAsyncConnection _databaseConnection; + protected BaseDatabase(IFileSystem fileSystem, IAnalyticsService analyticsService, TimeSpan expiresAt) { ExpiresAt = expiresAt; AnalyticsService = analyticsService; var databasePath = Path.Combine(fileSystem.AppDataDirectory, $"{nameof(GitTrends)}.db3"); - DatabaseConnection = new SQLiteAsyncConnection(databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache); + _databaseConnection = new SQLiteAsyncConnection(databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache); } public TimeSpan ExpiresAt { get; } protected IAnalyticsService AnalyticsService { get; } - SQLiteAsyncConnection DatabaseConnection { get; } - public abstract Task DeleteAllData(); protected static Task AttemptAndRetry(Func> action, int numRetries = 12) @@ -38,22 +38,22 @@ protected static Task AttemptAndRetry(Func> action, int numRetries protected async ValueTask GetDatabaseConnection() { - if (!DatabaseConnection.TableMappings.Any(x => x.MappedType == typeof(T))) + if (!_databaseConnection.TableMappings.Any(x => x.MappedType == typeof(T))) { - await DatabaseConnection.EnableWriteAheadLoggingAsync().ConfigureAwait(false); + await _databaseConnection.EnableWriteAheadLoggingAsync().ConfigureAwait(false); try { - await DatabaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false); + await _databaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false); } catch (SQLiteException e) when (e.Message.Contains("PRIMARY KEY", StringComparison.OrdinalIgnoreCase)) { - await DatabaseConnection.DropTableAsync(DatabaseConnection.TableMappings.First(x => x.MappedType == typeof(T))); - await DatabaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false); + await _databaseConnection.DropTableAsync(_databaseConnection.TableMappings.First(x => x.MappedType == typeof(T))); + await _databaseConnection.CreateTablesAsync(CreateFlags.None, typeof(T)).ConfigureAwait(false); } } - return DatabaseConnection; + return _databaseConnection; } } } \ No newline at end of file