-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.php
53 lines (43 loc) · 1.39 KB
/
migrate.php
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
<?php
require_once __DIR__ . "/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// tables: users, courses, exercises, assessments, course_user
$users_table_sql = "CREATE TABLE IF NOT EXISTS users(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
token VARCHAR(255)
);";
$courses_table_sql = "CREATE TABLE IF NOT EXISTS courses (
id VARCHAR(255) PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
language VARCHAR(255) NOT NULL
);";
$exercises_table_sql = "CREATE TABLE IF NOT EXISTS exercises (
id VARCHAR(255) PRIMARY KEY,
course_id VARCHAR(255) NOT NULL,
question VARCHAR(255) NOT NULL,
answer VARCHAR(255) NOT NULL,
FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE
);";
$assessment_table_sql = "CREATE TABLE IF NOT EXISTS assessment (
id VARCHAR(255) PRIMARY KEY,
user_id VARCHAR(255) NOT NULL,
course_id VARCHAR(255) NOT NULL,
score INT DEFAULT 0
);";
$dbname = $_ENV["DB_NAME"];
$host = $_ENV["DB_HOST"];
$user = $_ENV["DB_USER"];
$password = $_ENV["DB_PASSWORD"];
$db = new PDO(
"mysql:dbname=$dbname;host=$host",
$user,
$password
);
($db->prepare($users_table_sql))->execute();
($db->prepare($courses_table_sql))->execute();
($db->prepare($exercises_table_sql))->execute();