-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (66 loc) · 2.61 KB
/
Copy pathProgram.cs
File metadata and controls
81 lines (66 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Partitional.ConsoleApp;
using Sa.Data.PostgreSql;
using Sa.Partitional.PostgreSql;
Console.WriteLine("Hello, Partitional.PostgreSql!");
var connectionString = "Host=localhost;Username=postgres;Password=postgres;Database=postgres";
var hostBuilder = Host.CreateApplicationBuilder(args);
hostBuilder.Services
.AddSingleton<Tester>()
.AddLogging(c => c.AddConsole())
.AddSaPostgreSqlDataSource(builder => builder.WithConnectionString(connectionString))
.AddSaPartitional((sp, builder) =>
{
builder.AddSchema("public", schema =>
{
// Configure the 'customer' table
schema.AddTable("customer",
"id INT NOT NULL",
"country TEXT NOT NULL",
"city TEXT NOT NULL"
)
// Separate partitions in tables
.WithPartSeparator("_")
// Partition by 'country' and 'city' (if PartByRange is not specified, defaults to daily)
.PartByList("country", "city")
// Migration of partitions for each tenant by city
.AddMigration("RU", ["Moscow", "Samara"])
.AddMigration("USA", ["Alabama", "New York"])
.AddMigration("FR", ["Paris", "Lyon", "Bordeaux"]);
});
})
// Schedule for creating new partitions
.AddPartMigrationSchedule((sp, opts) =>
{
opts.AsBackgroundJob = true;
opts.ExecutionInterval = TimeSpan.FromHours(2);
opts.ForwardDays = 2;
})
// Schedule for removing old partitions
.AddPartCleanupSchedule((sp, opts) =>
{
opts.AsBackgroundJob = true;
opts.DropPartsAfterRetention = TimeSpan.FromDays(21);
});
using IHost host = hostBuilder.Build();
await host.Services.GetRequiredService<Tester>()
.ShouldRunTest(host.Services.GetRequiredService<IPartitionManager>());
await Task.Delay(2000);
namespace Partitional.ConsoleApp
{
public sealed class Tester(ILogger<Tester> logger, IPartRepository repository)
{
public async Task ShouldRunTest(IPartitionManager partition)
{
await partition.Migrate();
var parts = await repository.GetPartsToDate("customer", DateTime.Now.AddDays(3));
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation($"list of parts:{Environment.NewLine}{string.Join(Environment.NewLine, parts.Select(c => c.Id))}");
logger.LogInformation("Successfully: {Ok}", parts.Count > 0);
}
}
}
}