Add unified posts database groundwork (#2)
Adds self-referencing post and post-vote tables, generated schema models, and an idempotent snapshot migration that preserves the legacy tables during the staged application cutover. Co-authored-by: codegirl-007 <s.raide@gmail.com>
This commit was merged in pull request #2.
This commit is contained in:
+32
@@ -42,6 +42,38 @@ CREATE TABLE IF NOT EXISTS answers (
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
id TEXT PRIMARY KEY,
|
||||
parent_id TEXT REFERENCES posts(id) ON DELETE CASCADE,
|
||||
author_id TEXT NOT NULL REFERENCES users(id),
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
body TEXT NOT NULL,
|
||||
city TEXT NOT NULL DEFAULT '',
|
||||
post_date TEXT NOT NULL DEFAULT '',
|
||||
hidden INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
CHECK (
|
||||
(parent_id IS NULL AND title <> '' AND post_date <> '')
|
||||
OR
|
||||
(parent_id IS NOT NULL AND title = '' AND city = '' AND post_date = '' AND hidden = 0)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_posts_parent_created
|
||||
ON posts(parent_id, created_at, id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_posts_root_date
|
||||
ON posts(post_date, hidden)
|
||||
WHERE parent_id IS NULL;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS post_votes (
|
||||
user_id TEXT NOT NULL REFERENCES users(id),
|
||||
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
||||
value INTEGER NOT NULL CHECK (value IN (-1, 1)),
|
||||
PRIMARY KEY (user_id, post_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token TEXT PRIMARY KEY,
|
||||
data BYTEA NOT NULL,
|
||||
|
||||
Reference in New Issue
Block a user