38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
/**
|
|
* Adds likes on comments, as the `comment_likes` table and a `like_count` on
|
|
* each comment that comment-like-service keeps in step, so that listing
|
|
* comments doesn't count their likes.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { DatabaseSync } from "node:sqlite";
|
|
|
|
/**
|
|
* Run once by `runMigrations`. Existing comments start with no likes, and a
|
|
* database made from schema.sql already has both.
|
|
*/
|
|
export function up(db: DatabaseSync): void {
|
|
const commentCols = db.prepare(`PRAGMA table_info(comments);`).all() as {
|
|
name: string;
|
|
}[];
|
|
if (!commentCols.some((c) => c.name === "like_count")) {
|
|
db.exec(
|
|
`ALTER TABLE comments ADD COLUMN like_count INTEGER NOT NULL DEFAULT 0;`,
|
|
);
|
|
}
|
|
|
|
db.exec(`CREATE TABLE IF NOT EXISTS comment_likes (
|
|
comment_id TEXT NOT NULL,
|
|
user_id TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
PRIMARY KEY (comment_id, user_id),
|
|
FOREIGN KEY (comment_id) REFERENCES comments(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
);`);
|
|
|
|
db.exec(
|
|
`CREATE INDEX IF NOT EXISTS idx_comment_likes_user ON comment_likes(user_id);`,
|
|
);
|
|
}
|