persistence/DD.Persistence.Database.Postgres/Migrations/20241222112828_ParameterDataMigration.cs

62 lines
2.8 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace DD.Persistence.Database.Postgres.Migrations.PersistencePostgres
{
/// <inheritdoc />
public partial class ParameterDataMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ParameterData",
columns: table => new
{
DiscriminatorId = table.Column<Guid>(type: "uuid", nullable: false, comment: "Дискриминатор системы"),
ParameterId = table.Column<int>(type: "integer", nullable: false, comment: "Id параметра")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "Временная отметка"),
Value = table.Column<string>(type: "varchar(256)", nullable: true, comment: "Значение параметра в виде строки")
},
constraints: table =>
{
table.PrimaryKey("PK_ParameterData", x => new { x.DiscriminatorId, x.ParameterId, x.Timestamp });
});
string sql;
for (int i = 1; i <= 25; i++)
{
sql = $"create table if not exists \"ParameterData{i}\" (like public.\"ParameterData\" including all) inherits (public.\"ParameterData\");";
migrationBuilder.Sql(sql);
}
//sql = "create or replace function partition_for_parameter_data() returns trigger as $$ " +
// "DECLARE " +
// " v_parition_name text; " +
// "BEGIN " +
// " v_parition_name := format( 'ParameterData%s', NEW.\"ParameterId\" / 1000 ); " +
// " execute 'INSERT INTO public.\"' || v_parition_name || '\" VALUES ( ($1).* )' USING NEW; " +
// " return NULL; " +
// "END; " +
// "$$ language plpgsql;";
//migrationBuilder.Sql(sql);
//sql = "create or replace trigger partition_parameter_data " +
// "before insert on public.\"ParameterData\" " +
// "for each row execute procedure public.partition_for_parameter_data();";
//migrationBuilder.Sql(sql);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ParameterData");
}
}
}