Skip to content

Commit a18d200

Browse files
committed
Add migration
1 parent a0eb924 commit a18d200

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

src/JoinRpg.Dal.Impl/Migrations/202106200922043_LastCommentDates.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace JoinRpg.Dal.Impl.Migrations
1+
namespace JoinRpg.Dal.Impl.Migrations
22
{
33
using System;
44
using System.Data.Entity.Migrations;
5-
5+
66
public partial class LastCommentDates : DbMigration
77
{
88
public override void Up()
@@ -16,8 +16,64 @@ public override void Up()
1616
CreateIndex("dbo.Claims", "LastVisibleMasterCommentBy_Id");
1717
AddForeignKey("dbo.Claims", "LastMasterCommentBy_Id", "dbo.Users", "UserId");
1818
AddForeignKey("dbo.Claims", "LastVisibleMasterCommentBy_Id", "dbo.Users", "UserId");
19+
20+
Sql(@"
21+
UPDATE Claims
22+
SET
23+
[LastPlayerCommentAt] = LastCommentTime
24+
FROM (
25+
SELECT
26+
Claims.ClaimId,
27+
ROW_NUMBER() OVER (partition by PlayerComment.CommentDiscussionId order by PlayerComment.LastEditTime desc) AS PC_NUM,
28+
PlayerComment.AuthorUserId,
29+
PlayerComment.LastEditTime AS LastCommentTime
30+
FROM [dbo].[Claims]
31+
INNER JOIN Comments PlayerComment ON
32+
PlayerComment.CommentDiscussionId = Claims.CommentDiscussionId
33+
WHERE PlayerComment.AuthorUserId = Claims.PlayerUserId
34+
) T
35+
WHERE PC_NUM = 1 AND T.ClaimId = Claims.ClaimId"); // Players comments
36+
37+
Sql(@"
38+
UPDATE Claims
39+
SET
40+
LastVisibleMasterCommentAt = LastCommentTime,
41+
LastVisibleMasterCommentBy_Id = AuthorUserId
42+
FROM (
43+
SELECT
44+
Claims.ClaimId,
45+
ROW_NUMBER() OVER (partition by C.CommentDiscussionId order by C.LastEditTime desc) AS PC_NUM,
46+
C.AuthorUserId,
47+
C.LastEditTime AS LastCommentTime
48+
FROM [dbo].[Claims]
49+
INNER JOIN Comments C ON
50+
C.CommentDiscussionId = Claims.CommentDiscussionId
51+
WHERE C.AuthorUserId <> Claims.PlayerUserId AND C.IsVisibleToPlayer = 1
52+
) T
53+
WHERE PC_NUM = 1 AND T.ClaimId = Claims.ClaimId
54+
");
55+
56+
57+
Sql(@"
58+
UPDATE Claims
59+
SET
60+
LastMasterCommentAt = LastCommentTime,
61+
LastMasterCommentBy_Id = AuthorUserId
62+
FROM (
63+
SELECT
64+
Claims.ClaimId,
65+
ROW_NUMBER() OVER (partition by C.CommentDiscussionId order by C.LastEditTime desc) AS PC_NUM,
66+
C.AuthorUserId,
67+
C.LastEditTime AS LastCommentTime
68+
FROM [dbo].[Claims]
69+
INNER JOIN Comments C ON
70+
C.CommentDiscussionId = Claims.CommentDiscussionId
71+
WHERE C.AuthorUserId <> Claims.PlayerUserId
72+
) T
73+
WHERE PC_NUM = 1 AND T.ClaimId = Claims.ClaimId
74+
");
1975
}
20-
76+
2177
public override void Down()
2278
{
2379
DropForeignKey("dbo.Claims", "LastVisibleMasterCommentBy_Id", "dbo.Users");

0 commit comments

Comments
 (0)