Fix unnest issues

This commit is contained in:
2025-11-28 17:11:29 +01:00
parent 8fc279d2ed
commit 464d720ef9
6 changed files with 20 additions and 14 deletions

View File

@@ -73,9 +73,10 @@ export const insertCollection = async (
}),
);
await flushImageQueue(tx, imgQueue, 100);
// we can't unnest values here because show translations contains arrays.
await tx
.insert(showTranslations)
.select(unnestValues(trans, showTranslations))
.values(trans)
.onConflictDoUpdate({
target: [showTranslations.pk, showTranslations.language],
set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]),

View File

@@ -161,22 +161,22 @@ export const insertEntries = async (
.select(
db
.select({
entryPk: sql<number>`vids.entryPk`.as("entry"),
entryPk: sql<number>`vids."entryPk"`.as("entry"),
videoPk: videos.pk,
slug: computeVideoSlug(
sql`vids.entrySlug`,
sql`vids.needRendering`,
sql`vids."entrySlug"`,
sql`vids."needRendering"`,
),
})
.from(
unnest(vids, "vids", {
entryPk: "integer",
entrySlug: "string",
entrySlug: "varchar(255)",
needRendering: "boolean",
videoId: "uuid",
}),
)
.innerJoin(videos, eq(videos.id, sql`vids.videoId`)),
.innerJoin(videos, eq(videos.id, sql`vids."videoId"`)),
)
.onConflictDoNothing()
.returning({

View File

@@ -93,9 +93,10 @@ export const insertShow = async (
}),
);
await flushImageQueue(tx, imgQueue, 200);
// we can't unnest values here because show translations contains arrays.
await tx
.insert(showTranslations)
.select(unnestValues(trans, showTranslations))
.values(trans)
.onConflictDoUpdate({
target: [showTranslations.pk, showTranslations.language],
set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]),

View File

@@ -61,9 +61,9 @@ export const insertStudios = async (
db
.select({
showPk: sql`${showPk}`.as("showPk"),
studioPk: sql`v.studioPk`.as("studioPk"),
studioPk: sql`v."studioPk"`.as("studioPk"),
})
.from(sql`unnest(${sqlarr(ret.map((x) => x.pk))}) as v("studioPk")`),
.from(sql`unnest(${sqlarr(ret.map((x) => x.pk))}::integer[]) as v("studioPk")`),
)
.onConflictDoNothing();
return ret;

View File

@@ -77,7 +77,7 @@ export function conflictUpdateAllExcept<
export function sqlarr(array: unknown[]): string {
return `{${array
.map((item) =>
!item || item === "null"
item === "null" || item === null || item === undefined
? "null"
: Array.isArray(item)
? sqlarr(item)

View File

@@ -17,11 +17,11 @@ describe("images", () => {
});
it("Create a serie download images", async () => {
await db.delete(mqueue);
await createSerie(madeInAbyss);
const release = await processImages();
// remove notifications to prevent other images to be downloaded (do not curl 20000 images for nothing)
release();
await db.delete(mqueue);
const ret = await db.query.shows.findFirst({
where: eq(shows.slug, madeInAbyss.slug),
@@ -32,12 +32,17 @@ describe("images", () => {
});
it("Download 404 image", async () => {
await db.delete(mqueue);
const url404 = "https://mockhttp.org/status/404";
const [ret, body] = await createMovie({
...dune,
translations: {
en: {
...dune.translations.en,
poster: "https://www.google.com/404",
poster: url404,
thumbnail: null,
banner: null,
logo: null,
},
},
});
@@ -46,12 +51,11 @@ describe("images", () => {
const release = await processImages();
// remove notifications to prevent other images to be downloaded (do not curl 20000 images for nothing)
release();
await db.delete(mqueue);
const failed = await db.query.mqueue.findFirst({
where: and(
eq(mqueue.kind, "image"),
eq(sql`${mqueue.message}->>'url'`, "https://www.google.com/404"),
eq(sql`${mqueue.message}->>'url'`, url404),
),
});
expect(failed!.attempt).toBe(5);