-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.php
85 lines (68 loc) · 1.79 KB
/
migration.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
use Framework\Config;
require_once "./vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$config = Config::getEnv();
$connection = new PDO(
"mysql:host={$config['host']};dbname={$config['dbname']}",
$config["user"],
$config["password"]
);
$sql_create_users_table =
"CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
password VARCHAR(255),
created_at INT,
updated_at INT
);
";
$sql_create_threads_table =
"CREATE TABLE IF NOT EXISTS threads (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
title VARCHAR(255),
body TEXT,
upvotes INT,
downvotes INT,
created_at INT,
updated_at INT,
FOREIGN KEY (user_id) REFERENCES users(id)
);
";
$sql_create_replies_table =
"CREATE TABLE IF NOT EXISTS replies (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
thread_id INT,
body TEXT,
upvotes INT,
downvotes INT,
created_at INT,
updated_at INT,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (thread_id) REFERENCES threads(id)
);
";
$sql_create_moderators_table =
"CREATE TABLE IF NOT EXISTS moderators (
user_id INT,
created_at INT,
updated_at INT,
FOREIGN KEY (user_id) REFERENCES users(id)
);
";
$stmt = $connection->prepare($sql_create_users_table);
$stmt->execute();
echo "user table created ..." . PHP_EOL;
$stmt = $connection->prepare($sql_create_threads_table);
$stmt->execute();
echo "threads table created ..." . PHP_EOL;
$stmt = $connection->prepare($sql_create_replies_table);
$stmt->execute();
echo "replies table created ..." . PHP_EOL;
$stmt = $connection->prepare($sql_create_moderators_table);
$stmt->execute();
echo "moderators table created ..." . PHP_EOL;