aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Migrations/20200808071611_UserAddUniqueId.cs
blob: 651a2b0518edc52f2bf71f25b4f35b76037de4ce (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
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.EntityFrameworkCore.Migrations;

namespace Timeline.Migrations
{
    public partial class UserAddUniqueId : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
@"
PRAGMA foreign_keys=OFF;

BEGIN TRANSACTION;

CREATE TABLE new_users (
    id       INTEGER NOT NULL
                     CONSTRAINT PK_users PRIMARY KEY AUTOINCREMENT,
	unique_id TEXT NOT NULL DEFAULT (lower(hex(randomblob(16)))),
    username TEXT    NOT NULL,
    password TEXT    NOT NULL,
    roles    TEXT    NOT NULL,
    version  INTEGER NOT NULL
                     DEFAULT 0,
    nickname TEXT
);

INSERT INTO new_users (id, username, password, roles, version, nickname)
	SELECT id, username, password, roles, version, nickname FROM users;
	
DROP TABLE users;

ALTER TABLE new_users
	RENAME TO users; 

CREATE UNIQUE INDEX IX_users_username ON users (
    username
);

PRAGMA foreign_key_check;

COMMIT TRANSACTION;

PRAGMA foreign_keys=ON;
"
    , true);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "unique_id",
                table: "users");
        }
    }
}