stremio/apps/master-api/prisma/migrations/20260906030000_viewer_profiles/migration.sql
Jos Vooges | STH f58a088156 Add Netflix-style viewer profiles with kids filter on master, TV, and mobile.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-06 03:01:00 +02:00

114 lines
4.6 KiB
SQL

-- Viewer profiles (Netflix-style) + per-profile progress/prefs + TMDB adult flags
-- 1) Profiles table
CREATE TABLE "viewer_profiles" (
"id" TEXT NOT NULL,
"viewer_user_id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"avatar_id" TEXT NOT NULL DEFAULT 'fox',
"is_owner" BOOLEAN NOT NULL DEFAULT false,
"is_kids" BOOLEAN NOT NULL DEFAULT false,
"pin_hash" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "viewer_profiles_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "viewer_profiles_viewer_user_id_name_key" ON "viewer_profiles"("viewer_user_id", "name");
CREATE INDEX "viewer_profiles_viewer_user_id_idx" ON "viewer_profiles"("viewer_user_id");
ALTER TABLE "viewer_profiles"
ADD CONSTRAINT "viewer_profiles_viewer_user_id_fkey"
FOREIGN KEY ("viewer_user_id") REFERENCES "viewer_users"("id")
ON DELETE CASCADE ON UPDATE CASCADE;
-- 2) Default owner profile per existing viewer
INSERT INTO "viewer_profiles" ("id", "viewer_user_id", "name", "avatar_id", "is_owner", "is_kids", "created_at", "updated_at")
SELECT
gen_random_uuid()::text,
vu."id",
COALESCE(NULLIF(TRIM(vu."name"), ''), 'Hoofdprofiel'),
'fox',
true,
false,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
FROM "viewer_users" vu;
-- 3) Device active profile
ALTER TABLE "viewer_devices" ADD COLUMN "active_profile_id" TEXT;
ALTER TABLE "viewer_devices"
ADD CONSTRAINT "viewer_devices_active_profile_id_fkey"
FOREIGN KEY ("active_profile_id") REFERENCES "viewer_profiles"("id")
ON DELETE SET NULL ON UPDATE CASCADE;
CREATE INDEX "viewer_devices_active_profile_id_idx" ON "viewer_devices"("active_profile_id");
-- 4) Watch progress → profile_id
ALTER TABLE "watch_progress" ADD COLUMN "profile_id" TEXT;
UPDATE "watch_progress" wp
SET "profile_id" = vp."id"
FROM "viewer_profiles" vp
WHERE vp."viewer_user_id" = wp."viewer_user_id"
AND vp."is_owner" = true;
DELETE FROM "watch_progress" WHERE "profile_id" IS NULL;
ALTER TABLE "watch_progress" ALTER COLUMN "profile_id" SET NOT NULL;
ALTER TABLE "watch_progress" DROP CONSTRAINT IF EXISTS "watch_progress_viewer_user_id_fkey";
DROP INDEX IF EXISTS "watch_progress_viewer_user_id_movie_id_key";
DROP INDEX IF EXISTS "watch_progress_viewer_user_id_episode_id_key";
DROP INDEX IF EXISTS "watch_progress_viewer_user_id_updated_at_idx";
DROP INDEX IF EXISTS "watch_progress_viewer_user_id_hidden_from_continue_completed_idx";
ALTER TABLE "watch_progress" DROP COLUMN "viewer_user_id";
CREATE UNIQUE INDEX "watch_progress_profile_id_movie_id_key" ON "watch_progress"("profile_id", "movie_id");
CREATE UNIQUE INDEX "watch_progress_profile_id_episode_id_key" ON "watch_progress"("profile_id", "episode_id");
CREATE INDEX "watch_progress_profile_id_updated_at_idx" ON "watch_progress"("profile_id", "updated_at");
CREATE INDEX "watch_progress_profile_id_hidden_from_continue_completed_idx"
ON "watch_progress"("profile_id", "hidden_from_continue", "completed");
ALTER TABLE "watch_progress"
ADD CONSTRAINT "watch_progress_profile_id_fkey"
FOREIGN KEY ("profile_id") REFERENCES "viewer_profiles"("id")
ON DELETE CASCADE ON UPDATE CASCADE;
-- 5) Shelf prefs → profile_id
ALTER TABLE "viewer_shelf_preferences" ADD COLUMN "profile_id" TEXT;
UPDATE "viewer_shelf_preferences" sp
SET "profile_id" = vp."id"
FROM "viewer_profiles" vp
WHERE vp."viewer_user_id" = sp."viewer_user_id"
AND vp."is_owner" = true;
DELETE FROM "viewer_shelf_preferences" WHERE "profile_id" IS NULL;
ALTER TABLE "viewer_shelf_preferences" ALTER COLUMN "profile_id" SET NOT NULL;
ALTER TABLE "viewer_shelf_preferences" DROP CONSTRAINT IF EXISTS "viewer_shelf_preferences_viewer_user_id_fkey";
DROP INDEX IF EXISTS "viewer_shelf_preferences_viewer_user_id_shelf_id_key";
DROP INDEX IF EXISTS "viewer_shelf_preferences_viewer_user_id_sort_order_idx";
ALTER TABLE "viewer_shelf_preferences" DROP COLUMN "viewer_user_id";
CREATE UNIQUE INDEX "viewer_shelf_preferences_profile_id_shelf_id_key"
ON "viewer_shelf_preferences"("profile_id", "shelf_id");
CREATE INDEX "viewer_shelf_preferences_profile_id_sort_order_idx"
ON "viewer_shelf_preferences"("profile_id", "sort_order");
ALTER TABLE "viewer_shelf_preferences"
ADD CONSTRAINT "viewer_shelf_preferences_profile_id_fkey"
FOREIGN KEY ("profile_id") REFERENCES "viewer_profiles"("id")
ON DELETE CASCADE ON UPDATE CASCADE;
-- 6) TMDB adult flags (kids-profielen filteren adult + geblokkeerde genres in API)
ALTER TABLE "movies" ADD COLUMN "adult" BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE "series" ADD COLUMN "adult" BOOLEAN NOT NULL DEFAULT false;
CREATE INDEX "movies_adult_idx" ON "movies"("adult");
CREATE INDEX "series_adult_idx" ON "series"("adult");