Skip to content

Commit

Permalink
Created ConsoleApp to update sharding entries to AuthP format
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPSmith committed Aug 15, 2023
1 parent 782feda commit a889e29
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace AuthPermissions.AspNetCore.ShardingServices;
/// </summary>
public class GetSetShardingEntriesFileStoreCache : IGetSetShardingEntries
{
/// <summary>
/// This is the prefix for creating the key to a sharding entry
/// </summary>
public static string ShardingEntryPrefix = "ShardingEntry-";

/// <summary>
/// This contains the methods with are specific to a database provider
/// </summary>
Expand Down Expand Up @@ -242,8 +247,6 @@ public string FormConnectionString(string shardingEntryName)
//------------------------------------------------------
//private methods

private const string ShardingEntryPrefix = "ShardingEntry-";

private string FormShardingEntryKey(string shardingEntryName)
{
return ShardingEntryPrefix + shardingEntryName;
Expand Down
19 changes: 19 additions & 0 deletions ConsoleApp.AuthP6Upgrade/ConsoleApp.AuthP6Upgrade.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Net.DistributedFileStoreCache" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AuthPermissions.AspNetCore\AuthPermissions.AspNetCore.csproj" />
<ProjectReference Include="..\AuthPermissions.BaseCode\AuthPermissions.BaseCode.csproj" />
</ItemGroup>

</Project>
102 changes: 102 additions & 0 deletions ConsoleApp.AuthP6Upgrade/Program.cs
Original file line number Diff line number Diff line change
@@ -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<JsonFileFormat>(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<IDistributedFileStoreCacheClass>();

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<ShardingEntry> ShardingDatabases { get; set; }
}
15 changes: 9 additions & 6 deletions UpdateToVersion6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

0 comments on commit a889e29

Please sign in to comment.