Skip to content

Commit

Permalink
setup database for efficient multi-user setup
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rbit committed Dec 30, 2023
1 parent 88480af commit fa69d6d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
15 changes: 14 additions & 1 deletion sql/base_seed.sql
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
61 changes: 24 additions & 37 deletions sql/setup.sql
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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);

0 comments on commit fa69d6d

Please sign in to comment.