-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sql
44 lines (38 loc) · 1.37 KB
/
init.sql
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
DROP DATABASE IF EXISTS "MorsseDB";
CREATE DATABASE "MorsseDB";
\c MorsseDB;
DROP TYPE IF EXISTS "user_theme";
CREATE TYPE "user_theme" AS ENUM ('light', 'dark');
DROP TYPE IF EXISTS "lesson_type";
CREATE TYPE "lesson_type" AS ENUM ('send', 'receive');
DROP TABLE IF EXISTS "users";
CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY,
"email" VARCHAR(100) NOT NULL UNIQUE,
"password" VARCHAR(255) NOT NULL,
"name" VARCHAR(50) NOT NULL UNIQUE,
"reputation" INTEGER NOT NULL DEFAULT 0,
"wpm" SMALLINT NOT NULL DEFAULT 15,
"completion_count" INTEGER NOT NULL DEFAULT 0,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"edited_at" TIMESTAMP,
"theme" user_theme NOT NULL DEFAULT 'dark'
);
DROP TABLE IF EXISTS "lessons";
CREATE TABLE "lessons" (
"id" SERIAL PRIMARY KEY,
"title" VARCHAR(100) NOT NULL,
"text" VARCHAR(255) NOT NULL,
"message" VARCHAR(500) NOT NULL,
"type" lesson_type NOT NULL,
"completion_count" INTEGER NOT NULL DEFAULT 0,
"difficulty" SMALLINT NOT NULL,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"user_id" INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
);
DROP TABLE IF EXISTS "user_lesson";
CREATE TABLE "user_lesson" (
"user_id" INTEGER REFERENCES users(id) ON DELETE CASCADE,
"lesson_id" INTEGER REFERENCES lessons(id) ON DELETE CASCADE,
PRIMARY KEY(user_id, lesson_id)
);