From 0d42024f02314f9d9b788c643b76cdba2fece6f8 Mon Sep 17 00:00:00 2001 From: Ashley Marques Date: Sat, 10 Jun 2023 10:32:35 -0300 Subject: [PATCH 1/2] change conn string --- .../DatabaseOperations.cs | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/Extensions.Caching.PostgreSql/DatabaseOperations.cs b/Extensions.Caching.PostgreSql/DatabaseOperations.cs index d5166ca..e59bf21 100644 --- a/Extensions.Caching.PostgreSql/DatabaseOperations.cs +++ b/Extensions.Caching.PostgreSql/DatabaseOperations.cs @@ -19,7 +19,7 @@ internal sealed class DatabaseOperations : IDatabaseOperations public DatabaseOperations(IOptions options, ILogger logger) { - var cacheOptions = options.Value; + cacheOptions = options.Value; if (string.IsNullOrEmpty(cacheOptions.ConnectionString)) { @@ -37,7 +37,7 @@ public DatabaseOperations(IOptions options, ILogger options, ILogger + /// Modify current conection string + /// + /// the new connection string to replace + public void ChangeConnectionString(string newConnectionString) + { + string original = _connectionString; + try + { + using var newConnection = new NpgsqlConnection(newConnectionString); + newConnection.Open(); + bool connected = newConnection.ExecuteScalar("SELECT 1=1;"); + if (connected) + { + _connectionString = newConnectionString; + _logger.LogDebug("ChangeConnectionString: new connection is valid and is use"); + } + newConnection.Close(); + if (cacheOptions.CreateInfrastructure) + { + CreateSchemaAndTableIfNotExist(); + } + } + catch (System.Exception ex) + { + _connectionString = original; + throw new InvalidOperationException("ChangeConnectionString failed.\nPrevious connection restored.", ex); + } - private ISystemClock SystemClock { get; } + + } private void CreateSchemaAndTableIfNotExist() { - using (var connection = new NpgsqlConnection(ConnectionString)) + using (var connection = new NpgsqlConnection(_connectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) @@ -76,7 +107,7 @@ private void CreateSchemaAndTableIfNotExist() public void DeleteCacheItem(string key) { - using var connection = new NpgsqlConnection(ConnectionString); + using var connection = new NpgsqlConnection(_connectionString); var deleteCacheItem = new CommandDefinition( SqlCommands.DeleteCacheItemSql, @@ -88,7 +119,7 @@ public void DeleteCacheItem(string key) public async Task DeleteCacheItemAsync(string key, CancellationToken cancellationToken) { - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var deleteCacheItem = new CommandDefinition( SqlCommands.DeleteCacheItemSql, @@ -116,7 +147,7 @@ public async Task DeleteExpiredCacheItemsAsync(CancellationToken cancellationTok { var utcNow = SystemClock.UtcNow; - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var deleteExpiredCache = new CommandDefinition( SqlCommands.DeleteExpiredCacheSql, @@ -132,7 +163,7 @@ public void SetCacheItem(string key, byte[] value, DistributedCacheEntryOptions var absoluteExpiration = GetAbsoluteExpiration(utcNow, options); ValidateOptions(options.SlidingExpiration, absoluteExpiration); - using var connection = new NpgsqlConnection(ConnectionString); + using var connection = new NpgsqlConnection(_connectionString); var expiresAtTime = options.SlidingExpiration == null ? absoluteExpiration!.Value @@ -159,7 +190,7 @@ public async Task SetCacheItemAsync(string key, byte[] value, DistributedCacheEn var absoluteExpiration = GetAbsoluteExpiration(utcNow, options); ValidateOptions(options.SlidingExpiration, absoluteExpiration); - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var expiresAtTime = options.SlidingExpiration == null ? absoluteExpiration!.Value @@ -185,7 +216,7 @@ private byte[] GetCacheItem(string key, bool includeValue) var utcNow = SystemClock.UtcNow; byte[] value = null; - using var connection = new NpgsqlConnection(ConnectionString); + using var connection = new NpgsqlConnection(_connectionString); var updateCacheItem = new CommandDefinition( SqlCommands.UpdateCacheItemSql, @@ -208,7 +239,7 @@ private async Task GetCacheItemAsync(string key, bool includeValue, Canc var utcNow = SystemClock.UtcNow; byte[] value = null; - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var updateCacheItem = new CommandDefinition( SqlCommands.UpdateCacheItemSql, From ae9ca0f3878e1fead878c5b7f979d7068dbe4332 Mon Sep 17 00:00:00 2001 From: Ashley Marques Date: Sat, 10 Jun 2023 10:38:08 -0300 Subject: [PATCH 2/2] modify interface --- Extensions.Caching.PostgreSql/IDatabaseOperations.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Extensions.Caching.PostgreSql/IDatabaseOperations.cs b/Extensions.Caching.PostgreSql/IDatabaseOperations.cs index 68d529c..559ed97 100644 --- a/Extensions.Caching.PostgreSql/IDatabaseOperations.cs +++ b/Extensions.Caching.PostgreSql/IDatabaseOperations.cs @@ -26,5 +26,6 @@ public interface IDatabaseOperations Task SetCacheItemAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken cancellationToken); Task DeleteExpiredCacheItemsAsync(CancellationToken cancellationToken); + void ChangeConnectionString(string newConnectionString); } } \ No newline at end of file