diff --git a/sql/base_seed.sql b/sql/base_seed.sql index a5dde3d..37bcb51 100644 --- a/sql/base_seed.sql +++ b/sql/base_seed.sql @@ -1,7 +1,20 @@ INSERT OR IGNORE INTO users (user_id, github_id, username, email, avatar_url) VALUES (1, '81222943', 'f0rbit', 'dev@forbit.dev', 'https://avatars.githubusercontent.com/u/81222943?v=4'); --- Insert data into 'posts' table if it's empty +INSERT OR IGNORE INTO categories (owner_id, name, parent) VALUES + (1, 'coding', 'root'), + (1, 'learning', 'coding'), + (1, 'devlog', 'coding'), + (1, 'gamedev', 'devlog'), + (1, 'webdev', 'devlog'), + (1, 'code-story', 'coding'), + (1, 'hobbies', 'root'), + (1, 'photography', 'hobbies'), + (1, 'painting', 'hobbies'), + (1, 'hiking', 'hobbies'), + (1, 'story', 'root'), + (1, 'advice', 'root'); + INSERT OR IGNORE INTO posts (slug, author_id, title, content, category) VALUES ('test-post', 1, 'test', 'this is a test post, first post.', 'coding'); diff --git a/sql/setup.sql b/sql/setup.sql index a712e72..44ed896 100644 --- a/sql/setup.sql +++ b/sql/setup.sql @@ -1,25 +1,26 @@ -- setup.sql +-- Create 'users' table if it doesn't exist +CREATE TABLE IF NOT EXISTS users ( + user_id INTEGER PRIMARY KEY AUTOINCREMENT, + github_id INTEGER NOT NULL UNIQUE, + username VARCHAR(255) NOT NULL, + email VARCHAR(255), + avatar_url VARCHAR(255), + -- Add other user-related fields as needed + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + -- Create 'categories' table if it doesn't exist CREATE TABLE IF NOT EXISTS categories ( + owner_id INTEGER NOT NULL, name VARCHAR(15) PRIMARY KEY, - parent VARCHAR(15) -); + parent VARCHAR(15), --- Insert data into 'categories' table if it's empty -INSERT OR IGNORE INTO categories (name, parent) VALUES - ('coding', 'root'), - ('learning', 'coding'), - ('devlog', 'coding'), - ('gamedev', 'devlog'), - ('webdev', 'devlog'), - ('code-story', 'coding'), - ('hobbies', 'root'), - ('photography', 'hobbies'), - ('painting', 'hobbies'), - ('hiking', 'hobbies'), - ('story', 'root'), - ('advice', 'root'); + FOREIGN KEY (owner_id) REFERENCES users(user_id), + UNIQUE(owner_id, name) +); -- Create 'posts' table if it doesn't exist CREATE TABLE IF NOT EXISTS posts ( @@ -32,27 +33,13 @@ CREATE TABLE IF NOT EXISTS posts ( archived BOOLEAN DEFAULT 0, publish_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, --- Create 'users' table if it doesn't exist -CREATE TABLE IF NOT EXISTS users ( - user_id INTEGER PRIMARY KEY AUTOINCREMENT, - github_id INTEGER NOT NULL UNIQUE, - username VARCHAR(255) NOT NULL, - email VARCHAR(255), - avatar_url VARCHAR(255), - -- Add other user-related fields as needed - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + FOREIGN KEY (author_id) REFERENCES users(user_id), + UNIQUE(author_id, slug) ); --- Add 'sessions' table -CREATE TABLE IF NOT EXISTS sessions ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER, - session_token TEXT NOT NULL UNIQUE, - expires_at TIMESTAMP NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY(user_id) REFERENCES users(id) -); +-- create indexes +CREATE INDEX IF NOT EXISTS idx_categories_user ON categories(owner_id); +CREATE INDEX IF NOT EXISTS idx_user_posts ON posts(author_id); +