20260323003506_Migration_20260323083440.cs 3.96 KB
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Rcs.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class Migration_20260323083440 : Migration
    {
        /// <inheritdoc />
        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 = 'map_edges'
                    ) THEN
                        IF NOT EXISTS (
                            SELECT 1
                            FROM information_schema.columns
                            WHERE table_schema = current_schema()
                              AND table_name = 'map_edges'
                              AND column_name = 'curve_type'
                        ) THEN
                            ALTER TABLE map_edges
                            ADD COLUMN curve_type integer NOT NULL DEFAULT 0;
                        END IF;

                        IF EXISTS (
                            SELECT 1
                            FROM information_schema.columns
                            WHERE table_schema = current_schema()
                              AND table_name = 'map_edges'
                              AND column_name = 'is_curve'
                        ) THEN
                            UPDATE map_edges
                            SET curve_type = CASE
                                WHEN is_curve = FALSE THEN 0
                                WHEN is_curve = TRUE
                                     AND radius IS NOT NULL
                                     AND center_x IS NOT NULL
                                     AND center_y IS NOT NULL THEN 1
                                ELSE 2
                            END;

                            ALTER TABLE map_edges
                            DROP COLUMN is_curve;
                        END IF;
                    END IF;
                END
                $$;
                """);
        }

        /// <inheritdoc />
        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 = 'map_edges'
                    ) THEN
                        IF NOT EXISTS (
                            SELECT 1
                            FROM information_schema.columns
                            WHERE table_schema = current_schema()
                              AND table_name = 'map_edges'
                              AND column_name = 'is_curve'
                        ) THEN
                            ALTER TABLE map_edges
                            ADD COLUMN is_curve boolean NOT NULL DEFAULT FALSE;
                        END IF;

                        IF EXISTS (
                            SELECT 1
                            FROM information_schema.columns
                            WHERE table_schema = current_schema()
                              AND table_name = 'map_edges'
                              AND column_name = 'curve_type'
                        ) THEN
                            UPDATE map_edges
                            SET is_curve = CASE
                                WHEN curve_type = 0 THEN FALSE
                                ELSE TRUE
                            END;

                            ALTER TABLE map_edges
                            DROP COLUMN curve_type;
                        END IF;
                    END IF;
                END
                $$;
                """);
        }
    }
}