Files
gerbeur/api/sql/migrations/0001_comment_likes.ts
khannurien 7afb5d3f07
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 42s
v3: added comment likes, added votes/likes list view, added db migrations mechanism, updated project dependencies
2026-06-21 14:17:56 +00:00

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);`,
);
}