v4: playlists have categories of their own — a set apart from the dumps', which admins manage on /manage under "Playlist categories", next to "Dump categories". Each category now has a kind, dump or playlist, its slug and name unique within its kind only; migration 0025 rebuilds categories with the column, every existing one becoming a dump category with its dumps, and adds playlist_categories. A playlist's owner files it under any of them in the create and edit forms, and it shows them as chips on its card and its page, each leading to /playlists/<slug>.
/playlists is the new page of every public playlist, newest first (GET /api/playlists), and /playlists/<slug> a playlist category's, with a menu of the categories, linked from the user menu as "All playlists"; `playlists` is reserved as a dump category's slug, and one that had it is renamed. A profile's Playlists tab narrows to a playlist category with the same menu as its Dumps tab (?category=), and so do the Dumps and Playlists tabs of /search, each with its own kind (category and playlistCategory for GET /api/search), another tab dropping it. The category service takes the kind throughout, so a dump can't be filed under a playlist category nor the other way round, and categoryJoin is the one filter join the dump, follow and playlist services share. useCategories(kind) gives one kind's set from the one fetch, CategoryChips replaces the chips the dump card and page each drew, useCategoryFilter keeps a list to its category as the socket brings more, and useCategorySelection holds a form's picked categories. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
110
api/db/migrations/0025_playlist_categories.ts
Normal file
110
api/db/migrations/0025_playlist_categories.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Gives playlists categories of their own, a set apart from the dumps': each
|
||||
* category gets a `kind`, `dump` or `playlist`, its slug and name now unique
|
||||
* within its kind only, and `playlist_categories` files playlists under them.
|
||||
*
|
||||
* `/playlists` becomes the page of every public playlist, so its first segment
|
||||
* is reserved.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import { renameReservedCategories } from "../../lib/reserved-slugs.ts";
|
||||
|
||||
/**
|
||||
* Run once by `runMigrations`. Every existing category becomes a dump
|
||||
* category, filing the same dumps.
|
||||
*
|
||||
* SQLite can't change a table's UNIQUE constraints in place, so `categories`
|
||||
* is rebuilt. `foreign_keys` can't be switched off inside the runner's
|
||||
* transaction, and dropping `categories` would then empty `dump_categories`
|
||||
* through its cascade, so both are read, dropped child first, made anew as in
|
||||
* schema.sql, and refilled. A database made from schema.sql already has the
|
||||
* `kind` column and is left as it is.
|
||||
*/
|
||||
export function up(db: DatabaseSync): void {
|
||||
const columns = new Set(
|
||||
(db.prepare(`PRAGMA table_info(categories);`).all() as { name: string }[])
|
||||
.map((c) => c.name),
|
||||
);
|
||||
|
||||
if (!columns.has("kind")) {
|
||||
const categories = db.prepare(
|
||||
`SELECT id, slug, name, position, created_at, updated_at FROM categories;`,
|
||||
).all() as {
|
||||
id: string;
|
||||
slug: string;
|
||||
name: string;
|
||||
position: number;
|
||||
created_at: string;
|
||||
updated_at: string | null;
|
||||
}[];
|
||||
const links = db.prepare(
|
||||
`SELECT dump_id, category_id FROM dump_categories;`,
|
||||
).all() as { dump_id: string; category_id: string }[];
|
||||
|
||||
db.exec(`DROP TABLE dump_categories;`);
|
||||
db.exec(`DROP TABLE categories;`);
|
||||
|
||||
db.exec(
|
||||
`CREATE TABLE categories (
|
||||
id TEXT PRIMARY KEY,
|
||||
kind TEXT NOT NULL DEFAULT 'dump' CHECK (kind IN ('dump', 'playlist')),
|
||||
slug TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
position INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT,
|
||||
UNIQUE (kind, slug),
|
||||
UNIQUE (kind, name)
|
||||
);`,
|
||||
);
|
||||
db.exec(
|
||||
`CREATE TABLE dump_categories (
|
||||
dump_id TEXT NOT NULL,
|
||||
category_id TEXT NOT NULL,
|
||||
PRIMARY KEY (dump_id, category_id),
|
||||
FOREIGN KEY (dump_id) REFERENCES dumps(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
|
||||
);`,
|
||||
);
|
||||
db.exec(
|
||||
`CREATE INDEX idx_dump_categories_category ON dump_categories(category_id);`,
|
||||
);
|
||||
|
||||
const insertCategory = db.prepare(
|
||||
`INSERT INTO categories (id, kind, slug, name, position, created_at, updated_at)
|
||||
VALUES (?, 'dump', ?, ?, ?, ?, ?);`,
|
||||
);
|
||||
for (const c of categories) {
|
||||
insertCategory.run(
|
||||
c.id,
|
||||
c.slug,
|
||||
c.name,
|
||||
c.position,
|
||||
c.created_at,
|
||||
c.updated_at,
|
||||
);
|
||||
}
|
||||
const insertLink = db.prepare(
|
||||
`INSERT INTO dump_categories (dump_id, category_id) VALUES (?, ?);`,
|
||||
);
|
||||
for (const l of links) insertLink.run(l.dump_id, l.category_id);
|
||||
}
|
||||
|
||||
db.exec(
|
||||
`CREATE TABLE IF NOT EXISTS playlist_categories (
|
||||
playlist_id TEXT NOT NULL,
|
||||
category_id TEXT NOT NULL,
|
||||
PRIMARY KEY (playlist_id, category_id),
|
||||
FOREIGN KEY (playlist_id) REFERENCES playlists(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
|
||||
);`,
|
||||
);
|
||||
db.exec(
|
||||
`CREATE INDEX IF NOT EXISTS idx_playlist_categories_category ON playlist_categories(category_id);`,
|
||||
);
|
||||
|
||||
renameReservedCategories(db);
|
||||
}
|
||||
Reference in New Issue
Block a user