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); } /// /// Добавить партиционирование таблицы ParameterData (Wits - данные) /// /// private static void AddParameterDataPartitioning(this DatabaseFacade db) { var type = typeof(ParameterData); var tableAttribute = type.GetCustomAttribute(); if (tableAttribute is null) { return; } var sqlCreateHypertableString = $"SELECT create_hypertable('{tableAttribute.Name}'," + $"by_range('{nameof(ParameterData.Timestamp)}', INTERVAL '1 day'), if_not_exists => {true});"; db.ExecuteSqlRaw(sqlCreateHypertableString); var sqlCreateDimensionString = $"SELECT add_dimension('{tableAttribute.Name}'," + $"by_hash('{nameof(ParameterData.ParameterId)}', 1));"; db.ExecuteSqlRaw(sqlCreateDimensionString); //var sqlString = $"SELECT create_hypertable('{tableAttribute.Name}'," + // $"'{nameof(ParameterData.Timestamp)}'," + // $"'{nameof(ParameterData.ParameterId)}'," + // $"{sectionsNumber}," + // $"chunk_time_interval => INTERVAL '{chunkTimeInterval} day');"; //db.ExecuteSqlRaw(sqlString); } }