Fix entries fk and manually fix migrations

This commit is contained in:
Zoe Roux 2024-11-08 23:44:24 +01:00
parent 143ac6c721
commit 3f97ba729d
No known key found for this signature in database
4 changed files with 17 additions and 19 deletions

View File

@ -1,5 +1,3 @@
CREATE SCHEMA "kyoo";
--> statement-breakpoint
CREATE TYPE "kyoo"."entry_type" AS ENUM('unknown', 'episode', 'movie', 'special', 'extra');--> statement-breakpoint CREATE TYPE "kyoo"."entry_type" AS ENUM('unknown', 'episode', 'movie', 'special', 'extra');--> statement-breakpoint
CREATE TYPE "kyoo"."genres" AS ENUM('action', 'adventure', 'animation', 'comedy', 'crime', 'documentary', 'drama', 'family', 'fantasy', 'history', 'horror', 'music', 'mystery', 'romance', 'science-fiction', 'thriller', 'war', 'western', 'kids', 'reality', 'politics', 'soap', 'talk');--> statement-breakpoint CREATE TYPE "kyoo"."genres" AS ENUM('action', 'adventure', 'animation', 'comedy', 'crime', 'documentary', 'drama', 'family', 'fantasy', 'history', 'horror', 'music', 'mystery', 'romance', 'science-fiction', 'thriller', 'war', 'western', 'kids', 'reality', 'politics', 'soap', 'talk');--> statement-breakpoint
CREATE TYPE "kyoo"."show_kind" AS ENUM('serie', 'movie');--> statement-breakpoint CREATE TYPE "kyoo"."show_kind" AS ENUM('serie', 'movie');--> statement-breakpoint
@ -8,7 +6,7 @@ CREATE TABLE IF NOT EXISTS "kyoo"."entries" (
"pk" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "kyoo"."entries_pk_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "pk" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "kyoo"."entries_pk_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"id" uuid DEFAULT gen_random_uuid() NOT NULL, "id" uuid DEFAULT gen_random_uuid() NOT NULL,
"slug" varchar(255) NOT NULL, "slug" varchar(255) NOT NULL,
"show_id" integer, "show_pk" integer,
"order" integer NOT NULL, "order" integer NOT NULL,
"season_number" integer, "season_number" integer,
"episode_number" integer, "episode_number" integer,
@ -21,7 +19,7 @@ CREATE TABLE IF NOT EXISTS "kyoo"."entries" (
"next_refresh" timestamp with time zone, "next_refresh" timestamp with time zone,
CONSTRAINT "entries_id_unique" UNIQUE("id"), CONSTRAINT "entries_id_unique" UNIQUE("id"),
CONSTRAINT "entries_slug_unique" UNIQUE("slug"), CONSTRAINT "entries_slug_unique" UNIQUE("slug"),
CONSTRAINT "entries_showId_seasonNumber_episodeNumber_unique" UNIQUE("show_id","season_number","episode_number"), CONSTRAINT "entries_showPk_seasonNumber_episodeNumber_unique" UNIQUE("show_pk","season_number","episode_number"),
CONSTRAINT "order_positive" CHECK ("entries"."order" >= 0) CONSTRAINT "order_positive" CHECK ("entries"."order" >= 0)
); );
--> statement-breakpoint --> statement-breakpoint
@ -54,7 +52,7 @@ CREATE TABLE IF NOT EXISTS "kyoo"."shows" (
"id" uuid DEFAULT gen_random_uuid() NOT NULL, "id" uuid DEFAULT gen_random_uuid() NOT NULL,
"slug" varchar(255) NOT NULL, "slug" varchar(255) NOT NULL,
"kind" "kyoo"."show_kind" NOT NULL, "kind" "kyoo"."show_kind" NOT NULL,
"genres" "genres"[] NOT NULL, "genres" "kyoo"."genres"[] NOT NULL,
"rating" smallint, "rating" smallint,
"runtime" integer, "runtime" integer,
"status" "kyoo"."show_status" NOT NULL, "status" "kyoo"."show_status" NOT NULL,
@ -86,7 +84,7 @@ CREATE TABLE IF NOT EXISTS "kyoo"."videos" (
); );
--> statement-breakpoint --> statement-breakpoint
DO $$ BEGIN DO $$ BEGIN
ALTER TABLE "kyoo"."entries" ADD CONSTRAINT "entries_show_id_shows_id_fk" FOREIGN KEY ("show_id") REFERENCES "kyoo"."shows"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "kyoo"."entries" ADD CONSTRAINT "entries_show_pk_shows_pk_fk" FOREIGN KEY ("show_pk") REFERENCES "kyoo"."shows"("pk") ON DELETE cascade ON UPDATE no action;
EXCEPTION EXCEPTION
WHEN duplicate_object THEN null; WHEN duplicate_object THEN null;
END $$; END $$;

View File

@ -1,5 +1,5 @@
{ {
"id": "88c55813-3ceb-468d-b010-c2b8f7fa875e", "id": "82560792-5f4a-4723-9543-808719ade682",
"prevId": "00000000-0000-0000-0000-000000000000", "prevId": "00000000-0000-0000-0000-000000000000",
"version": "7", "version": "7",
"dialect": "postgresql", "dialect": "postgresql",
@ -38,8 +38,8 @@
"primaryKey": false, "primaryKey": false,
"notNull": true "notNull": true
}, },
"show_id": { "show_pk": {
"name": "show_id", "name": "show_pk",
"type": "integer", "type": "integer",
"primaryKey": false, "primaryKey": false,
"notNull": false "notNull": false
@ -110,13 +110,13 @@
}, },
"indexes": {}, "indexes": {},
"foreignKeys": { "foreignKeys": {
"entries_show_id_shows_id_fk": { "entries_show_pk_shows_pk_fk": {
"name": "entries_show_id_shows_id_fk", "name": "entries_show_pk_shows_pk_fk",
"tableFrom": "entries", "tableFrom": "entries",
"tableTo": "shows", "tableTo": "shows",
"schemaTo": "kyoo", "schemaTo": "kyoo",
"columnsFrom": ["show_id"], "columnsFrom": ["show_pk"],
"columnsTo": ["id"], "columnsTo": ["pk"],
"onDelete": "cascade", "onDelete": "cascade",
"onUpdate": "no action" "onUpdate": "no action"
} }
@ -133,10 +133,10 @@
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": ["slug"] "columns": ["slug"]
}, },
"entries_showId_seasonNumber_episodeNumber_unique": { "entries_showPk_seasonNumber_episodeNumber_unique": {
"name": "entries_showId_seasonNumber_episodeNumber_unique", "name": "entries_showPk_seasonNumber_episodeNumber_unique",
"nullsNotDistinct": false, "nullsNotDistinct": false,
"columns": ["show_id", "season_number", "episode_number"] "columns": ["show_pk", "season_number", "episode_number"]
} }
}, },
"policies": {}, "policies": {},

View File

@ -5,7 +5,7 @@
{ {
"idx": 0, "idx": 0,
"version": "7", "version": "7",
"when": 1731105447005, "when": 1731105746157,
"tag": "0000_init", "tag": "0000_init",
"breakpoints": true "breakpoints": true
} }

View File

@ -28,7 +28,7 @@ export const entries = schema.table(
pk: integer().primaryKey().generatedAlwaysAsIdentity(), pk: integer().primaryKey().generatedAlwaysAsIdentity(),
id: uuid().notNull().unique().defaultRandom(), id: uuid().notNull().unique().defaultRandom(),
slug: varchar({ length: 255 }).notNull().unique(), slug: varchar({ length: 255 }).notNull().unique(),
showId: integer().references(() => shows.id, { onDelete: "cascade" }), showPk: integer().references(() => shows.pk, { onDelete: "cascade" }),
order: integer().notNull(), order: integer().notNull(),
seasonNumber: integer(), seasonNumber: integer(),
episodeNumber: integer(), episodeNumber: integer(),
@ -43,7 +43,7 @@ export const entries = schema.table(
nextRefresh: timestamp({ withTimezone: true, mode: "string" }), nextRefresh: timestamp({ withTimezone: true, mode: "string" }),
}, },
(t) => [ (t) => [
unique().on(t.showId, t.seasonNumber, t.episodeNumber), unique().on(t.showPk, t.seasonNumber, t.episodeNumber),
check("order_positive", sql`${t.order} >= 0`), check("order_positive", sql`${t.order} >= 0`),
], ],
); );