-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_fixed.sql
More file actions
116 lines (105 loc) · 4.26 KB
/
database_fixed.sql
File metadata and controls
116 lines (105 loc) · 4.26 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
-- UniShare Database Schema (Fixed)
CREATE DATABASE IF NOT EXISTS unishare;
USE unishare;
-- Users table
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
family_name VARCHAR(50) DEFAULT '',
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
specialty VARCHAR(100) DEFAULT '',
academic_level VARCHAR(10) DEFAULT 'L1',
profile_photo VARCHAR(255) DEFAULT 'default_avatar.png',
role ENUM('user', 'admin') DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP NULL,
INDEX idx_email (email),
INDEX idx_role (role)
);
-- Categories table (fixed - removed description column if it doesn't exist)
CREATE TABLE IF NOT EXISTS categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
slug VARCHAR(50) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Posts table
CREATE TABLE IF NOT EXISTS posts (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
category_id INT NULL,
specialty VARCHAR(100) DEFAULT '',
academic_level VARCHAR(10) DEFAULT '',
image_path VARCHAR(255) NULL,
file_path VARCHAR(255) NULL,
file_name VARCHAR(255) NULL,
likes INT DEFAULT 0,
views INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL,
INDEX idx_user (user_id),
INDEX idx_created (created_at),
INDEX idx_specialty (specialty)
);
-- Comments table
CREATE TABLE IF NOT EXISTS comments (
id INT AUTO_INCREMENT PRIMARY KEY,
post_id INT NOT NULL,
user_id INT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
INDEX idx_post (post_id),
INDEX idx_user (user_id)
);
-- Favorites table
CREATE TABLE IF NOT EXISTS favorites (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
post_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY unique_favorite (user_id, post_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
INDEX idx_user (user_id),
INDEX idx_post (post_id)
);
-- Sessions table for persistent token-based authentication
CREATE TABLE IF NOT EXISTS sessions (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
token VARCHAR(255) UNIQUE NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
INDEX idx_token (token),
INDEX idx_user (user_id),
INDEX idx_expires (expires_at)
);
-- Insert default categories (without description)
INSERT INTO categories (name, slug) VALUES
('Lecture Notes', 'lecture-notes'),
('Exam Schedules', 'exam-schedules'),
('Solutions', 'solutions'),
('Projects', 'projects'),
('Study Tips', 'study-tips'),
('Resources', 'resources');
-- Create test user (password: test123)
INSERT INTO users (username, family_name, email, password, specialty, academic_level, role)
VALUES ('TestUser', 'Test', 'test@test.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'Computer Science', 'L3', 'user');
-- Create admin user (password: admin123)
INSERT INTO users (username, family_name, email, password, specialty, academic_level, role)
VALUES ('Admin', 'UniShare', 'admin@unishare.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'Administration', 'M2', 'admin');
-- Create a second test user
INSERT INTO users (username, family_name, email, password, specialty, academic_level, role)
VALUES ('Student', 'User', 'student@unishare.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'Physics', 'L2', 'user');
-- Show success message
SELECT 'Database setup completed successfully!' as Status;
SELECT COUNT(*) as TotalUsers FROM users;
SELECT COUNT(*) as TotalCategories FROM categories;