CREATE DATABASE vintagecoding; USE vintagecoding; CREATE TABLE roles ( role_id INT PRIMARY KEY AUTO_INCREMENT, role VARCHAR(16) NOT NULL ); INSERT INTO roles (role) VALUES ('owner'), ('developer'), ('user'); CREATE TABLE user ( user_id INT PRIMARY KEY, username VARCHAR(32) NOT NULL UNIQUE, email VARCHAR(64) NOT NULL UNIQUE, password_hash VARCHAR(256) NOT NULL, role_id INT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, last_active TIMESTAMP DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, is_active BOOLEAN DEFAULT FALSE, profile_picture VARCHAR(256), bio VARCHAR(256), website VARCHAR(64), FOREIGN KEY (role_id) REFERENCES roles (role_id) ON DELETE CASCADE ); INSERT INTO user (user_id, username, email, password_hash, role_id, profile_picture) VALUES (1, 'quaxlyqueen', 'me@joshashton.dev', '057ba03d6c44104863dc7361fe4578965d1887360f90a0895882e58a6248fc86', 1, '2025.jpg'); CREATE TABLE remember_user ( token VARCHAR(128) PRIMARY KEY, user_id INT NOT NULL, remote_addr VARCHAR(64) NOT NULL, http_forward VARCHAR(64), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES user (user_id) ON DELETE CASCADE ); -- TODO: Need cron job to auto-delete after 30 days of inactivity CREATE TABLE post_types ( post_type_id INT PRIMARY KEY AUTO_INCREMENT, post_type VARCHAR(16) ); INSERT INTO post_types (post_type) VALUES ('article'), ('moment'), ('thought'); CREATE TABLE post ( post_id INT PRIMARY KEY, author_id INT NOT NULL, post_type INT NOT NULL, title VARCHAR(26) NOT NULL, slug VARCHAR(32), read_count INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, published_at TIMESTAMP DEFAULT NULL, published BOOLEAN DEFAULT TRUE, excerpt VARCHAR(256), FOREIGN KEY (author_id) REFERENCES user(user_id) ON DELETE CASCADE, FOREIGN KEY (post_type) REFERENCES post_types(post_type_id) ); INSERT INTO post (author_id, post_type, title, excerpt, published_at) VALUES (1, 1, 'Building vintagecoding.net', 'A devlog of the creation of vintagecoding.net and a test at the formatting of post cards and posts. Let the great experiment begin!', CURRENT_TIMESTAMP); CREATE TABLE categories ( category_id INT PRIMARY KEY AUTO_INCREMENT, category VARCHAR(16) NOT NULL ); INSERT INTO categories (category) VALUES ('web-dev'), ('devlog'); CREATE TABLE post_categories ( post_id INT NOT NULL, category_id INT NOT NULL, PRIMARY KEY (post_id, category_id), FOREIGN KEY (post_id) REFERENCES post(post_id) ON DELETE CASCADE, FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE ); INSERT INTO post_tags (post_id, tag_id) VALUES (1, 1), (1, 2); CREATE TABLE follows ( follower_user_id INT NOT NULL, following_user_id INT NOT NULL, notify_user BOOLEAN DEFAULT FALSE, followed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (follower_user_id, following_user_id), FOREIGN KEY (follower_user_id) REFERENCES user(user_id) ON DELETE CASCADE, FOREIGN KEY (following_user_id) REFERENCES user(user_id) ON DELETE CASCADE ); CREATE TABLE space_types ( space_type_id INT PRIMARY KEY AUTO_INCREMENT, space_type VARCHAR(16) ); INSERT INTO space_types (space_type) VALUES ('open', 'public', 'shared', 'private'); CREATE TABLE spaces ( space_id INT PRIMARY KEY AUTO_INCREMENT, space_type INT NOT NULL, name VARCHAR(64) NOT NULL, slug VARCHAR(64) UNIQUE, description TEXT, parent_space_id INT DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (parent_space_id) REFERENCES spaces(space_id) ON DELETE SET NULL, FOREIGN KEY (space_type) REFERENCES space_types(space_type_id) ); CREATE TABLE space_posts ( post_id INT NOT NULL, space_id INT NOT NULL, PRIMARY KEY (post_id, space_id), FOREIGN KEY (post_id) REFERENCES post(post_id) ON DELETE CASCADE, FOREIGN KEY (space_id) REFERENCES spaces(space_id) ON DELETE CASCADE ); CREATE TABLE space_members ( space_id INT NOT NULL, user_id INT NOT NULL, added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (space_id, user_id), FOREIGN KEY (space_id) REFERENCES spaces(space_id) ON DELETE CASCADE, FOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE );