init database design using neo4j

This commit is contained in:
2025-04-24 17:42:56 -06:00
parent ab8f1b9ec8
commit 275e6cb153
3 changed files with 282 additions and 27 deletions
+72 -27
View File
@@ -6,10 +6,8 @@ CREATE TABLE roles (
role VARCHAR(16) NOT NULL
);
CREATE TABLE tags (
tag_id INT PRIMARY KEY AUTO_INCREMENT,
tag VARCHAR(16) NOT NULL
);
INSERT INTO roles (role)
VALUES ('owner'), ('developer'), ('user');
CREATE TABLE user (
user_id INT PRIMARY KEY,
@@ -22,25 +20,34 @@ CREATE TABLE user (
is_active BOOLEAN DEFAULT FALSE,
profile_picture VARCHAR(256),
bio VARCHAR(256),
x_username VARCHAR(32),
fb_username VARCHAR(32),
ig_username VARCHAR(32),
website VARCHAR(64),
FOREIGN KEY (role_id) REFERENCES roles (role_id)
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)
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)
);
CREATE TABLE article (
article_id INT PRIMARY KEY AUTO_INCREMENT,
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,
@@ -49,17 +56,31 @@ CREATE TABLE article (
published_at TIMESTAMP DEFAULT NULL,
published BOOLEAN DEFAULT TRUE,
excerpt VARCHAR(256),
FOREIGN KEY (author_id) REFERENCES user(user_id)
FOREIGN KEY (author_id) REFERENCES user(user_id) ON DELETE CASCADE,
FOREIGN KEY (post_type) REFERENCES post_types(post_type_id)
);
CREATE TABLE article_tags (
article_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (article_id, tag_id),
FOREIGN KEY (article_id) REFERENCES article(article_id),
FOREIGN KEY (tag_id) REFERENCES tags(tag_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,
@@ -70,16 +91,40 @@ CREATE TABLE follows (
FOREIGN KEY (following_user_id) REFERENCES user(user_id) ON DELETE CASCADE
);
INSERT INTO roles (role)
VALUES ('owner'), ('admin'), ('contributor'), ('reader');
CREATE TABLE space_types (
space_type_id INT PRIMARY KEY AUTO_INCREMENT,
space_type VARCHAR(16)
);
INSERT INTO tags (tag)
VALUES ('linux'), ('web-dev'), ('raspberry-pi'), ('self-hosting'), ('devlog'), ('testing');
INSERT INTO space_types (space_type)
VALUES ('open', 'public', 'shared', 'private');
INSERT INTO user (user_id, username, email, password_hash, role_id, profile_picture)
VALUES (2025, 'quaxlyqueen', 'me@joshashton.dev', '057ba03d6c44104863dc7361fe4578965d1887360f90a0895882e58a6248fc86', 1, '2025.jpg');
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)
);
INSERT INTO article (author_id, title, excerpt, published_at)
VALUES (2025, 'Building vintagecoding.net', 'A devlog of the creation of vintagecoding.net and a test at the formatting of article cards and articles. Let the great experiment begin!', CURRENT_TIMESTAMP);
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
);
INSERT INTO article_tags (article_id, tag_id) VALUES (1, 2), (1, 4), (1, 5);
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
);