Fix history population (drizzle patch)

This commit is contained in:
Zoe Roux 2025-04-07 21:24:04 +02:00
parent c48815a71a
commit e32fc229f8
No known key found for this signature in database
7 changed files with 1946 additions and 20 deletions

View File

@ -0,0 +1,5 @@
ALTER TABLE "kyoo"."history" ALTER COLUMN "video_pk" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "kyoo"."watchlist" ALTER COLUMN "status" SET DATA TYPE text;--> statement-breakpoint
DROP TYPE "kyoo"."watchlist_status";--> statement-breakpoint
CREATE TYPE "kyoo"."watchlist_status" AS ENUM('watching', 'rewatching', 'completed', 'dropped', 'planned');--> statement-breakpoint
ALTER TABLE "kyoo"."watchlist" ALTER COLUMN "status" SET DATA TYPE "kyoo"."watchlist_status" USING "status"::"kyoo"."watchlist_status";

File diff suppressed because it is too large Load Diff

View File

@ -127,6 +127,13 @@
"when": 1743944773824, "when": 1743944773824,
"tag": "0017_watchlist", "tag": "0017_watchlist",
"breakpoints": true "breakpoints": true
},
{
"idx": 18,
"version": "7",
"when": 1744053556621,
"tag": "0018_history",
"breakpoints": true
} }
] ]
} }

View File

@ -1,9 +1,90 @@
diff --git a/node_modules/drizzle-orm/.bun-tag-3622ae30f31c0d9a b/.bun-tag-3622ae30f31c0d9a
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/drizzle-orm/.bun-tag-36446a2521398ee8 b/.bun-tag-36446a2521398ee8 diff --git a/node_modules/drizzle-orm/.bun-tag-36446a2521398ee8 b/.bun-tag-36446a2521398ee8
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/drizzle-orm/.bun-tag-844efc51a55b820c b/.bun-tag-844efc51a55b820c
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/drizzle-orm/.bun-tag-9fae835e61d5cc75 b/.bun-tag-9fae835e61d5cc75 diff --git a/node_modules/drizzle-orm/.bun-tag-9fae835e61d5cc75 b/.bun-tag-9fae835e61d5cc75
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/drizzle-orm/.bun-tag-ce8efc9a806990a3 b/.bun-tag-ce8efc9a806990a3
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/pg-core/dialect.cjs b/pg-core/dialect.cjs
index 52acbfb6038fb1bbba4e34115d75a22bb0f9ab1a..1f10884caf05329ab98b06a68c8e7803e5283d32 100644
--- a/pg-core/dialect.cjs
+++ b/pg-core/dialect.cjs
@@ -347,7 +347,14 @@ class PgDialect {
buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }) {
const valuesSqlList = [];
const columns = table[import_table2.Table.Symbol.Columns];
- const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
+ let colEntries = Object.entries(columns);
+ colEntries = select && !is(valuesOrSelect, SQL)
+ ? Object
+ .keys(valuesOrSelect._.selectedFields)
+ .map((key) => [key, columns[key]])
+ : overridingSystemValue_
+ ? colEntries
+ : colEntries.filter(([_, col]) => !col.shouldDisableInsert());
const insertOrder = colEntries.map(
([, column]) => import_sql2.sql.identifier(this.casing.getColumnCasing(column))
);
diff --git a/pg-core/dialect.js b/pg-core/dialect.js
index d7985c81f3d224f7671efe72e79b14153d5ca8ce..91d99ccd2ebda807a7d45c76f7164e571b922159 100644
--- a/pg-core/dialect.js
+++ b/pg-core/dialect.js
@@ -345,7 +345,14 @@ class PgDialect {
buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }) {
const valuesSqlList = [];
const columns = table[Table.Symbol.Columns];
- const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
+ let colEntries = Object.entries(columns);
+ colEntries = select && !is(valuesOrSelect, SQL)
+ ? Object
+ .keys(valuesOrSelect._.selectedFields)
+ .map((key) => [key, columns[key]])
+ : overridingSystemValue_
+ ? colEntries
+ : colEntries.filter(([_, col]) => !col.shouldDisableInsert());
const insertOrder = colEntries.map(
([, column]) => sql.identifier(this.casing.getColumnCasing(column))
);
diff --git a/pg-core/query-builders/insert.cjs b/pg-core/query-builders/insert.cjs
index 08bb0d7485ebf997e3f081e2254ea8fd8bc20f65..341d2513d4377acc33ee0606d05580566fd4b88c 100644
--- a/pg-core/query-builders/insert.cjs
+++ b/pg-core/query-builders/insert.cjs
@@ -75,11 +75,6 @@ class PgInsertBuilder {
}
select(selectQuery) {
const select = typeof selectQuery === "function" ? selectQuery(new import_query_builder.QueryBuilder()) : selectQuery;
- if (!(0, import_entity.is)(select, import_sql.SQL) && !(0, import_utils.haveSameKeys)(this.table[import_table.Columns], select._.selectedFields)) {
- throw new Error(
- "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
- );
- }
return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
}
}
diff --git a/pg-core/query-builders/insert.js b/pg-core/query-builders/insert.js
index 0fc8eeb80f4a5512f6c84f3d596832623a33b748..b993f226daf16f423db012dff828d89c522603c3 100644
--- a/pg-core/query-builders/insert.js
+++ b/pg-core/query-builders/insert.js
@@ -51,11 +51,6 @@ class PgInsertBuilder {
}
select(selectQuery) {
const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
- if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
- throw new Error(
- "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
- );
- }
return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
}
}
diff --git a/pg-core/query-builders/select.d.cts b/pg-core/query-builders/select.d.cts diff --git a/pg-core/query-builders/select.d.cts b/pg-core/query-builders/select.d.cts
index b968ebb3f563f37c8c36221dd17cc6f3603270ec..3fda6d0a97997f6bd07ec6a0c83397c0fdd2e97e 100644 index b968ebb3f563f37c8c36221dd17cc6f3603270ec..3fda6d0a97997f6bd07ec6a0c83397c0fdd2e97e 100644
--- a/pg-core/query-builders/select.d.cts --- a/pg-core/query-builders/select.d.cts

View File

@ -146,7 +146,7 @@ export const historyH = new Elysia({ tags: ["profiles"] })
) )
.post( .post(
"/profiles/me/history", "/profiles/me/history",
async ({ body, jwt: { sub } }) => { async ({ body, jwt: { sub }, error }) => {
const profilePk = await getOrCreateProfile(sub); const profilePk = await getOrCreateProfile(sub);
const rows = await db const rows = await db
@ -175,14 +175,14 @@ export const historyH = new Elysia({ tags: ["profiles"] })
), ),
and( and(
not(sql`hist.entryUseId::boolean`), not(sql`hist.entryUseId::boolean`),
eq(entries.id, sql`hist.entry`), eq(entries.slug, sql`hist.entry`),
), ),
), ),
) )
.innerJoin(videos, eq(videos.id, sql`hist.videoId::uuid`)), .leftJoin(videos, eq(videos.id, sql`hist.videoId::uuid`)),
) )
.returning({ pk: history.pk }); .returning({ pk: history.pk });
return { status: 201, inserted: rows.length }; return error(201, { status: 201, inserted: rows.length });
}, },
{ {
detail: { description: "Bulk add entries/movies to your watch history." }, detail: { description: "Bulk add entries/movies to your watch history." },

View File

@ -15,9 +15,7 @@ export const history = schema.table(
entryPk: integer() entryPk: integer()
.notNull() .notNull()
.references(() => entries.pk, { onDelete: "cascade" }), .references(() => entries.pk, { onDelete: "cascade" }),
videoPk: integer() videoPk: integer().references(() => videos.pk, { onDelete: "set null" }),
.notNull()
.references(() => videos.pk, { onDelete: "set null" }),
percent: integer().notNull().default(0), percent: integer().notNull().default(0),
time: integer(), time: integer(),
playedDate: timestamp({ withTimezone: true, mode: "string" }) playedDate: timestamp({ withTimezone: true, mode: "string" })

View File

@ -5,11 +5,7 @@ import {
createSerie, createSerie,
getEntries, getEntries,
getHistory, getHistory,
getMovie,
getNews, getNews,
getShows,
getWatchlist,
setMovieStatus,
} from "tests/helpers"; } from "tests/helpers";
import { expectStatus } from "tests/utils"; import { expectStatus } from "tests/utils";
import { db } from "~/db"; import { db } from "~/db";
@ -59,12 +55,12 @@ describe("Set & get history", () => {
expectStatus(resp, body).toBe(200); expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(2); expect(body.items).toBeArrayOfSize(2);
expect(body.items[0].slug).toBe(bubble.slug); expect(body.items[0].slug).toBe(bubble.slug);
expect(body.items[0].watchStatus).toMatchObject({ expect(body.items[0].progress).toMatchObject({
percent: 100, percent: 100,
time: 2 * 60, time: 2 * 60,
}); });
expect(body.items[1].slug).toBe(miaEntrySlug); expect(body.items[1].slug).toBe(miaEntrySlug);
expect(body.items[1].watchStatus).toMatchObject({ expect(body.items[1].progress).toMatchObject({
percent: 58, percent: 58,
videoId: madeInAbyssVideo.id, videoId: madeInAbyssVideo.id,
}); });
@ -87,17 +83,17 @@ describe("Set & get history", () => {
expectStatus(resp, body).toBe(200); expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(3); expect(body.items).toBeArrayOfSize(3);
expect(body.items[0].slug).toBe(miaEntrySlug); expect(body.items[0].slug).toBe(miaEntrySlug);
expect(body.items[0].watchStatus).toMatchObject({ expect(body.items[0].progress).toMatchObject({
percent: 100, percent: 100,
videoId: madeInAbyssVideo.id, videoId: madeInAbyssVideo.id,
}); });
expect(body.items[1].slug).toBe(bubble.slug); expect(body.items[1].slug).toBe(bubble.slug);
expect(body.items[1].watchStatus).toMatchObject({ expect(body.items[1].progress).toMatchObject({
percent: 100, percent: 100,
time: 2 * 60, time: 2 * 60,
}); });
expect(body.items[2].slug).toBe(miaEntrySlug); expect(body.items[2].slug).toBe(miaEntrySlug);
expect(body.items[2].watchStatus).toMatchObject({ expect(body.items[2].progress).toMatchObject({
percent: 58, percent: 58,
videoId: madeInAbyssVideo.id, videoId: madeInAbyssVideo.id,
}); });
@ -112,7 +108,7 @@ describe("Set & get history", () => {
percent: 100, percent: 100,
time: 38 * 60, time: 38 * 60,
videoId: madeInAbyssVideo.id, videoId: madeInAbyssVideo.id,
playedDate: "2025-02-03 00:00:00", playedDate: "2025-02-03 00:00:00+00",
}); });
}); });
@ -120,12 +116,12 @@ describe("Set & get history", () => {
const [resp, body] = await getNews({ langs: "en" }); const [resp, body] = await getNews({ langs: "en" });
expectStatus(resp, body).toBe(200); expectStatus(resp, body).toBe(200);
const entry = body.items.findFirst((x: any) => x.slug === miaEntrySlug); const entry = body.items.find((x: any) => x.slug === miaEntrySlug);
expect(entry.progress).toMatchObject({ expect(entry.progress).toMatchObject({
percent: 100, percent: 100,
time: 38 * 60, time: 38 * 60,
videoId: madeInAbyssVideo.id, videoId: madeInAbyssVideo.id,
playedDate: "2025-02-03 00:00:00", playedDate: "2025-02-03 00:00:00+00",
}); });
}); });
@ -143,7 +139,7 @@ describe("Set & get history", () => {
// const [resp, body] = await getMovie(bubble.slug, {}); // const [resp, body] = await getMovie(bubble.slug, {});
// expectStatus(resp, body).toBe(200); // expectStatus(resp, body).toBe(200);
// expect(body.slug).toBe(bubble.slug); // expect(body.slug).toBe(bubble.slug);
// expect(body.watchStatus).toMatchObject({ // expect(body.progress).toMatchObject({
// status: "rewatching", // status: "rewatching",
// completedAt: "2024-12-21 00:00:00+00", // completedAt: "2024-12-21 00:00:00+00",
// score: 85, // score: 85,