aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Migrations/20200229103848_AddPostLocalId.cs
blob: 497b38a1f3101960a252f51f4bc81f90539eb40d (plain)
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
using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Timeline.Migrations
{
    public partial class AddPostLocalId : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

            migrationBuilder.AddColumn<long>(
                name: "current_post_local_id",
                table: "timelines",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.AddColumn<long>(
                name: "local_id",
                table: "timeline_posts",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.Sql(@"
UPDATE timeline_posts
SET local_id = (SELECT COUNT (*)
                FROM timeline_posts AS p
                WHERE p.timeline =  timeline_posts.timeline
                    AND p.id  <= timeline_posts.id);

UPDATE timelines
SET current_post_local_id = (SELECT COUNT (*)
                             FROM timeline_posts AS p
                             WHERE p.timeline =  timelines.id);
            ");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}