20260320093000_AddChargingPileMapNodeIdHotfix.cs 3.52 KB
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Rcs.Infrastructure.DB.MsSql;

#nullable disable

namespace Rcs.Infrastructure.Migrations
{
    [DbContext(typeof(AppDbContext))]
    [Migration("20260320093000_AddChargingPileMapNodeIdHotfix")]
    public class AddChargingPileMapNodeIdHotfix : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                """
                DO $$
                BEGIN
                    IF EXISTS (
                        SELECT 1
                        FROM information_schema.tables
                        WHERE table_schema = current_schema()
                          AND table_name = 'charging_piles'
                    ) THEN
                        IF NOT EXISTS (
                            SELECT 1
                            FROM information_schema.columns
                            WHERE table_schema = current_schema()
                              AND table_name = 'charging_piles'
                              AND column_name = 'map_node_id'
                        ) THEN
                            ALTER TABLE charging_piles
                            ADD COLUMN map_node_id uuid;
                        END IF;

                        IF NOT EXISTS (
                            SELECT 1
                            FROM pg_indexes
                            WHERE schemaname = current_schema()
                              AND indexname = 'idx_charging_pile_map_node_id'
                        ) THEN
                            CREATE INDEX idx_charging_pile_map_node_id
                            ON charging_piles (map_node_id);
                        END IF;

                        IF EXISTS (
                            SELECT 1
                            FROM information_schema.tables
                            WHERE table_schema = current_schema()
                              AND table_name = 'map_nodes'
                        ) AND NOT EXISTS (
                            SELECT 1
                            FROM pg_constraint
                            WHERE conname = 'FK_charging_piles_map_nodes_map_node_id'
                        ) THEN
                            ALTER TABLE charging_piles
                            ADD CONSTRAINT "FK_charging_piles_map_nodes_map_node_id"
                            FOREIGN KEY (map_node_id)
                            REFERENCES map_nodes (node_id)
                            ON DELETE SET NULL;
                        END IF;
                    END IF;
                END
                $$;
                """);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                """
                DO $$
                BEGIN
                    IF EXISTS (
                        SELECT 1
                        FROM information_schema.tables
                        WHERE table_schema = current_schema()
                          AND table_name = 'charging_piles'
                    ) THEN
                        ALTER TABLE charging_piles
                        DROP CONSTRAINT IF EXISTS "FK_charging_piles_map_nodes_map_node_id";

                        DROP INDEX IF EXISTS idx_charging_pile_map_node_id;

                        ALTER TABLE charging_piles
                        DROP COLUMN IF EXISTS map_node_id;
                    END IF;
                END
                $$;
                """);
        }
    }
}