Skip to content

Commit a889e29

Browse files
committed
Created ConsoleApp to update sharding entries to AuthP format
1 parent 782feda commit a889e29

File tree

4 files changed

+135
-8
lines changed

4 files changed

+135
-8
lines changed

AuthPermissions.AspNetCore/ShardingServices/GetSetShardingEntriesFileStoreCache.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ namespace AuthPermissions.AspNetCore.ShardingServices;
2020
/// </summary>
2121
public class GetSetShardingEntriesFileStoreCache : IGetSetShardingEntries
2222
{
23+
/// <summary>
24+
/// This is the prefix for creating the key to a sharding entry
25+
/// </summary>
26+
public static string ShardingEntryPrefix = "ShardingEntry-";
27+
2328
/// <summary>
2429
/// This contains the methods with are specific to a database provider
2530
/// </summary>
@@ -242,8 +247,6 @@ public string FormConnectionString(string shardingEntryName)
242247
//------------------------------------------------------
243248
//private methods
244249

245-
private const string ShardingEntryPrefix = "ShardingEntry-";
246-
247250
private string FormShardingEntryKey(string shardingEntryName)
248251
{
249252
return ShardingEntryPrefix + shardingEntryName;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Net.DistributedFileStoreCache" Version="2.0.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\AuthPermissions.AspNetCore\AuthPermissions.AspNetCore.csproj" />
16+
<ProjectReference Include="..\AuthPermissions.BaseCode\AuthPermissions.BaseCode.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) 2023 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
4+
using Net.DistributedFileStoreCache.SupportCode;
5+
using System.Text.Json;
6+
using AuthPermissions.AspNetCore.ShardingServices;
7+
using Net.DistributedFileStoreCache;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using System.Text.Encodings.Web;
10+
11+
namespace ConsoleApp.AuthP6Upgrade;
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
//The arguments needed are
17+
//1. The name of the json file holding the AuthP version 5 sharding entries, e.g. shardingsettings.Production.json
18+
//2. The name for the new FileStore Cache file, e.g. FileStoreCacheFile.Production.json
19+
//3. The filepath to the the json file. This can be a relative or absolute
20+
21+
22+
if (args.Length != 3)
23+
{
24+
Console.WriteLine("This app expects three arguments: ");
25+
Console.WriteLine(" 1. The filepath to the the json file. This can be a relative or absolute.");
26+
Console.WriteLine(" 2. The name of the json file holding the AuthP version 5 sharding entries.");
27+
Console.WriteLine(" e.g. shardingsettings.Production.json");
28+
Console.WriteLine(" 3. The name for the new FileStore Cache file used by AuthP version 6.");
29+
Console.WriteLine(" e.g. FileStoreCacheFile.Production.json");
30+
return;
31+
}
32+
33+
var filePathToJsonFilePath = Path.Combine(args[0], args[1]);
34+
35+
if (!File.Exists(filePathToJsonFilePath))
36+
{
37+
Console.WriteLine("No json file was found using the filepath and the json file name you provided.");
38+
Console.WriteLine("The full filePath you provided is");
39+
Console.WriteLine(filePathToJsonFilePath);
40+
return;
41+
}
42+
43+
var jsonString = File.ReadAllText(filePathToJsonFilePath);
44+
Console.WriteLine(jsonString);
45+
var shardingData = JsonSerializer.Deserialize<JsonFileFormat>(jsonString)?.ShardingDatabases;
46+
47+
if (shardingData == null || !shardingData.Any())
48+
{
49+
Console.WriteLine("There aren't any sharding entries in your json file to place the the FileStore cache.");
50+
Console.WriteLine("In this case the FileStore cache will start empty, which is what you want.");
51+
}
52+
53+
var breakDownCacheName = args[2].Split('.');
54+
if (!((breakDownCacheName.Length == 3 && breakDownCacheName[2] == "json")
55+
|| (breakDownCacheName.Length == 2 && breakDownCacheName[1] != "json")))
56+
{
57+
Console.WriteLine("The name of the FileStore Cache name doesn't have the correct format.");
58+
Console.WriteLine("The FileStore Cache should have three parts, e.g. FileStoreCacheFile.Production.json");
59+
Console.WriteLine(args[2]);
60+
return;
61+
}
62+
//Console.WriteLine(breakDownCacheName.ToString());
63+
64+
//Now use Net.DistributedFileStoreCache to add the
65+
string fileStoreName = null;
66+
var services = new ServiceCollection();
67+
services.AddDistributedFileStoreCache(options =>
68+
{
69+
options.WhichVersion = FileStoreCacheVersions.Class;
70+
options.PathToCacheFileDirectory = Path.GetFullPath(args[0]);
71+
options.FirstPartOfCacheFileName = breakDownCacheName[0];
72+
options.SecondPartOfCacheFileName = breakDownCacheName[1];
73+
74+
options.JsonSerializerForCacheFile = new JsonSerializerOptions
75+
{
76+
//This will make the json in the FileStore json file will be easier to read
77+
//BUT it will be a bit slower and take up more characters
78+
WriteIndented = true,
79+
//This makes unicode chars smaller - especially useful for FileStoreCacheVersions.Class
80+
//see https://github.com/JonPSmith/Net.DistributedFileStoreCache/wiki/Tips-on-making-your-cache-fast#class-version---already-has-unsaferelaxedjsonescaping
81+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
82+
};
83+
fileStoreName = options.FormCacheFileName();
84+
});
85+
var serviceProvider = services.BuildServiceProvider();
86+
var readWriteService = serviceProvider.GetRequiredService<IDistributedFileStoreCacheClass>();
87+
88+
foreach (var shardingEntry in shardingData)
89+
{
90+
var key = GetSetShardingEntriesFileStoreCache.ShardingEntryPrefix + shardingEntry.Name;
91+
readWriteService.SetClass(key, shardingEntry);
92+
Console.WriteLine($"Added the sharding entry with the name of {shardingEntry.Name} added to the FileStore");
93+
}
94+
95+
Console.WriteLine($"Successfully copied {shardingData.Count} sharding entry to the FileStore Cache called '{fileStoreName}'.");
96+
}
97+
}
98+
99+
public class JsonFileFormat
100+
{
101+
public List<ShardingEntry> ShardingDatabases { get; set; }
102+
}

UpdateToVersion6.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ Of course this creates more breaking changes, but the code will be easier to und
1414
1. Changing to the new IGetSetShardingEntries service.
1515
2. Updating the IGetSetShardingEntries method names
1616
3. Make sure that distributed FileStore Cache is registered
17-
4. Once it complies and **before you run your application**
18-
- Move your sharding entries from the json file to the distributed FileStore Cache
17+
4. Move your AuthP 5 sharding entries to the AuthP 5 FileStore Cache
1918

2019
The subsections below the items listed in the table of content.
2120

@@ -47,16 +46,20 @@ The table below gives the old and new.
4746
| `GetAllPossibleShardingData` | `GetAllShardingEntries` | |
4847
| `GetDatabaseInfoNamesWithTenantNamesAsync` | `GetShardingsWithTenantNamesAsync` | |
4948

50-
A properly that has been simplified
49+
A properly that has been simplified.
5150
| Old name | new name | Notes |
5251
| ----------------- | ------------- | ----- |
5352
| `ShardingDatabaseProviders.Keys.ToArray()` | `PossibleDatabaseProviders()` | |
5453

55-
Other methods that haven't changed are listed below
54+
Other `IShardingConnections` methods that haven't changed are listed below.
5655
| Old name | new name | Notes |
5756
| ----------------- | ------------- | ----- |
58-
| `GetConnectionStringNames` | `GetConnectionStringNames` | see note1 |
57+
| `GetConnectionStringNames` | `GetConnectionStringNames` | No change |
58+
59+
## Move your AuthP 5 sharding entries to the AuthP 5 FileStore Cache
60+
61+
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.
62+
5963

60-
Note1
6164

6265
END

0 commit comments

Comments
 (0)