using System; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace DD.Persistence.Database.Postgres.Migrations.PersistencePostgres { /// public partial class ParameterDataMigration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "ParameterData", columns: table => new { DiscriminatorId = table.Column(type: "uuid", nullable: false, comment: "Дискриминатор системы"), ParameterId = table.Column(type: "integer", nullable: false, comment: "Id параметра") .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Timestamp = table.Column(type: "timestamp with time zone", nullable: false, comment: "Временная отметка"), Value = table.Column(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); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "ParameterData"); } } }