using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace Tnsu.ApprovalRoute.Infrastructure.Persistence.Migrations { /// public partial class AddMultipleStepConditions : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "conditions_logic", table: "template_route_steps", type: "character varying(16)", maxLength: 16, nullable: false, defaultValue: "And"); migrationBuilder.CreateTable( name: "template_route_step_conditions", columns: table => new { id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), step_id = table.Column(type: "integer", nullable: false), order_no = table.Column(type: "integer", nullable: false), field_name = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), operation = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), value = table.Column(type: "character varying(256)", maxLength: 256, nullable: false) }, constraints: table => { table.PrimaryKey("pk_template_route_step_conditions", x => x.id); table.ForeignKey( name: "fk_template_route_step_conditions_template_route_steps_step_id", column: x => x.step_id, principalTable: "template_route_steps", principalColumn: "id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "ix_template_route_step_conditions_step_id_order_no", table: "template_route_step_conditions", columns: new[] { "step_id", "order_no" }); // Существующее одиночное условие шага (field_name/operation/value) переносим // как первое (и единственное) условие в новую таблицу — до удаления старых // колонок, иначе уже настроенные в деве/проде условия шагов молча пропадут. migrationBuilder.Sql( """ INSERT INTO template_route_step_conditions (step_id, order_no, field_name, operation, value) SELECT id, 0, field_name, operation, value FROM template_route_steps WHERE field_name IS NOT NULL AND operation IS NOT NULL AND value IS NOT NULL; """); migrationBuilder.DropColumn( name: "field_name", table: "template_route_steps"); migrationBuilder.DropColumn( name: "operation", table: "template_route_steps"); migrationBuilder.DropColumn( name: "value", table: "template_route_steps"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "field_name", table: "template_route_steps", type: "character varying(128)", maxLength: 128, nullable: true); migrationBuilder.AddColumn( name: "operation", table: "template_route_steps", type: "character varying(32)", maxLength: 32, nullable: true); migrationBuilder.AddColumn( name: "value", table: "template_route_steps", type: "character varying(256)", maxLength: 256, nullable: true); // Откат теряет условия сверх первого (по order_no) — старая схема хранила // только одно условие на шаг, второе и далее девать некуда. migrationBuilder.Sql( """ UPDATE template_route_steps s SET field_name = c.field_name, operation = c.operation, value = c.value FROM ( SELECT DISTINCT ON (step_id) step_id, field_name, operation, value FROM template_route_step_conditions ORDER BY step_id, order_no ) c WHERE c.step_id = s.id; """); migrationBuilder.DropTable( name: "template_route_step_conditions"); migrationBuilder.DropColumn( name: "conditions_logic", table: "template_route_steps"); } } }