All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 39s
28 lines
945 B
TypeScript
28 lines
945 B
TypeScript
import type { DatabaseSync } from "node:sqlite";
|
|
|
|
// Idempotent: safe to run against a fresh db (already created from schema.sql
|
|
// with these columns/tables) or an existing one that predates them.
|
|
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);`,
|
|
);
|
|
}
|