Repository navigation
Expand file tree
/
Copy pathdatabase_setup.sql
More file actions
200 lines (178 loc) · 6.89 KB
/
Copy pathdatabase_setup.sql
File metadata and controls
200 lines (178 loc) · 6.89 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
-- QuizMaster Database Setup
-- Run this script to create/update the database schema
-- Create users table with proper constraints
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role ENUM('user', 'admin') DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_username (username),
INDEX idx_email (email),
INDEX idx_role (role)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create quizzes table with proper constraints and foreign keys
CREATE TABLE IF NOT EXISTS quizzes (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_by INT NOT NULL,
creator_name VARCHAR(100) NOT NULL,
status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
admin_notes TEXT DEFAULT NULL,
approved_by INT DEFAULT NULL,
approved_at TIMESTAMP NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_status (status),
INDEX idx_created_by (created_by),
INDEX idx_approved_by (approved_by),
INDEX idx_created_at (created_at),
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (approved_by) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create questions table with proper constraints
CREATE TABLE IF NOT EXISTS questions (
id INT AUTO_INCREMENT PRIMARY KEY,
quiz_id INT NOT NULL,
question TEXT NOT NULL,
option_a VARCHAR(255) NOT NULL,
option_b VARCHAR(255) NOT NULL,
option_c VARCHAR(255) NOT NULL,
option_d VARCHAR(255) NOT NULL,
correct_option ENUM('A', 'B', 'C', 'D') NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_quiz_id (quiz_id),
FOREIGN KEY (quiz_id) REFERENCES quizzes(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create leaderboard table with proper constraints
CREATE TABLE IF NOT EXISTS leaderboard (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
username VARCHAR(100) NOT NULL,
quiz_id INT NOT NULL,
quiz_title VARCHAR(255) NOT NULL,
score INT NOT NULL DEFAULT 0,
total_questions INT NOT NULL DEFAULT 0,
percentage DECIMAL(5,2) NOT NULL DEFAULT 0.00,
completion_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
INDEX idx_quiz_id (quiz_id),
INDEX idx_percentage (percentage),
INDEX idx_completion_time (completion_time),
INDEX idx_score (score),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (quiz_id) REFERENCES quizzes(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Add email column to users if not exists (for future use)
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND column_name = 'email';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE users ADD COLUMN email VARCHAR(255) UNIQUE NOT NULL AFTER username',
'SELECT "Column email already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add password reset columns to users
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND column_name = 'reset_token';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE users ADD COLUMN reset_token VARCHAR(64) DEFAULT NULL',
'SELECT "Column reset_token already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND column_name = 'reset_expires';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE users ADD COLUMN reset_expires TIMESTAMP NULL DEFAULT NULL',
'SELECT "Column reset_expires already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add index for reset_token
SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND index_name = 'idx_reset_token';
SET @sql = IF(@index_exists = 0,
'ALTER TABLE users ADD INDEX idx_reset_token (reset_token)',
'SELECT "Index idx_reset_token already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add updated_at column to users if not exists
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND column_name = 'updated_at';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE users ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
'SELECT "Column updated_at already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add updated_at column to quizzes if not exists
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'updated_at';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
'SELECT "Column updated_at already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Ensure foreign keys are properly set (MySQL may ignore if they exist)
-- These will fail silently if constraints already exist
ALTER TABLE quizzes
ADD CONSTRAINT fk_quizzes_created_by
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE quizzes
ADD CONSTRAINT fk_quizzes_approved_by
FOREIGN KEY (approved_by) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE questions
ADD CONSTRAINT fk_questions_quiz_id
FOREIGN KEY (quiz_id) REFERENCES quizzes(id) ON DELETE CASCADE;
ALTER TABLE leaderboard
ADD CONSTRAINT fk_leaderboard_user_id
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE leaderboard
ADD CONSTRAINT fk_leaderboard_quiz_id
FOREIGN KEY (quiz_id) REFERENCES quizzes(id) ON DELETE CASCADE;
-- Create a default admin user if none exists (password: Admin@123 - change immediately!)
-- This is just for initial setup - remove or change in production!
INSERT IGNORE INTO users (username, email, password, role)
VALUES (
'admin',
'admin@quizmaster.local',
'$2y$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj/RK.PZvO.S', -- bcrypt hash of 'Admin@123'
'admin'
);