Cleanup availableCount & episodeCount update transaction

This commit is contained in:
Zoe Roux 2025-03-10 14:45:00 +01:00
parent 8687cf8de0
commit 878d72f3a4
No known key found for this signature in database
4 changed files with 41 additions and 27 deletions

View File

@ -10,6 +10,7 @@ import { conflictUpdateAllExcept, values } from "~/db/utils";
import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry"; import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry";
import { processOptImage } from "../images"; import { processOptImage } from "../images";
import { guessNextRefresh } from "../refresh"; import { guessNextRefresh } from "../refresh";
import { updateAvailableCount } from "./shows";
type SeedEntry = SEntry & { type SeedEntry = SEntry & {
video?: undefined; video?: undefined;
@ -41,8 +42,9 @@ const generateSlug = (
}; };
export const insertEntries = async ( export const insertEntries = async (
show: { pk: number; slug: string }, show: { pk: number; slug: string; kind: "movie" | "serie" | "collection" },
items: (SeedEntry | SeedExtra)[], items: (SeedEntry | SeedExtra)[],
onlyExtras = false,
) => { ) => {
if (!items) return []; if (!items) return [];
@ -135,29 +137,39 @@ export const insertEntries = async (
})); }));
}); });
if (vids.length === 0) if (vids.length === 0) {
// we have not added videos but we need to update the `entriesCount`
if (show.kind === "serie" && !onlyExtras)
await updateAvailableCount(db, [show.pk], true);
return retEntries.map((x) => ({ id: x.id, slug: x.slug, videos: [] })); return retEntries.map((x) => ({ id: x.id, slug: x.slug, videos: [] }));
}
const retVideos = await db const retVideos = await db.transaction(async (tx) => {
.insert(entryVideoJoin) const ret = await tx
.select( .insert(entryVideoJoin)
db .select(
.select({ db
entryPk: sql<number>`vids.entryPk::integer`.as("entry"), .select({
videoPk: videos.pk, entryPk: sql<number>`vids.entryPk::integer`.as("entry"),
slug: computeVideoSlug( videoPk: videos.pk,
sql`vids.entrySlug::text`, slug: computeVideoSlug(
sql`vids.needRendering::boolean`, sql`vids.entrySlug::text`,
), sql`vids.needRendering::boolean`,
}) ),
.from(values(vids).as("vids")) })
.innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)), .from(values(vids).as("vids"))
) .innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)),
.onConflictDoNothing() )
.returning({ .onConflictDoNothing()
slug: entryVideoJoin.slug, .returning({
entryPk: entryVideoJoin.entryPk, slug: entryVideoJoin.slug,
}); entryPk: entryVideoJoin.entryPk,
});
if (!onlyExtras)
await updateAvailableCount(tx, [show.pk], show.kind === "serie");
return ret;
});
return retEntries.map((entry) => ({ return retEntries.map((entry) => ({
id: entry.id, id: entry.id,

View File

@ -60,6 +60,7 @@ async function insertBaseShow(
}) })
.returning({ .returning({
pk: shows.pk, pk: shows.pk,
kind: shows.kind,
id: shows.id, id: shows.id,
slug: shows.slug, slug: shows.slug,
// https://stackoverflow.com/questions/39058213/differentiate-inserted-and-updated-rows-in-upsert-using-system-columns/39204667#39204667 // https://stackoverflow.com/questions/39058213/differentiate-inserted-and-updated-rows-in-upsert-using-system-columns/39204667#39204667
@ -81,13 +82,14 @@ async function insertBaseShow(
// if at this point ret is still undefined, we could not reconciliate. // if at this point ret is still undefined, we could not reconciliate.
// simply bail and let the caller handle this. // simply bail and let the caller handle this.
const [{ pk, id }] = await db const [{ pk, id, kind }] = await db
.select({ pk: shows.pk, id: shows.id }) .select({ pk: shows.pk, id: shows.id, kind: shows.kind })
.from(shows) .from(shows)
.where(eq(shows.slug, show.slug)) .where(eq(shows.slug, show.slug))
.limit(1); .limit(1);
return { return {
status: 409 as const, status: 409 as const,
kind,
pk, pk,
id, id,
slug: show.slug, slug: show.slug,
@ -95,10 +97,11 @@ async function insertBaseShow(
} }
export async function updateAvailableCount( export async function updateAvailableCount(
tx: typeof db | Parameters<Parameters<typeof db.transaction>[0]>[0],
showPks: number[], showPks: number[],
updateEntryCount = true, updateEntryCount = true,
) { ) {
return await db return await tx
.update(shows) .update(shows)
.set({ .set({
availableCount: sql`${db availableCount: sql`${db

View File

@ -105,7 +105,6 @@ export const seedMovie = async (
videos, videos,
}, },
]); ]);
await updateAvailableCount([show.pk], false);
const retStudios = await insertStudios(studios, show.pk); const retStudios = await insertStudios(studios, show.pk);
const retStaff = await insertStaff(staff, show.pk); const retStaff = await insertStaff(staff, show.pk);

View File

@ -131,8 +131,8 @@ export const seedSerie = async (
const retExtras = await insertEntries( const retExtras = await insertEntries(
show, show,
(extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })), (extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })),
true,
); );
await updateAvailableCount([show.pk]);
const retStudios = await insertStudios(studios, show.pk); const retStudios = await insertStudios(studios, show.pk);
const retStaff = await insertStaff(staff, show.pk); const retStaff = await insertStaff(staff, show.pk);