45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
|
using DD.Persistence.Database.Entity;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.Reflection;
|
|||
|
|
|||
|
namespace DD.Persistence.Database.Postgres.Extensions;
|
|||
|
public static class EFExtensionsPartitioning
|
|||
|
{
|
|||
|
public static void AddPartitioning(this DatabaseFacade db)
|
|||
|
{
|
|||
|
db.CreateTimescaledbExtension();
|
|||
|
db.AddParameterDataPartitioning();
|
|||
|
}
|
|||
|
|
|||
|
private static void CreateTimescaledbExtension(this DatabaseFacade db)
|
|||
|
{
|
|||
|
var sqlString = $"CREATE EXTENSION IF NOT EXISTS timescaledb;";
|
|||
|
db.ExecuteSqlRaw(sqlString);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавить партиционирование таблицы ParameterData (Wits - данные)
|
|||
|
/// </summary>
|
|||
|
/// <param name="db"></param>
|
|||
|
private static void AddParameterDataPartitioning(this DatabaseFacade db)
|
|||
|
{
|
|||
|
var type = typeof(ParameterData);
|
|||
|
var tableAttribute = type.GetCustomAttribute<TableAttribute>();
|
|||
|
if (tableAttribute is null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
const int sectionsNumber = 2;
|
|||
|
const int chunkTimeInterval = 5;
|
|||
|
var sqlString = $"SELECT create_hypertable({tableAttribute.Name}," +
|
|||
|
$"'{nameof(ParameterData.Timestamp)}'," +
|
|||
|
$"'{nameof(ParameterData.ParameterId)}'," +
|
|||
|
$"{sectionsNumber}," +
|
|||
|
$"chunk_time_interval => INTERVAL '{chunkTimeInterval} day');";
|
|||
|
db.ExecuteSqlRaw(sqlString);
|
|||
|
}
|
|||
|
}
|