mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Cleanup availableCount
& episodeCount
update transaction
This commit is contained in:
parent
8687cf8de0
commit
878d72f3a4
@ -10,6 +10,7 @@ import { conflictUpdateAllExcept, values } from "~/db/utils";
|
||||
import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry";
|
||||
import { processOptImage } from "../images";
|
||||
import { guessNextRefresh } from "../refresh";
|
||||
import { updateAvailableCount } from "./shows";
|
||||
|
||||
type SeedEntry = SEntry & {
|
||||
video?: undefined;
|
||||
@ -41,8 +42,9 @@ const generateSlug = (
|
||||
};
|
||||
|
||||
export const insertEntries = async (
|
||||
show: { pk: number; slug: string },
|
||||
show: { pk: number; slug: string; kind: "movie" | "serie" | "collection" },
|
||||
items: (SeedEntry | SeedExtra)[],
|
||||
onlyExtras = false,
|
||||
) => {
|
||||
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: [] }));
|
||||
}
|
||||
|
||||
const retVideos = await db
|
||||
.insert(entryVideoJoin)
|
||||
.select(
|
||||
db
|
||||
.select({
|
||||
entryPk: sql<number>`vids.entryPk::integer`.as("entry"),
|
||||
videoPk: videos.pk,
|
||||
slug: computeVideoSlug(
|
||||
sql`vids.entrySlug::text`,
|
||||
sql`vids.needRendering::boolean`,
|
||||
),
|
||||
})
|
||||
.from(values(vids).as("vids"))
|
||||
.innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)),
|
||||
)
|
||||
.onConflictDoNothing()
|
||||
.returning({
|
||||
slug: entryVideoJoin.slug,
|
||||
entryPk: entryVideoJoin.entryPk,
|
||||
});
|
||||
const retVideos = await db.transaction(async (tx) => {
|
||||
const ret = await tx
|
||||
.insert(entryVideoJoin)
|
||||
.select(
|
||||
db
|
||||
.select({
|
||||
entryPk: sql<number>`vids.entryPk::integer`.as("entry"),
|
||||
videoPk: videos.pk,
|
||||
slug: computeVideoSlug(
|
||||
sql`vids.entrySlug::text`,
|
||||
sql`vids.needRendering::boolean`,
|
||||
),
|
||||
})
|
||||
.from(values(vids).as("vids"))
|
||||
.innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)),
|
||||
)
|
||||
.onConflictDoNothing()
|
||||
.returning({
|
||||
slug: entryVideoJoin.slug,
|
||||
entryPk: entryVideoJoin.entryPk,
|
||||
});
|
||||
|
||||
if (!onlyExtras)
|
||||
await updateAvailableCount(tx, [show.pk], show.kind === "serie");
|
||||
return ret;
|
||||
});
|
||||
|
||||
return retEntries.map((entry) => ({
|
||||
id: entry.id,
|
||||
|
@ -60,6 +60,7 @@ async function insertBaseShow(
|
||||
})
|
||||
.returning({
|
||||
pk: shows.pk,
|
||||
kind: shows.kind,
|
||||
id: shows.id,
|
||||
slug: shows.slug,
|
||||
// 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.
|
||||
// simply bail and let the caller handle this.
|
||||
const [{ pk, id }] = await db
|
||||
.select({ pk: shows.pk, id: shows.id })
|
||||
const [{ pk, id, kind }] = await db
|
||||
.select({ pk: shows.pk, id: shows.id, kind: shows.kind })
|
||||
.from(shows)
|
||||
.where(eq(shows.slug, show.slug))
|
||||
.limit(1);
|
||||
return {
|
||||
status: 409 as const,
|
||||
kind,
|
||||
pk,
|
||||
id,
|
||||
slug: show.slug,
|
||||
@ -95,10 +97,11 @@ async function insertBaseShow(
|
||||
}
|
||||
|
||||
export async function updateAvailableCount(
|
||||
tx: typeof db | Parameters<Parameters<typeof db.transaction>[0]>[0],
|
||||
showPks: number[],
|
||||
updateEntryCount = true,
|
||||
) {
|
||||
return await db
|
||||
return await tx
|
||||
.update(shows)
|
||||
.set({
|
||||
availableCount: sql`${db
|
||||
|
@ -105,7 +105,6 @@ export const seedMovie = async (
|
||||
videos,
|
||||
},
|
||||
]);
|
||||
await updateAvailableCount([show.pk], false);
|
||||
|
||||
const retStudios = await insertStudios(studios, show.pk);
|
||||
const retStaff = await insertStaff(staff, show.pk);
|
||||
|
@ -131,8 +131,8 @@ export const seedSerie = async (
|
||||
const retExtras = await insertEntries(
|
||||
show,
|
||||
(extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })),
|
||||
true,
|
||||
);
|
||||
await updateAvailableCount([show.pk]);
|
||||
|
||||
const retStudios = await insertStudios(studios, show.pk);
|
||||
const retStaff = await insertStaff(staff, show.pk);
|
||||
|
Loading…
x
Reference in New Issue
Block a user