Repository navigation
Expand file tree
/
Copy pathdatabase-reset.js
More file actions
363 lines (310 loc) · 14.8 KB
/
Copy pathdatabase-reset.js
File metadata and controls
363 lines (310 loc) · 14.8 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const mysql = require('mysql2/promise');
const { faker } = require('@faker-js/faker');
const pool = mysql.createPool({
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'root',
database: process.env.DB_NAME || 'university',
password: process.env.DB_PASSWORD || 'password',
port: process.env.DB_PORT || 3306,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
function generateTempPassword(length = 10) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%&*';
let password = '';
for (let i = 0; i < length; i++) {
password += chars.charAt(Math.floor(Math.random() * chars.length));
}
return password;
}
async function generateFakeData(connection) {
const departments = ['Computer Science', 'Mathematics', 'Physics', 'Business', 'Arts', 'Engineering', 'Biology', 'Chemistry'];
const locations = ['Building A', 'Building B', 'Building C', 'Building D', 'Building E', 'Science Wing', 'Tech Center', 'Main Campus'];
for (let i = 0; i < departments.length; i++) {
await connection.query(
'INSERT INTO DEPARTMENT (department_id, name, location) VALUES (?, ?, ?)',
[i + 1, departments[i], locations[i]]
);
}
const [deptRows] = await connection.query('SELECT department_id FROM DEPARTMENT');
const departmentIds = deptRows.map(row => row.department_id);
const professorCount = 15;
for (let i = 0; i < professorCount; i++) {
const deptId = departmentIds[Math.floor(Math.random() * departmentIds.length)];
const name = faker.person.fullName();
const email = faker.internet.email();
await connection.query(
'INSERT INTO PROFESSOR (professor_id, name, email) VALUES (?, ?, ?)',
[i + 1, name, email]
);
await connection.query(
'INSERT INTO PROFESSOR_ACCOUNT (professor_id, password, account_type) VALUES (?, ?, ?)',
[i + 1, generateTempPassword(), 'regular']
);
await connection.query(
'INSERT INTO PROFESSOR_DEPARTMENT (professor_id, department_id) VALUES (?, ?)',
[i + 1, deptId]
);
}
const professorIds = Array.from({ length: professorCount }, (_, i) => i + 1);
const testName = 'Test Student';
const testEmail = 'test@student.com';
const testDob = '2000-01-01';
const testPassword = 'password123';
const testRoleId = 1;
const testStudentId = 1;
const testDeptId = departmentIds[0];
await connection.query(
'INSERT INTO STUDENT (student_id, name, email, dob, advisor_id, role_id) VALUES (?, ?, ?, ?, ?, ?)',
[testStudentId, testName, testEmail, testDob, null, testRoleId]
);
await connection.query(
'INSERT INTO STUDENT_ACCOUNT (student_id, password, account_type) VALUES (?, ?, ?)',
[testStudentId, testPassword, 'regular']
);
await connection.query(
'INSERT INTO STUDENT_DEPARTMENT (student_id, department_id) VALUES (?, ?)',
[testStudentId, testDeptId]
);
const studentCount = 100;
for (let i = 1; i < studentCount; i++) {
const name = faker.person.fullName();
const email = faker.internet.email();
const dob = faker.date.past({ years: 20, refDate: new Date(2002, 0, 1) }).toISOString().split('T')[0];
const roleId = Math.random() < 0.9 ? 1 : 2;
const advisorId = professorIds[Math.floor(Math.random() * professorIds.length)];
await connection.query(
'INSERT INTO STUDENT (student_id, name, email, dob, advisor_id, role_id) VALUES (?, ?, ?, ?, ?, ?)',
[i + 1, name, email, dob, null, roleId]
);
await connection.query(
'INSERT INTO STUDENT_ACCOUNT (student_id, password, account_type) VALUES (?, ?, ?)',
[i + 1, generateTempPassword(), 'regular']
);
const deptId = departmentIds[Math.floor(Math.random() * departmentIds.length)];
await connection.query(
'INSERT INTO STUDENT_DEPARTMENT (student_id, department_id) VALUES (?, ?)',
[i + 1, deptId]
);
}
const studentIds = Array.from({ length: studentCount }, (_, i) => i + 1);
const studentAdvisorIds = [];
for (let i = 1; i <= studentCount; i++) {
const [roleCheck] = await connection.query('SELECT role_id FROM STUDENT WHERE student_id = ?', [i]);
if (roleCheck[0] && roleCheck[0].role_id === 2) {
studentAdvisorIds.push(i);
}
}
if (studentAdvisorIds.length === 0) {
await connection.query('UPDATE STUDENT SET role_id = 2 WHERE student_id = 2');
studentAdvisorIds.push(2);
}
for (let i = 1; i <= studentCount; i++) {
if (studentAdvisorIds.includes(i)) continue;
const advisorId = studentAdvisorIds[Math.floor(Math.random() * studentAdvisorIds.length)];
await connection.query('UPDATE STUDENT SET advisor_id = ? WHERE student_id = ?', [advisorId, i]);
}
const courseSubjects = [
'Introduction to Programming', 'Data Structures', 'Algorithms', 'Database Systems', 'Web Development',
'Calculus I', 'Calculus II', 'Linear Algebra', 'Statistics', 'Discrete Mathematics',
'Physics I', 'Physics II', 'Quantum Mechanics', 'Thermodynamics', 'Electromagnetism',
'Business Management', 'Marketing', 'Finance', 'Economics', 'Accounting',
'Art History', 'Drawing', 'Painting', 'Sculpture', 'Digital Art',
'Software Engineering', 'Machine Learning', 'Artificial Intelligence', 'Computer Networks', 'Cybersecurity'
];
const courseCount = 30;
for (let i = 0; i < courseCount; i++) {
const courseId = i + 1;
const courseName = courseSubjects[i % courseSubjects.length] + (i >= courseSubjects.length ? ` ${Math.floor(i / courseSubjects.length) + 2}` : '');
const credits = Math.floor(Math.random() * 5) + 2;
const seatsAvailable = 15 + Math.floor(Math.random() * 35);
const profId = professorIds[Math.floor(Math.random() * professorIds.length)];
await connection.query(
'INSERT INTO COURSE (course_id, course_name, credits, seats_available, professor_id) VALUES (?, ?, ?, ?, ?)',
[courseId, courseName, credits, seatsAvailable, profId]
);
await connection.query(
'INSERT INTO teaches (professor_id, course_id) VALUES (?, ?)',
[profId, courseId]
);
}
const courseIds = Array.from({ length: courseCount }, (_, i) => i + 1);
const enrollmentCount = 200;
let successfulEnrollments = 0;
for (let i = 0; i < enrollmentCount; i++) {
const studentId = studentIds[Math.floor(Math.random() * studentIds.length)];
const courseId = courseIds[Math.floor(Math.random() * courseIds.length)];
const [exists] = await connection.query(
'SELECT COUNT(*) as count FROM ENROLLMENT WHERE student_id = ? AND course_id = ?',
[studentId, courseId]
);
if (exists[0].count > 0) continue;
const [seatCheck] = await connection.query(
'SELECT seats_available, (SELECT COUNT(*) FROM ENROLLMENT WHERE course_id = ?) as enrolled FROM COURSE WHERE course_id = ?',
[courseId, courseId]
);
if (seatCheck[0].enrolled >= seatCheck[0].seats_available) continue;
const grade = Math.random() < 0.7 ? ['A', 'B', 'C', 'D', 'F'][Math.floor(Math.random() * 5)] : null;
await connection.query(
'INSERT INTO ENROLLMENT (student_id, course_id, grade) VALUES (?, ?, ?)',
[studentId, courseId, grade]
);
await connection.query(
'UPDATE COURSE SET seats_available = GREATEST(seats_available - 1, 0) WHERE course_id = ?',
[courseId]
);
successfulEnrollments++;
}
await connection.query(
'INSERT INTO PROFESSOR (name, email) VALUES (?, ?)',
['Test Admin', 'admin@university.com']
);
const [adminResult] = await connection.query('SELECT LAST_INSERT_ID() as id');
const adminProfessorId = adminResult[0].id;
await connection.query(
'INSERT INTO PROFESSOR_ACCOUNT (professor_id, password, account_type) VALUES (?, ?, ?)',
[adminProfessorId, 'admin123', 'admin']
);
console.log('\nDatabase reset and dummy data generation completed successfully!');
console.log('Summary of generated data:');
console.log(` - ${departments.length} Departments`);
console.log(` - ${professorCount} Professors with accounts`);
console.log(` - ${studentCount} Students with accounts`);
console.log(` - ${courseCount} Courses`);
console.log(` - ${successfulEnrollments} Enrollments`);
console.log('\nTest Login Credentials:');
console.log(' Student: test@student.com / password123');
console.log(' Admin: admin@university.com / admin123');
console.log(' Professor: Any generated professor email / check db for password');
}
function initResetRoutes(app) {
app.post('/api/reset-database', async (req, res) => {
const connection = await pool.getConnection();
try {
await connection.beginTransaction();
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
const tables = [
'teaches', 'advises', 'ENROLLMENT', 'STUDENT_ACCOUNT', 'PROFESSOR_ACCOUNT',
'STUDENT_DEPARTMENT', 'PROFESSOR_DEPARTMENT', 'COURSE', 'STUDENT', 'PROFESSOR',
'DEPARTMENT', 'ROLE'
];
for (const table of tables) {
await connection.query(`DROP TABLE IF EXISTS ${table}`);
}
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
const createTablesSQL = [
`CREATE TABLE ROLE (
role_id INT AUTO_INCREMENT PRIMARY KEY,
role_name VARCHAR(20) NOT NULL
)`,
`CREATE TABLE DEPARTMENT (
department_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
location VARCHAR(100)
)`,
`CREATE TABLE PROFESSOR (
professor_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
)`,
`CREATE TABLE PROFESSOR_ACCOUNT (
account_id INT AUTO_INCREMENT PRIMARY KEY,
professor_id INT NOT NULL,
password VARCHAR(100) NOT NULL,
account_type VARCHAR(20),
FOREIGN KEY (professor_id) REFERENCES PROFESSOR(professor_id)
)`,
`CREATE TABLE PROFESSOR_DEPARTMENT (
professor_id INT NOT NULL,
department_id INT NOT NULL,
PRIMARY KEY (professor_id, department_id),
FOREIGN KEY (professor_id) REFERENCES PROFESSOR(professor_id),
FOREIGN KEY (department_id) REFERENCES DEPARTMENT(department_id)
)`,
`CREATE TABLE COURSE (
course_id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL,
credits INT,
seats_available INT,
professor_id INT,
FOREIGN KEY (professor_id) REFERENCES PROFESSOR(professor_id)
)`,
`CREATE TABLE STUDENT (
student_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
dob DATE,
advisor_id INT,
role_id INT,
FOREIGN KEY (role_id) REFERENCES ROLE(role_id)
)`,
`CREATE TABLE STUDENT_ACCOUNT (
account_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
password VARCHAR(100) NOT NULL,
account_type VARCHAR(20),
FOREIGN KEY (student_id) REFERENCES STUDENT(student_id)
)`,
`CREATE TABLE STUDENT_DEPARTMENT (
student_id INT NOT NULL,
department_id INT NOT NULL,
PRIMARY KEY (student_id, department_id),
FOREIGN KEY (student_id) REFERENCES STUDENT(student_id),
FOREIGN KEY (department_id) REFERENCES DEPARTMENT(department_id)
)`,
`CREATE TABLE ENROLLMENT (
enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
course_id INT NOT NULL,
grade VARCHAR(2),
FOREIGN KEY (student_id) REFERENCES STUDENT(student_id),
FOREIGN KEY (course_id) REFERENCES COURSE(course_id)
)`,
`CREATE TABLE teaches (
professor_id INT NOT NULL,
course_id INT NOT NULL,
PRIMARY KEY (professor_id, course_id),
FOREIGN KEY (professor_id) REFERENCES PROFESSOR(professor_id),
FOREIGN KEY (course_id) REFERENCES COURSE(course_id)
)`,
`CREATE TABLE advises (
professor_id INT NOT NULL,
student_id INT NOT NULL,
PRIMARY KEY (professor_id, student_id),
FOREIGN KEY (professor_id) REFERENCES PROFESSOR(professor_id),
FOREIGN KEY (student_id) REFERENCES STUDENT(student_id)
)`
];
for (const stmt of createTablesSQL) {
await connection.query(stmt);
}
await connection.query(`
INSERT INTO ROLE (role_id, role_name)
VALUES
(1, 'student'),
(2, 'student_advisor'),
(3, 'professor'),
(4, 'administrative_staff')
`);
await generateFakeData(connection);
await connection.query(`
ALTER TABLE STUDENT
ADD CONSTRAINT fk_student_advisor
FOREIGN KEY (advisor_id) REFERENCES STUDENT(student_id)
`);
await connection.commit();
res.json({
success: true,
message: 'Database reset and dummy data generation completed successfully!'
});
} catch (error) {
await connection.rollback();
console.error('Error during database reset and data generation:', error);
res.status(500).json({ success: false, message: error.message });
} finally {
connection.release();
}
});
}
module.exports = { initResetRoutes };