20260304163000_AddRobotTaskHistoryTablesHotfix.cs 3.98 KB
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Rcs.Infrastructure.DB.MsSql;

#nullable disable

namespace Rcs.Infrastructure.Migrations
{
    [DbContext(typeof(AppDbContext))]
    [Migration("20260304163000_AddRobotTaskHistoryTablesHotfix")]
    public class AddRobotTaskHistoryTablesHotfix : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(@"
CREATE TABLE IF NOT EXISTS robot_task_histories (
    task_id uuid NOT NULL,
    task_code character varying(50) NOT NULL,
    task_name character varying(100),
    robot_code character varying(50),
    task_template_code character varying(50),
    begin_location_code character varying(50),
    end_location_code character varying(50),
    status integer NOT NULL,
    pause boolean NOT NULL DEFAULT FALSE,
    priority integer NOT NULL DEFAULT 99,
    source character varying(300),
    relation character varying(300),
    shelf_code character varying(50),
    error_info text,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone,
    archived_at timestamp without time zone NOT NULL,
    CONSTRAINT ""PK_robot_task_histories"" PRIMARY KEY (task_id)
);

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'ak_task_history_task_code'
    ) THEN
        ALTER TABLE robot_task_histories
            ADD CONSTRAINT ""ak_task_history_task_code"" UNIQUE (task_code);
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_task_history_archived_at ON robot_task_histories (archived_at);
CREATE INDEX IF NOT EXISTS idx_task_history_begin_location_code ON robot_task_histories (begin_location_code);
CREATE INDEX IF NOT EXISTS idx_task_history_end_location_code ON robot_task_histories (end_location_code);
CREATE INDEX IF NOT EXISTS idx_task_history_robot_code ON robot_task_histories (robot_code);
CREATE INDEX IF NOT EXISTS idx_task_history_status ON robot_task_histories (status);
CREATE INDEX IF NOT EXISTS idx_task_history_task_code ON robot_task_histories (task_code);

CREATE TABLE IF NOT EXISTS robot_sub_task_histories (
    sub_task_id uuid NOT NULL,
    task_code character varying(50) NOT NULL,
    robot_code character varying(50),
    begin_node_code character varying(50) NOT NULL,
    end_node_code character varying(50) NOT NULL,
    sequence integer NOT NULL,
    status integer NOT NULL,
    execution_count integer NOT NULL,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone,
    archived_at timestamp without time zone NOT NULL,
    CONSTRAINT ""PK_robot_sub_task_histories"" PRIMARY KEY (sub_task_id)
);

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'FK_robot_sub_task_histories_robot_task_histories_task_code'
    ) THEN
        ALTER TABLE robot_sub_task_histories
            ADD CONSTRAINT ""FK_robot_sub_task_histories_robot_task_histories_task_code""
            FOREIGN KEY (task_code)
            REFERENCES robot_task_histories(task_code)
            ON DELETE CASCADE;
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_sub_task_history_archived_at ON robot_sub_task_histories (archived_at);
CREATE INDEX IF NOT EXISTS idx_sub_task_history_begin_node_code ON robot_sub_task_histories (begin_node_code);
CREATE INDEX IF NOT EXISTS idx_sub_task_history_end_node_code ON robot_sub_task_histories (end_node_code);
CREATE INDEX IF NOT EXISTS idx_sub_task_history_robot_code ON robot_sub_task_histories (robot_code);
CREATE INDEX IF NOT EXISTS idx_sub_task_history_task_code ON robot_sub_task_histories (task_code);
CREATE INDEX IF NOT EXISTS idx_sub_task_history_task_sequence ON robot_sub_task_histories (task_code, sequence);
");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(@"
DROP TABLE IF EXISTS robot_sub_task_histories;
DROP TABLE IF EXISTS robot_task_histories;
");
        }
    }
}