20260320093000_AddChargingPileMapNodeIdHotfix.cs
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
$$;
""");
}
}
}