From a889e29d64964fc1604d1871622551cb34e353a6 Mon Sep 17 00:00:00 2001 From: Jon P Smith Date: Tue, 15 Aug 2023 13:58:06 +0100 Subject: [PATCH] Created ConsoleApp to update sharding entries to AuthP format --- .../GetSetShardingEntriesFileStoreCache.cs | 7 +- .../ConsoleApp.AuthP6Upgrade.csproj | 19 ++++ ConsoleApp.AuthP6Upgrade/Program.cs | 102 ++++++++++++++++++ UpdateToVersion6.md | 15 +-- 4 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 ConsoleApp.AuthP6Upgrade/ConsoleApp.AuthP6Upgrade.csproj create mode 100644 ConsoleApp.AuthP6Upgrade/Program.cs diff --git a/AuthPermissions.AspNetCore/ShardingServices/GetSetShardingEntriesFileStoreCache.cs b/AuthPermissions.AspNetCore/ShardingServices/GetSetShardingEntriesFileStoreCache.cs index c7bf4ec9..d20d9b19 100644 --- a/AuthPermissions.AspNetCore/ShardingServices/GetSetShardingEntriesFileStoreCache.cs +++ b/AuthPermissions.AspNetCore/ShardingServices/GetSetShardingEntriesFileStoreCache.cs @@ -20,6 +20,11 @@ namespace AuthPermissions.AspNetCore.ShardingServices; /// public class GetSetShardingEntriesFileStoreCache : IGetSetShardingEntries { + /// + /// This is the prefix for creating the key to a sharding entry + /// + public static string ShardingEntryPrefix = "ShardingEntry-"; + /// /// This contains the methods with are specific to a database provider /// @@ -242,8 +247,6 @@ public string FormConnectionString(string shardingEntryName) //------------------------------------------------------ //private methods - private const string ShardingEntryPrefix = "ShardingEntry-"; - private string FormShardingEntryKey(string shardingEntryName) { return ShardingEntryPrefix + shardingEntryName; diff --git a/ConsoleApp.AuthP6Upgrade/ConsoleApp.AuthP6Upgrade.csproj b/ConsoleApp.AuthP6Upgrade/ConsoleApp.AuthP6Upgrade.csproj new file mode 100644 index 00000000..1ba0a3f0 --- /dev/null +++ b/ConsoleApp.AuthP6Upgrade/ConsoleApp.AuthP6Upgrade.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/ConsoleApp.AuthP6Upgrade/Program.cs b/ConsoleApp.AuthP6Upgrade/Program.cs new file mode 100644 index 00000000..2fa5d48e --- /dev/null +++ b/ConsoleApp.AuthP6Upgrade/Program.cs @@ -0,0 +1,102 @@ +// Copyright (c) 2023 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ +// Licensed under MIT license. See License.txt in the project root for license information. + +using Net.DistributedFileStoreCache.SupportCode; +using System.Text.Json; +using AuthPermissions.AspNetCore.ShardingServices; +using Net.DistributedFileStoreCache; +using Microsoft.Extensions.DependencyInjection; +using System.Text.Encodings.Web; + +namespace ConsoleApp.AuthP6Upgrade; +class Program +{ + static void Main(string[] args) + { + //The arguments needed are + //1. The name of the json file holding the AuthP version 5 sharding entries, e.g. shardingsettings.Production.json + //2. The name for the new FileStore Cache file, e.g. FileStoreCacheFile.Production.json + //3. The filepath to the the json file. This can be a relative or absolute + + + if (args.Length != 3) + { + Console.WriteLine("This app expects three arguments: "); + Console.WriteLine(" 1. The filepath to the the json file. This can be a relative or absolute."); + Console.WriteLine(" 2. The name of the json file holding the AuthP version 5 sharding entries."); + Console.WriteLine(" e.g. shardingsettings.Production.json"); + Console.WriteLine(" 3. The name for the new FileStore Cache file used by AuthP version 6."); + Console.WriteLine(" e.g. FileStoreCacheFile.Production.json"); + return; + } + + var filePathToJsonFilePath = Path.Combine(args[0], args[1]); + + if (!File.Exists(filePathToJsonFilePath)) + { + Console.WriteLine("No json file was found using the filepath and the json file name you provided."); + Console.WriteLine("The full filePath you provided is"); + Console.WriteLine(filePathToJsonFilePath); + return; + } + + var jsonString = File.ReadAllText(filePathToJsonFilePath); + Console.WriteLine(jsonString); + var shardingData = JsonSerializer.Deserialize(jsonString)?.ShardingDatabases; + + if (shardingData == null || !shardingData.Any()) + { + Console.WriteLine("There aren't any sharding entries in your json file to place the the FileStore cache."); + Console.WriteLine("In this case the FileStore cache will start empty, which is what you want."); + } + + var breakDownCacheName = args[2].Split('.'); + if (!((breakDownCacheName.Length == 3 && breakDownCacheName[2] == "json") + || (breakDownCacheName.Length == 2 && breakDownCacheName[1] != "json"))) + { + Console.WriteLine("The name of the FileStore Cache name doesn't have the correct format."); + Console.WriteLine("The FileStore Cache should have three parts, e.g. FileStoreCacheFile.Production.json"); + Console.WriteLine(args[2]); + return; + } + //Console.WriteLine(breakDownCacheName.ToString()); + + //Now use Net.DistributedFileStoreCache to add the + string fileStoreName = null; + var services = new ServiceCollection(); + services.AddDistributedFileStoreCache(options => + { + options.WhichVersion = FileStoreCacheVersions.Class; + options.PathToCacheFileDirectory = Path.GetFullPath(args[0]); + options.FirstPartOfCacheFileName = breakDownCacheName[0]; + options.SecondPartOfCacheFileName = breakDownCacheName[1]; + + options.JsonSerializerForCacheFile = new JsonSerializerOptions + { + //This will make the json in the FileStore json file will be easier to read + //BUT it will be a bit slower and take up more characters + WriteIndented = true, + //This makes unicode chars smaller - especially useful for FileStoreCacheVersions.Class + //see https://github.com/JonPSmith/Net.DistributedFileStoreCache/wiki/Tips-on-making-your-cache-fast#class-version---already-has-unsaferelaxedjsonescaping + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }; + fileStoreName = options.FormCacheFileName(); + }); + var serviceProvider = services.BuildServiceProvider(); + var readWriteService = serviceProvider.GetRequiredService(); + + foreach (var shardingEntry in shardingData) + { + var key = GetSetShardingEntriesFileStoreCache.ShardingEntryPrefix + shardingEntry.Name; + readWriteService.SetClass(key, shardingEntry); + Console.WriteLine($"Added the sharding entry with the name of {shardingEntry.Name} added to the FileStore"); + } + + Console.WriteLine($"Successfully copied {shardingData.Count} sharding entry to the FileStore Cache called '{fileStoreName}'."); + } +} + +public class JsonFileFormat +{ + public List ShardingDatabases { get; set; } +} \ No newline at end of file diff --git a/UpdateToVersion6.md b/UpdateToVersion6.md index 9b4d09cf..f448fb99 100644 --- a/UpdateToVersion6.md +++ b/UpdateToVersion6.md @@ -14,8 +14,7 @@ Of course this creates more breaking changes, but the code will be easier to und 1. Changing to the new IGetSetShardingEntries service. 2. Updating the IGetSetShardingEntries method names 3. Make sure that distributed FileStore Cache is registered -4. Once it complies and **before you run your application** - - Move your sharding entries from the json file to the distributed FileStore Cache +4. Move your AuthP 5 sharding entries to the AuthP 5 FileStore Cache The subsections below the items listed in the table of content. @@ -47,16 +46,20 @@ The table below gives the old and new. | `GetAllPossibleShardingData` | `GetAllShardingEntries` | | | `GetDatabaseInfoNamesWithTenantNamesAsync` | `GetShardingsWithTenantNamesAsync` | | -A properly that has been simplified +A properly that has been simplified. | Old name | new name | Notes | | ----------------- | ------------- | ----- | | `ShardingDatabaseProviders.Keys.ToArray()` | `PossibleDatabaseProviders()` | | -Other methods that haven't changed are listed below +Other `IShardingConnections` methods that haven't changed are listed below. | Old name | new name | Notes | | ----------------- | ------------- | ----- | -| `GetConnectionStringNames` | `GetConnectionStringNames` | see note1 | +| `GetConnectionStringNames` | `GetConnectionStringNames` | No change | + +## Move your AuthP 5 sharding entries to the AuthP 5 FileStore Cache + +Once your code complies with AuthP version 6 and **before you run your application** you need to move your sharding entries from the json file to the distributed FileStore Cache. + -Note1 END