This commit is contained in:
Rotimi Best
2026-06-25 22:47:30 +01:00
61 changed files with 12591 additions and 15448 deletions
-1
View File
@@ -43,7 +43,6 @@ const envSchema = z.object({
PRIVATE_SERVER_KEY: z.string().optional(),
PROJECT_ID_VERCEL: z.string().optional(),
TEAM_ID_VERCEL: z.string().optional(),
AUTH_BEARER_TOKEN: z.string().optional(),
UNSPLASH_API_KEY: z.string().optional(),
/** When 'true', instance runs in self-hosted mode: single org, auto-enroll as student, no org switching */
PUBLIC_IS_SELFHOSTED: z.string().optional(),
+3
View File
@@ -54,6 +54,9 @@
"dev": "tsc --watch",
"db": "drizzle-kit",
"db:studio": "drizzle-kit studio",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:baseline": "tsx src/scripts/baseline.ts",
"db:reset": "tsx src/scripts/db-reset.ts --skip-seed",
"db:setup": "tsx src/scripts/db-setup.ts",
"db:setup:seed": "tsx src/scripts/db-setup.ts --seed",
File diff suppressed because it is too large Load Diff
@@ -1,644 +0,0 @@
-- CREATE TYPE "public"."COURSE_TYPE" AS ENUM('SELF_PACED', 'LIVE_CLASS');--> statement-breakpoint
-- CREATE TYPE "public"."COURSE_VERSION" AS ENUM('V1', 'V2');--> statement-breakpoint
-- CREATE TYPE "public"."LOCALE" AS ENUM('en', 'hi', 'fr', 'pt', 'de', 'vi', 'ru', 'es', 'pl', 'da');--> statement-breakpoint
-- CREATE TYPE "public"."PLAN" AS ENUM('EARLY_ADOPTER', 'ENTERPRISE', 'BASIC');--> statement-breakpoint
-- CREATE TABLE "analytics_login_events" (
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "user_id" uuid NOT NULL,
-- "logged_in_at" timestamp with time zone DEFAULT now(),
-- CONSTRAINT "analytics_login_events_user_id_unique" UNIQUE("user_id")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "analytics_login_events" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "apps_poll" (
-- "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "question" text,
-- "authorId" uuid,
-- "isPublic" boolean,
-- "status" varchar DEFAULT 'draft',
-- "expiration" timestamp with time zone,
-- "courseId" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "apps_poll" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "apps_poll_option" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "apps_poll_option_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "updated_at" timestamp with time zone,
-- "poll_id" uuid,
-- "label" varchar
-- );
-- --> statement-breakpoint
-- ALTER TABLE "apps_poll_option" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "apps_poll_submission" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "apps_poll_submision_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "poll_option_id" bigint,
-- "selected_by_id" uuid,
-- "poll_id" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "apps_poll_submission" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "community_answer" (
-- "id" uuid PRIMARY KEY DEFAULT extensions.gen_random_uuid() NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "question_id" bigint,
-- "body" varchar,
-- "author_id" bigint,
-- "votes" bigint,
-- "author_profile_id" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "community_answer" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "community_question" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "community_question_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now(),
-- "title" varchar,
-- "body" text,
-- "author_id" bigint,
-- "votes" bigint DEFAULT '0',
-- "organization_id" uuid,
-- "slug" text,
-- "author_profile_id" uuid,
-- "course_id" uuid NOT NULL
-- );
-- --> statement-breakpoint
-- ALTER TABLE "community_question" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "course" (
-- "title" varchar NOT NULL,
-- "description" varchar NOT NULL,
-- "overview" varchar DEFAULT 'Welcome to this amazing course 🚀 ',
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "group_id" uuid,
-- "is_template" boolean DEFAULT true,
-- "logo" text DEFAULT '' NOT NULL,
-- "slug" varchar,
-- "metadata" jsonb DEFAULT '{"goals":"","description":"","requirements":""}'::jsonb NOT NULL,
-- "cost" bigint DEFAULT '0',
-- "currency" varchar DEFAULT 'USD' NOT NULL,
-- "banner_image" text,
-- "is_published" boolean DEFAULT false,
-- "is_certificate_downloadable" boolean DEFAULT false,
-- "certificate_theme" text,
-- "status" text DEFAULT 'ACTIVE' NOT NULL,
-- "type" "COURSE_TYPE" DEFAULT 'LIVE_CLASS',
-- "version" "COURSE_VERSION" DEFAULT 'V1' NOT NULL,
-- CONSTRAINT "course_slug_key" UNIQUE("slug")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "course" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "course_newsfeed" (
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "author_id" uuid,
-- "content" text,
-- "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
-- "course_id" uuid,
-- "reaction" jsonb DEFAULT '{"clap":[],"smile":[],"thumbsup":[],"thumbsdown":[]}'::jsonb,
-- "is_pinned" boolean DEFAULT false NOT NULL
-- );
-- --> statement-breakpoint
-- ALTER TABLE "course_newsfeed" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "course_newsfeed_comment" (
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "author_id" uuid,
-- "content" text,
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "course_newsfeed_comment_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "course_newsfeed_id" uuid,
-- CONSTRAINT "course_newsfeed_comment_id_key" UNIQUE("id")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "course_newsfeed_comment" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "currency" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "currency_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now(),
-- "name" varchar
-- );
-- --> statement-breakpoint
-- CREATE TABLE "exercise" (
-- "title" varchar NOT NULL,
-- "description" varchar,
-- "lesson_id" uuid,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "due_by" timestamp
-- );
-- --> statement-breakpoint
-- ALTER TABLE "exercise" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "group" (
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "name" varchar NOT NULL,
-- "description" text,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "organization_id" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "group" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "group_attendance" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "group_attendance_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "course_id" uuid,
-- "student_id" uuid,
-- "is_present" boolean DEFAULT false,
-- "lesson_id" uuid NOT NULL
-- );
-- --> statement-breakpoint
-- ALTER TABLE "group_attendance" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "groupmember" (
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "group_id" uuid NOT NULL,
-- "role_id" bigint NOT NULL,
-- "profile_id" uuid,
-- "email" varchar,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "assigned_student_id" varchar,
-- CONSTRAINT "unique_entries" UNIQUE("group_id","profile_id","email"),
-- CONSTRAINT "unique_group_email" UNIQUE("group_id","email"),
-- CONSTRAINT "unique_group_profile" UNIQUE("group_id","profile_id")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "groupmember" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "lesson" (
-- "note" varchar,
-- "video_url" varchar,
-- "slide_url" varchar,
-- "course_id" uuid NOT NULL,
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "title" varchar NOT NULL,
-- "public" boolean DEFAULT false,
-- "lesson_at" timestamp with time zone DEFAULT now(),
-- "teacher_id" uuid,
-- "is_complete" boolean DEFAULT false,
-- "call_url" text,
-- "order" bigint,
-- "is_unlocked" boolean DEFAULT false,
-- "videos" jsonb DEFAULT '[]'::jsonb,
-- "section_id" uuid,
-- "documents" jsonb DEFAULT '[]'::jsonb
-- );
-- --> statement-breakpoint
-- ALTER TABLE "lesson" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "lesson_comment" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "lesson_comment_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "lesson_id" uuid,
-- "groupmember_id" uuid,
-- "comment" text
-- );
-- --> statement-breakpoint
-- ALTER TABLE "lesson_comment" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "lesson_completion" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "lesson_completion_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "lesson_id" uuid,
-- "profile_id" uuid,
-- "is_complete" boolean DEFAULT false,
-- "updated_at" timestamp with time zone DEFAULT now(),
-- CONSTRAINT "unique_lesson_profile" UNIQUE("lesson_id","profile_id")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "lesson_completion" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "lesson_language" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "lesson_language_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "content" text,
-- "lesson_id" uuid DEFAULT gen_random_uuid(),
-- "locale" "LOCALE" DEFAULT 'en'
-- );
-- --> statement-breakpoint
-- ALTER TABLE "lesson_language" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "lesson_language_history" (
-- "id" serial PRIMARY KEY NOT NULL,
-- "lesson_language_id" integer,
-- "old_content" text,
-- "new_content" text,
-- "timestamp" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL
-- );
-- --> statement-breakpoint
-- ALTER TABLE "lesson_language_history" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "lesson_section" (
-- "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "title" varchar,
-- "order" bigint DEFAULT '0',
-- "course_id" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "lesson_section" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "option" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "option_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "label" varchar NOT NULL,
-- "is_correct" boolean DEFAULT false NOT NULL,
-- "question_id" bigint NOT NULL,
-- "value" uuid DEFAULT extensions.gen_random_uuid(),
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now()
-- );
-- --> statement-breakpoint
-- ALTER TABLE "option" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "organization" (
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "name" varchar NOT NULL,
-- "siteName" text,
-- "avatar_url" text,
-- "settings" jsonb DEFAULT '{}'::jsonb,
-- "landingpage" jsonb DEFAULT '{}'::jsonb,
-- "theme" text,
-- "created_at" timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
-- "customization" json DEFAULT '{"apps":{"poll":true,"comments":true},"course":{"grading":true,"newsfeed":true},"dashboard":{"exercise":true,"community":true,"bannerText":"","bannerImage":""}}'::json NOT NULL,
-- "is_restricted" boolean DEFAULT false NOT NULL,
-- "customCode" text,
-- "customDomain" text,
-- "favicon" text,
-- "isCustomDomainVerified" boolean DEFAULT false,
-- CONSTRAINT "organization_siteName_key" UNIQUE("siteName"),
-- CONSTRAINT "organization_customDomain_key" UNIQUE("customDomain")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "organization" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "organization_contacts" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "organization_contacts_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "email" text,
-- "phone" text,
-- "name" text,
-- "message" text,
-- "organization_id" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "organization_contacts" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "organization_emaillist" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "organization_emaillist_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "email" text,
-- "organization_id" uuid
-- );
-- --> statement-breakpoint
-- ALTER TABLE "organization_emaillist" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "organization_plan" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "organization_plan_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "activated_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "org_id" uuid,
-- "plan_name" "PLAN",
-- "is_active" boolean,
-- "deactivated_at" timestamp with time zone,
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "payload" jsonb,
-- "triggered_by" bigint,
-- "provider" text DEFAULT 'lmz',
-- "subscription_id" text,
-- CONSTRAINT "organization_plan_subscription_id_key" UNIQUE("subscription_id")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "organization_plan" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "organizationmember" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "organizationmember_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "organization_id" uuid NOT NULL,
-- "role_id" bigint NOT NULL,
-- "profile_id" uuid,
-- "email" text,
-- "verified" boolean DEFAULT false,
-- "created_at" timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
-- );
-- --> statement-breakpoint
-- ALTER TABLE "organizationmember" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "profile" (
-- "id" uuid PRIMARY KEY NOT NULL,
-- "fullname" text NOT NULL,
-- "username" text NOT NULL,
-- "avatar_url" text DEFAULT 'https://tapaozmyjsuykgerrfkt.supabase.co/storage/v1/object/public/avatars/avatar.png',
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "email" varchar,
-- "can_add_course" boolean DEFAULT true,
-- "role" varchar,
-- "goal" varchar,
-- "source" varchar,
-- "metadata" json,
-- "telegram_chat_id" bigint,
-- "is_email_verified" boolean DEFAULT false,
-- "verified_at" timestamp with time zone,
-- "locale" "LOCALE" DEFAULT 'en',
-- "is_restricted" boolean DEFAULT false NOT NULL,
-- CONSTRAINT "profile_username_key" UNIQUE("username"),
-- CONSTRAINT "profile_email_key" UNIQUE("email")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "profile" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "question" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "question_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "question_type_id" bigint NOT NULL,
-- "title" varchar NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "exercise_id" uuid NOT NULL,
-- "name" uuid DEFAULT extensions.gen_random_uuid(),
-- "points" double precision,
-- "order" bigint
-- );
-- --> statement-breakpoint
-- ALTER TABLE "question" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "question_answer" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "question_answer_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "answers" varchar[],
-- "question_id" bigint NOT NULL,
-- "open_answer" text,
-- "group_member_id" uuid NOT NULL,
-- "submission_id" uuid,
-- "point" bigint DEFAULT '0'
-- );
-- --> statement-breakpoint
-- ALTER TABLE "question_answer" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "question_type" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "question_type_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "label" varchar NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "typename" varchar
-- );
-- --> statement-breakpoint
-- ALTER TABLE "question_type" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "quiz" (
-- "id" uuid PRIMARY KEY DEFAULT extensions.gen_random_uuid() NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "title" text,
-- "questions" json,
-- "timelimit" varchar DEFAULT '10s',
-- "theme" varchar DEFAULT 'standard',
-- "organization_id" uuid NOT NULL
-- );
-- --> statement-breakpoint
-- ALTER TABLE "quiz" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "quiz_play" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "play_quiz_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "quiz_id" uuid,
-- "players" json DEFAULT '[]'::json,
-- "started" boolean DEFAULT false,
-- "currentQuestionId" bigint DEFAULT '0',
-- "showCurrentQuestionAnswer" boolean DEFAULT false,
-- "isLastQuestion" boolean,
-- "step" text DEFAULT 'CONNECT_TO_PLAY',
-- "studentStep" text DEFAULT 'PIN_SETUP',
-- "pin" text,
-- CONSTRAINT "quiz_play_pin_key" UNIQUE("pin")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "quiz_play" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "role" (
-- "type" varchar NOT NULL,
-- "description" varchar,
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "role_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "created_at" timestamp with time zone DEFAULT now()
-- );
-- --> statement-breakpoint
-- ALTER TABLE "role" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "submission" (
-- "id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
-- "reviewer_id" bigint,
-- "status_id" bigint DEFAULT '1',
-- "total" bigint DEFAULT '0',
-- "created_at" timestamp with time zone DEFAULT now(),
-- "updated_at" timestamp with time zone DEFAULT now(),
-- "exercise_id" uuid NOT NULL,
-- "submitted_by" uuid,
-- "course_id" uuid,
-- "feedback" text
-- );
-- --> statement-breakpoint
-- ALTER TABLE "submission" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "submissionstatus" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "submission_status_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "label" varchar NOT NULL,
-- "updated_at" timestamp with time zone DEFAULT now()
-- );
-- --> statement-breakpoint
-- ALTER TABLE "submissionstatus" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
-- CREATE TABLE "test_tenant" (
-- "id" serial PRIMARY KEY NOT NULL,
-- "details" text
-- );
-- --> statement-breakpoint
-- CREATE TABLE "video_transcripts" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "video_transcripts_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "created_at" timestamp with time zone DEFAULT now() NOT NULL,
-- "muse_svid" text,
-- "transcript" text,
-- "downloaded" boolean DEFAULT false,
-- "link" text
-- );
-- --> statement-breakpoint
-- CREATE TABLE "waitinglist" (
-- "id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "waitinglist_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854776000 START WITH 1 CACHE 1),
-- "email" varchar NOT NULL,
-- "created_at" timestamp with time zone DEFAULT now(),
-- CONSTRAINT "constraint_name" UNIQUE("email")
-- );
-- --> statement-breakpoint
-- ALTER TABLE "analytics_login_events" ADD CONSTRAINT "analytics_login_events_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "apps_poll" ADD CONSTRAINT "apps_poll_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "apps_poll" ADD CONSTRAINT "apps_poll_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "apps_poll_option" ADD CONSTRAINT "apps_poll_option_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "public"."apps_poll"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "apps_poll_submission" ADD CONSTRAINT "apps_poll_submission_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "public"."apps_poll"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "apps_poll_submission" ADD CONSTRAINT "apps_poll_submission_poll_option_id_fkey" FOREIGN KEY ("poll_option_id") REFERENCES "public"."apps_poll_option"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "apps_poll_submission" ADD CONSTRAINT "apps_poll_submission_selected_by_id_fkey" FOREIGN KEY ("selected_by_id") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_answer" ADD CONSTRAINT "community_answer_author_id_fkey" FOREIGN KEY ("author_id") REFERENCES "public"."organizationmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_answer" ADD CONSTRAINT "community_answer_author_profile_id_fkey" FOREIGN KEY ("author_profile_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_answer" ADD CONSTRAINT "community_answer_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."community_question"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_question" ADD CONSTRAINT "community_question_author_id_fkey" FOREIGN KEY ("author_id") REFERENCES "public"."organizationmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_question" ADD CONSTRAINT "community_question_author_profile_id_fkey" FOREIGN KEY ("author_profile_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_question" ADD CONSTRAINT "community_question_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "community_question" ADD CONSTRAINT "community_question_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "course" ADD CONSTRAINT "course_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "course_newsfeed" ADD CONSTRAINT "course_newsfeed_author_id_fkey" FOREIGN KEY ("author_id") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "course_newsfeed" ADD CONSTRAINT "course_newsfeed_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "course_newsfeed_comment" ADD CONSTRAINT "course_newsfeed_comment_author_id_fkey" FOREIGN KEY ("author_id") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "course_newsfeed_comment" ADD CONSTRAINT "course_newsfeed_comment_course_newsfeed_id_fkey" FOREIGN KEY ("course_newsfeed_id") REFERENCES "public"."course_newsfeed"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "exercise" ADD CONSTRAINT "exercise_lesson_id_fkey" FOREIGN KEY ("lesson_id") REFERENCES "public"."lesson"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "group" ADD CONSTRAINT "group_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "group_attendance" ADD CONSTRAINT "group_attendance_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "group_attendance" ADD CONSTRAINT "group_attendance_student_id_fkey" FOREIGN KEY ("student_id") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "groupmember" ADD CONSTRAINT "groupmember_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "groupmember" ADD CONSTRAINT "groupmember_profile_id_fkey" FOREIGN KEY ("profile_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "groupmember" ADD CONSTRAINT "groupmember_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson" ADD CONSTRAINT "lesson_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson" ADD CONSTRAINT "lesson_teacher_id_fkey" FOREIGN KEY ("teacher_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson" ADD CONSTRAINT "public_lesson_section_id_fkey" FOREIGN KEY ("section_id") REFERENCES "public"."lesson_section"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
-- ALTER TABLE "lesson_comment" ADD CONSTRAINT "lesson_comment_groupmember_id_fkey" FOREIGN KEY ("groupmember_id") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson_comment" ADD CONSTRAINT "lesson_comment_lesson_id_fkey" FOREIGN KEY ("lesson_id") REFERENCES "public"."lesson"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson_completion" ADD CONSTRAINT "lesson_completion_lesson_id_fkey" FOREIGN KEY ("lesson_id") REFERENCES "public"."lesson"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson_completion" ADD CONSTRAINT "lesson_completion_profile_id_fkey" FOREIGN KEY ("profile_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "lesson_language" ADD CONSTRAINT "public_lesson_language_lesson_id_fkey" FOREIGN KEY ("lesson_id") REFERENCES "public"."lesson"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
-- ALTER TABLE "lesson_language_history" ADD CONSTRAINT "public_lesson_language_history_lesson_language_id_fkey" FOREIGN KEY ("lesson_language_id") REFERENCES "public"."lesson_language"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
-- ALTER TABLE "lesson_section" ADD CONSTRAINT "public_lesson_section_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
-- ALTER TABLE "option" ADD CONSTRAINT "option_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."question"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organization_contacts" ADD CONSTRAINT "organization_contacts_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organization_emaillist" ADD CONSTRAINT "organization_emaillist_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organization_plan" ADD CONSTRAINT "organization_plan_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organization_plan" ADD CONSTRAINT "organization_plan_triggered_by_fkey" FOREIGN KEY ("triggered_by") REFERENCES "public"."organizationmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organizationmember" ADD CONSTRAINT "organizationmember_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organizationmember" ADD CONSTRAINT "organizationmember_profile_id_fkey" FOREIGN KEY ("profile_id") REFERENCES "public"."profile"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "organizationmember" ADD CONSTRAINT "organizationmember_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "profile" ADD CONSTRAINT "profile_id_fkey" FOREIGN KEY ("id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "question" ADD CONSTRAINT "question_exercise_id_fkey" FOREIGN KEY ("exercise_id") REFERENCES "public"."exercise"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "question" ADD CONSTRAINT "question_question_type_id_fkey" FOREIGN KEY ("question_type_id") REFERENCES "public"."question_type"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "question_answer" ADD CONSTRAINT "question_answer_group_member_id_fkey" FOREIGN KEY ("group_member_id") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "question_answer" ADD CONSTRAINT "question_answer_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."question"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "question_answer" ADD CONSTRAINT "question_answer_submission_id_fkey" FOREIGN KEY ("submission_id") REFERENCES "public"."submission"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "quiz" ADD CONSTRAINT "quiz_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "quiz_play" ADD CONSTRAINT "quiz_play_quiz_id_fkey" FOREIGN KEY ("quiz_id") REFERENCES "public"."quiz"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "submission" ADD CONSTRAINT "submission_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "submission" ADD CONSTRAINT "submission_exercise_id_fkey" FOREIGN KEY ("exercise_id") REFERENCES "public"."exercise"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "submission" ADD CONSTRAINT "submission_status_id_fkey" FOREIGN KEY ("status_id") REFERENCES "public"."submissionstatus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- ALTER TABLE "submission" ADD CONSTRAINT "submission_submitted_by_fkey" FOREIGN KEY ("submitted_by") REFERENCES "public"."groupmember"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
-- CREATE INDEX "idx_analytics_login_events_logged_in_at" ON "analytics_login_events" USING btree ("logged_in_at" timestamptz_ops);--> statement-breakpoint
-- CREATE INDEX "idx_analytics_login_events_user_id" ON "analytics_login_events" USING btree ("user_id" uuid_ops);--> statement-breakpoint
-- CREATE VIEW "public"."dash_org_stats" AS (SELECT gp.organization_id AS org_id, count(DISTINCT course.id) AS no_of_courses, count(DISTINCT gm.profile_id) AS enrolled_students FROM course JOIN "group" gp ON gp.id = course.group_id LEFT JOIN groupmember gm ON gm.group_id = gp.id AND gm.role_id = 3 WHERE course.status = 'ACTIVE'::text GROUP BY gp.organization_id);--> statement-breakpoint
-- CREATE VIEW "public"."lesson_versions" AS (SELECT llh.old_content, llh.new_content, llh."timestamp", ll.locale, ll.lesson_id FROM lesson_language_history llh JOIN lesson_language ll ON ll.id = llh.lesson_language_id);--> statement-breakpoint
-- CREATE POLICY "Enable delete for users based on user_id" ON "analytics_login_events" AS PERMISSIVE FOR DELETE TO public USING ((auth.uid() = user_id));--> statement-breakpoint
-- CREATE POLICY "Users can insert their own login events" ON "analytics_login_events" AS PERMISSIVE FOR INSERT TO "authenticated";--> statement-breakpoint
-- CREATE POLICY "Delete only your own poll" ON "apps_poll" AS PERMISSIVE FOR DELETE TO public USING ((auth.uid() = ( SELECT groupmember.profile_id
-- FROM groupmember
-- WHERE (groupmember.id = apps_poll."authorId"))));--> statement-breakpoint
-- CREATE POLICY "Update only your own" ON "apps_poll" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to INSERT" ON "apps_poll" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be course member to SELECT" ON "apps_poll" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to INSERT" ON "apps_poll_option" AS PERMISSIVE FOR INSERT TO public WITH CHECK (is_user_in_course_group_or_admin(( SELECT groupmember.group_id
-- FROM groupmember
-- WHERE (groupmember.id = ( SELECT apps_poll."authorId"
-- FROM apps_poll
-- WHERE (apps_poll.id = apps_poll_option.poll_id)
-- LIMIT 1))
-- LIMIT 1)));--> statement-breakpoint
-- CREATE POLICY "User must be a course member to UPDATE" ON "apps_poll_option" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a teacher to DELETE" ON "apps_poll_option" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be course member to SELECT" ON "apps_poll_option" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "Authenticated users can read" ON "apps_poll_submission" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "Delete your own submission" ON "apps_poll_submission" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "apps_poll_submission" AS PERMISSIVE FOR INSERT TO "authenticated";--> statement-breakpoint
-- CREATE POLICY "Update your own submission" ON "apps_poll_submission" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Authenticated users can SELECT" ON "community_answer" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "Delete your own answer" ON "community_answer" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "community_answer" AS PERMISSIVE FOR INSERT TO "authenticated";--> statement-breakpoint
-- CREATE POLICY "Update your own answer" ON "community_answer" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Authenticated users can SELECT" ON "community_question" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "Delete your own question" ON "community_question" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "community_question" AS PERMISSIVE FOR INSERT TO "authenticated";--> statement-breakpoint
-- CREATE POLICY "Update your own question" ON "community_question" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable access to all users if PUBLIC or to course members when " ON "course" AS PERMISSIVE FOR SELECT TO public USING ((is_published OR is_user_in_course_group_or_admin(group_id)));--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "course" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "course" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "course" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Delete your own comment" ON "course_newsfeed" AS PERMISSIVE FOR DELETE TO public USING ((auth.uid() = ( SELECT groupmember.profile_id
-- FROM groupmember
-- WHERE (groupmember.id = course_newsfeed.author_id))));--> statement-breakpoint
-- CREATE POLICY "Update only your own" ON "course_newsfeed" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to INSERT" ON "course_newsfeed" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to SELECT" ON "course_newsfeed" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "Delete your own" ON "course_newsfeed_comment" AS PERMISSIVE FOR DELETE TO public USING ((auth.uid() = ( SELECT groupmember.profile_id
-- FROM groupmember
-- WHERE (groupmember.id = course_newsfeed_comment.author_id))));--> statement-breakpoint
-- CREATE POLICY "Update only your own" ON "course_newsfeed_comment" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to INSERT" ON "course_newsfeed_comment" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to SELECT" ON "course_newsfeed_comment" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "exercise" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "exercise" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "exercise" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "exercise" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "group" AS PERMISSIVE FOR INSERT TO "authenticated" WITH CHECK (true);--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "group" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "Only org admins can delete" ON "group" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Only org admins can update" ON "group" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to INSERT" ON "group_attendance" AS PERMISSIVE FOR INSERT TO public WITH CHECK (is_user_in_course_group_or_admin(( SELECT course.group_id
-- FROM course
-- WHERE (course.id = group_attendance.course_id)
-- LIMIT 1)));--> statement-breakpoint
-- CREATE POLICY "User must be a course member to SELECT" ON "group_attendance" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to UPDATE" ON "group_attendance" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "group_attendance" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "groupmember" AS PERMISSIVE FOR INSERT TO "authenticated" WITH CHECK (true);--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "groupmember" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "groupmember" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "groupmember" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "lesson" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "lesson" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "lesson" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "lesson" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Delete only your own comment" ON "lesson_comment" AS PERMISSIVE FOR DELETE TO public USING ((auth.uid() = ( SELECT groupmember.profile_id
-- FROM groupmember
-- WHERE (groupmember.id = lesson_comment.groupmember_id))));--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "lesson_comment" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "Update only your own" ON "lesson_comment" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be in course group to INSERT" ON "lesson_comment" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "lesson_completion" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be an course member or Admin to perform ALL operation" ON "lesson_completion" AS PERMISSIVE FOR ALL TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "lesson_completion" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "lesson_completion" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "lesson_language" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "lesson_language" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "lesson_language" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "lesson_language" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Authenticated users can SELECT" ON "lesson_language_history" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "lesson_language_history" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "lesson_language_history" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "lesson_language_history" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "lesson_section" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "lesson_section" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "lesson_section" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "lesson_section" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Allow authenticated users to SELECT" ON "option" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "option" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "option" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "option" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Authenticated users can delete" ON "organization" AS PERMISSIVE FOR DELETE TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "organization" AS PERMISSIVE FOR INSERT TO "authenticated";--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "organization" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be admin to UPDATE" ON "organization" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "organization_contacts" AS PERMISSIVE FOR INSERT TO public WITH CHECK (true);--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "organization_emaillist" AS PERMISSIVE FOR INSERT TO public WITH CHECK (true);--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "organization_plan" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "organization_plan" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "organization_plan" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "organization_plan" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Allow authenticated users to read." ON "organizationmember" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "Enable insert for authenticated users only" ON "organizationmember" AS PERMISSIVE FOR INSERT TO "authenticated";--> statement-breakpoint
-- CREATE POLICY "Only admin can delete" ON "organizationmember" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Only admin can update" ON "organizationmember" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Only user can update their account via email" ON "organizationmember" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an admin to INSERT or allow if no existing member" ON "organizationmember" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "Only auth users can read profile" ON "profile" AS PERMISSIVE FOR SELECT TO "anon", "authenticated" USING (true);--> statement-breakpoint
-- CREATE POLICY "Public profiles are viewable by everyone." ON "profile" AS PERMISSIVE FOR SELECT TO public;--> statement-breakpoint
-- CREATE POLICY "User can only delete their profiles" ON "profile" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "Users can insert their own profile." ON "profile" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "Users can update own profile." ON "profile" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Allow authenticated users to SELECT" ON "question" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "User must be an org member to DELETE" ON "question" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to INSERT" ON "question" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an org member to UPDATE" ON "question" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Only authenticated users can select." ON "question_answer" AS PERMISSIVE FOR SELECT TO public USING ((auth.uid() IS NOT NULL));--> statement-breakpoint
-- CREATE POLICY "User must be an course member to DELETE" ON "question_answer" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an course member to INSERT" ON "question_answer" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be an course member to UPDATE" ON "question_answer" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "question_type" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "Authenticated users can SELECT" ON "quiz" AS PERMISSIVE FOR SELECT TO "authenticated" USING (true);--> statement-breakpoint
-- CREATE POLICY "Authenticated users can SELECT" ON "quiz_play" AS PERMISSIVE FOR SELECT TO "authenticated" USING (true);--> statement-breakpoint
-- CREATE POLICY "Enable read access for all users" ON "role" AS PERMISSIVE FOR SELECT TO public USING (true);--> statement-breakpoint
-- CREATE POLICY "Only authenticated users can SELECT" ON "submission" AS PERMISSIVE FOR SELECT TO "authenticated" USING (true);--> statement-breakpoint
-- CREATE POLICY "User must be a course member to DELETE" ON "submission" AS PERMISSIVE FOR DELETE TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to INSERT" ON "submission" AS PERMISSIVE FOR INSERT TO public;--> statement-breakpoint
-- CREATE POLICY "User must be a course member to UPDATE" ON "submission" AS PERMISSIVE FOR UPDATE TO public;--> statement-breakpoint
-- CREATE POLICY "Authenticated users can SELECT" ON "submissionstatus" AS PERMISSIVE FOR SELECT TO "authenticated" USING (true);
select id from public.profile limit 1;
@@ -1,47 +0,0 @@
CREATE TABLE "public"."account" (
"id" text PRIMARY KEY NOT NULL,
"account_id" text NOT NULL,
"provider_id" text NOT NULL,
"user_id" text NOT NULL,
"access_token" text,
"refresh_token" text,
"id_token" text,
"access_token_expires_at" timestamp,
"refresh_token_expires_at" timestamp,
"scope" text,
"password" text,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE "public"."session" (
"id" text PRIMARY KEY NOT NULL,
"expires_at" timestamp NOT NULL,
"token" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp NOT NULL,
"ip_address" text,
"user_agent" text,
"user_id" text NOT NULL,
"impersonated_by" text,
CONSTRAINT "session_token_unique" UNIQUE("token")
);
--> statement-breakpoint
CREATE TABLE "public"."user" (
"id" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"email" text NOT NULL,
"email_verified" boolean DEFAULT false NOT NULL,
"image" text,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
"role" text,
"banned" boolean DEFAULT false,
"ban_reason" text,
"ban_expires" timestamp,
"is_anonymous" boolean,
CONSTRAINT "user_email_unique" UNIQUE("email")
);
--> statement-breakpoint
ALTER TABLE "public"."account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "public"."session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
@@ -1,20 +0,0 @@
CREATE TABLE IF NOT EXISTS "public"."verification" (
"id" uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
"identifier" text NOT NULL,
"value" text NOT NULL,
"expires_at" timestamp NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "public"."account" DROP CONSTRAINT IF EXISTS "account_user_id_user_id_fk";--> statement-breakpoint
ALTER TABLE "public"."session" DROP CONSTRAINT IF EXISTS "session_user_id_user_id_fk";--> statement-breakpoint
ALTER TABLE "public"."user" ALTER COLUMN "id" SET DATA TYPE uuid USING id::uuid;--> statement-breakpoint
ALTER TABLE "public"."user" ALTER COLUMN "id" SET DEFAULT uuid_generate_v4();--> statement-breakpoint
ALTER TABLE "public"."account" ALTER COLUMN "id" SET DEFAULT uuid_generate_v4();--> statement-breakpoint
ALTER TABLE "public"."account" ALTER COLUMN "user_id" SET DATA TYPE uuid USING user_id::uuid;--> statement-breakpoint
ALTER TABLE "public"."account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "public"."session" ALTER COLUMN "id" SET DATA TYPE uuid USING id::uuid;--> statement-breakpoint
ALTER TABLE "public"."session" ALTER COLUMN "id" SET DEFAULT uuid_generate_v4();--> statement-breakpoint
ALTER TABLE "public"."session" ALTER COLUMN "user_id" SET DATA TYPE uuid USING user_id::uuid;--> statement-breakpoint
ALTER TABLE "public"."session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
@@ -1,306 +0,0 @@
CREATE TABLE IF NOT EXISTS "public"."exercise_template" (
"id" bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "exercise_template_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"title" text,
"description" text,
"tag" text,
"questionnaire" jsonb DEFAULT '{}'::jsonb
);
--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."analytics_login_events" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."apps_poll" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."apps_poll_option" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."apps_poll_submission" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."community_answer" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."community_question" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."course" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."course_newsfeed" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."course_newsfeed_comment" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson_section" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."course_section" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."exercise" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."group" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."group_attendance" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."groupmember" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson_comment" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson_completion" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson_language" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson_language_history" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."option" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."organization" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."organization_contacts" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."organization_emaillist" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."organization_plan" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."organizationmember" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."profile" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."question" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."question_answer" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."question_type" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."quiz" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."quiz_play" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."role" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."submission" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."submissionstatus" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE "public"."lesson" DROP CONSTRAINT IF EXISTS "public_lesson_section_id_fkey";
--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."lesson_section" DROP CONSTRAINT IF EXISTS "public_lesson_section_course_id_fkey";--> statement-breakpoint
DO $migrate_rename_lesson_section_to_course_section$ BEGIN
IF EXISTS (
SELECT 1
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public' AND c.relkind = 'r' AND c.relname = 'lesson_section'
) AND NOT EXISTS (
SELECT 1
FROM pg_class c2
JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
WHERE n2.nspname = 'public' AND c2.relkind = 'r' AND c2.relname = 'course_section'
) THEN
ALTER TABLE public.lesson_section RENAME TO course_section;
END IF;
END;
$migrate_rename_lesson_section_to_course_section$;--> statement-breakpoint
ALTER TABLE IF EXISTS "public"."course_newsfeed_comment" DROP CONSTRAINT IF EXISTS "course_newsfeed_comment_id_key";--> statement-breakpoint
ALTER TABLE "public"."account" ALTER COLUMN "id" SET DATA TYPE uuid USING id::uuid;--> statement-breakpoint
ALTER TABLE "public"."account" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."analytics_login_events" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."community_answer" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."course" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."exercise" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."group" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."groupmember" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."lesson" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."option" ALTER COLUMN "value" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."organization" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."question" ALTER COLUMN "name" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."quiz" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."session" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."submission" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."user" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."verification" ALTER COLUMN "id" SET DATA TYPE uuid USING id::uuid;--> statement-breakpoint
ALTER TABLE "public"."verification" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "public"."exercise" ADD COLUMN "course_id" uuid;--> statement-breakpoint
ALTER TABLE "public"."exercise" ADD COLUMN "section_id" uuid;--> statement-breakpoint
ALTER TABLE "public"."exercise" ADD COLUMN "order" bigint;--> statement-breakpoint
ALTER TABLE "public"."exercise" ADD COLUMN "is_unlocked" boolean DEFAULT true;--> statement-breakpoint
DO $add_exercise_course_fk$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'exercise' AND schemaname = 'public')
AND NOT EXISTS (
SELECT 1 FROM pg_constraint co
JOIN pg_class rel ON rel.oid = co.conrelid
JOIN pg_namespace n ON n.oid = rel.relnamespace
WHERE n.nspname = 'public' AND rel.relname = 'exercise' AND co.contype = 'f' AND co.conname = 'exercise_course_id_fkey'
) THEN
ALTER TABLE public.exercise ADD CONSTRAINT exercise_course_id_fkey FOREIGN KEY ("course_id") REFERENCES public.course("id") ON DELETE no action ON UPDATE no action;
END IF;
END;
$add_exercise_course_fk$;--> statement-breakpoint
DO $add_exercise_section_fk$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'exercise' AND schemaname = 'public')
AND NOT EXISTS (
SELECT 1 FROM pg_constraint co
JOIN pg_class rel ON rel.oid = co.conrelid
JOIN pg_namespace n ON n.oid = rel.relnamespace
WHERE n.nspname = 'public' AND rel.relname = 'exercise' AND co.contype = 'f' AND co.conname = 'exercise_section_id_fkey'
) THEN
ALTER TABLE public.exercise ADD CONSTRAINT exercise_section_id_fkey FOREIGN KEY ("section_id") REFERENCES public.course_section("id") ON DELETE no action ON UPDATE no action;
END IF;
END;
$add_exercise_section_fk$;--> statement-breakpoint
DO $add_lesson_course_section_fk$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'lesson' AND schemaname = 'public')
AND NOT EXISTS (
SELECT 1 FROM pg_constraint co
JOIN pg_class rel ON rel.oid = co.conrelid
JOIN pg_namespace n ON n.oid = rel.relnamespace
WHERE n.nspname = 'public' AND rel.relname = 'lesson' AND co.contype = 'f' AND co.conname = 'public_course_section_id_fkey'
) THEN
ALTER TABLE public.lesson ADD CONSTRAINT public_course_section_id_fkey FOREIGN KEY ("section_id") REFERENCES public.course_section("id") ON DELETE cascade ON UPDATE cascade;
END IF;
END;
$add_lesson_course_section_fk$;--> statement-breakpoint
DO $add_course_section_course_fk$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'course_section' AND schemaname = 'public')
AND NOT EXISTS (
SELECT 1 FROM pg_constraint co
JOIN pg_class rel ON rel.oid = co.conrelid
JOIN pg_namespace n ON n.oid = rel.relnamespace
WHERE n.nspname = 'public' AND rel.relname = 'course_section' AND co.contype = 'f' AND co.conname = 'public_course_section_course_id_fkey'
) THEN
ALTER TABLE public.course_section ADD CONSTRAINT public_course_section_course_id_fkey FOREIGN KEY ("course_id") REFERENCES public.course("id") ON DELETE cascade ON UPDATE cascade;
END IF;
END;
$add_course_section_course_fk$;--> statement-breakpoint
ALTER TABLE "public"."course" DROP COLUMN "version";--> statement-breakpoint
DROP POLICY IF EXISTS "Enable delete for users based on user_id" ON "public"."analytics_login_events" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Users can insert their own login events" ON "public"."analytics_login_events" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete only your own poll" ON "public"."apps_poll" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update only your own" ON "public"."apps_poll" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to INSERT" ON "public"."apps_poll" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be course member to SELECT" ON "public"."apps_poll" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to INSERT" ON "public"."apps_poll_option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to UPDATE" ON "public"."apps_poll_option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a teacher to DELETE" ON "public"."apps_poll_option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be course member to SELECT" ON "public"."apps_poll_option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can read" ON "public"."apps_poll_submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete your own submission" ON "public"."apps_poll_submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."apps_poll_submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update your own submission" ON "public"."apps_poll_submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can SELECT" ON "public"."community_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete your own answer" ON "public"."community_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."community_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update your own answer" ON "public"."community_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can SELECT" ON "public"."community_question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete your own question" ON "public"."community_question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."community_question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update your own question" ON "public"."community_question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable access to all users if PUBLIC or to course members when " ON "public"."course" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."course" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."course" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."course" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete your own comment" ON "public"."course_newsfeed" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update only your own" ON "public"."course_newsfeed" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to INSERT" ON "public"."course_newsfeed" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to SELECT" ON "public"."course_newsfeed" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete your own" ON "public"."course_newsfeed_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update only your own" ON "public"."course_newsfeed_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to INSERT" ON "public"."course_newsfeed_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to SELECT" ON "public"."course_newsfeed_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."exercise" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."exercise" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."exercise" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."exercise" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."group" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."group" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only org admins can delete" ON "public"."group" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only org admins can update" ON "public"."group" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to INSERT" ON "public"."group_attendance" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to SELECT" ON "public"."group_attendance" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to UPDATE" ON "public"."group_attendance" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."group_attendance" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."groupmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."groupmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."groupmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."groupmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."lesson" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."lesson" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."lesson" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."lesson" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Delete only your own comment" ON "public"."lesson_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."lesson_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Update only your own" ON "public"."lesson_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be in course group to INSERT" ON "public"."lesson_comment" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."lesson_completion" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an course member or Admin to perform ALL operation" ON "public"."lesson_completion" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."lesson_completion" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."lesson_completion" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."lesson_language" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."lesson_language" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."lesson_language" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."lesson_language" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can SELECT" ON "public"."lesson_language_history" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."lesson_language_history" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."lesson_language_history" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."lesson_language_history" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."course_section" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."course_section" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."course_section" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."course_section" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Allow authenticated users to SELECT" ON "public"."option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."option" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can delete" ON "public"."organization" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."organization" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."organization" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be admin to UPDATE" ON "public"."organization" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."organization_contacts" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."organization_emaillist" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."organization_plan" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."organization_plan" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."organization_plan" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."organization_plan" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Allow authenticated users to read." ON "public"."organizationmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON "public"."organizationmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only admin can delete" ON "public"."organizationmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only admin can update" ON "public"."organizationmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only user can update their account via email" ON "public"."organizationmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an admin to INSERT or allow if no existing member" ON "public"."organizationmember" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only auth users can read profile" ON "public"."profile" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Public profiles are viewable by everyone." ON "public"."profile" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User can only delete their profiles" ON "public"."profile" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Users can insert their own profile." ON "public"."profile" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Users can update own profile." ON "public"."profile" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Allow authenticated users to SELECT" ON "public"."question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to DELETE" ON "public"."question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to INSERT" ON "public"."question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an org member to UPDATE" ON "public"."question" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only authenticated users can select." ON "public"."question_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an course member to DELETE" ON "public"."question_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an course member to INSERT" ON "public"."question_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be an course member to UPDATE" ON "public"."question_answer" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."question_type" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can SELECT" ON "public"."quiz" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can SELECT" ON "public"."quiz_play" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Enable read access for all users" ON "public"."role" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Only authenticated users can SELECT" ON "public"."submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to DELETE" ON "public"."submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to INSERT" ON "public"."submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "User must be a course member to UPDATE" ON "public"."submission" CASCADE;--> statement-breakpoint
DROP POLICY IF EXISTS "Authenticated users can SELECT" ON "public"."submissionstatus" CASCADE;--> statement-breakpoint
DROP TYPE "public"."COURSE_VERSION";
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.group_attendance', 'id'), COALESCE((SELECT MAX(id) FROM public.group_attendance), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.currency', 'id'), COALESCE((SELECT MAX(id) FROM public.currency), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.apps_poll_option', 'id'), COALESCE((SELECT MAX(id) FROM public.apps_poll_option), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.option', 'id'), COALESCE((SELECT MAX(id) FROM public.option), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.quiz_play', 'id'), COALESCE((SELECT MAX(id) FROM public.quiz_play), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.organization_contacts', 'id'), COALESCE((SELECT MAX(id) FROM public.organization_contacts), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.lesson_comment', 'id'), COALESCE((SELECT MAX(id) FROM public.lesson_comment), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.submissionstatus', 'id'), COALESCE((SELECT MAX(id) FROM public.submissionstatus), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.video_transcripts', 'id'), COALESCE((SELECT MAX(id) FROM public.video_transcripts), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.organization_emaillist', 'id'), COALESCE((SELECT MAX(id) FROM public.organization_emaillist), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.apps_poll_submission', 'id'), COALESCE((SELECT MAX(id) FROM public.apps_poll_submission), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.lesson_completion', 'id'), COALESCE((SELECT MAX(id) FROM public.lesson_completion), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.community_question', 'id'), COALESCE((SELECT MAX(id) FROM public.community_question), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.course_newsfeed_comment', 'id'), COALESCE((SELECT MAX(id) FROM public.course_newsfeed_comment), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.organization_plan', 'id'), COALESCE((SELECT MAX(id) FROM public.organization_plan), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.lesson_language', 'id'), COALESCE((SELECT MAX(id) FROM public.lesson_language), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.lesson_language_history', 'id'), COALESCE((SELECT MAX(id) FROM public.lesson_language_history), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.organizationmember', 'id'), COALESCE((SELECT MAX(id) FROM public.organizationmember), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.question', 'id'), COALESCE((SELECT MAX(id) FROM public.question), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.question_answer', 'id'), COALESCE((SELECT MAX(id) FROM public.question_answer), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.question_type', 'id'), COALESCE((SELECT MAX(id) FROM public.question_type), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.role', 'id'), COALESCE((SELECT MAX(id) FROM public.role), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.waitinglist', 'id'), COALESCE((SELECT MAX(id) FROM public.waitinglist), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.test_tenant', 'id'), COALESCE((SELECT MAX(id) FROM public.test_tenant), 1), true);
--> statement-breakpoint
SELECT setval(pg_get_serial_sequence('public.exercise_template', 'id'), COALESCE((SELECT MAX(id) FROM public.exercise_template), 1), true);
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+3 -24
View File
@@ -5,30 +5,9 @@
{
"idx": 0,
"version": "7",
"when": 1760910273722,
"tag": "0000_stale_storm",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1760912065001,
"tag": "0001_tranquil_thunderbolts",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1762733677656,
"tag": "0002_many_revanche",
"breakpoints": true
},
{
"idx": 3,
"version": "7",
"when": 1769931903954,
"tag": "0003_fancy_harry_osborn",
"when": 1781617442553,
"tag": "0000_shocking_carmella_unuscione",
"breakpoints": true
}
]
}
}
+131
View File
@@ -0,0 +1,131 @@
import 'dotenv/config';
import crypto from 'node:crypto';
import fs from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import postgres, { type Sql } from 'postgres';
const scriptPath = fileURLToPath(import.meta.url);
// .../packages/db/src/scripts/baseline.ts → migrations live at ../migrations
const migrationsDir = resolve(dirname(scriptPath), '../migrations');
type JournalEntry = { idx: number; when: number; tag: string };
/**
* Reads the drizzle journal and computes each migration's hash the SAME way drizzle-orm's
* `readMigrationFiles` does: sha256 of the raw `<tag>.sql` file contents, paired with the
* journal `when` timestamp (drizzle's `created_at`).
*/
function readJournal(): { tag: string; when: number; hash: string }[] {
const journalPath = resolve(migrationsDir, 'meta/_journal.json');
const journal = JSON.parse(fs.readFileSync(journalPath, 'utf8')) as { entries: JournalEntry[] };
return journal.entries.map((entry) => {
const sqlText = fs.readFileSync(resolve(migrationsDir, `${entry.tag}.sql`), 'utf8');
return {
tag: entry.tag,
when: entry.when,
hash: crypto.createHash('sha256').update(sqlText).digest('hex')
};
});
}
/**
* Adopt an existing database into drizzle's migration history so `drizzle-kit migrate` is safe.
*
* drizzle applies every journal entry whose `when` is greater than the max `created_at` in
* `drizzle.__drizzle_migrations`. A database that already has the schema but no journal — created
* by the old `drizzle-kit push` or by the prior supabase setup — would make `migrate` re-run the
* baseline migration and fail with `type/table "..." already exists`. Seeding the journal marks
* those migrations as already applied so only *future* migrations run.
*
* No-ops when:
* - the journal already records the consolidated baseline migration, or
* - the `public` schema has no tables (fresh DB — let `migrate` create everything).
*
* Special case — migration squash: a DB tracked under an older journal (its `__drizzle_migrations`
* holds the pre-squash hashes) would otherwise let `migrate` re-run the consolidated baseline,
* whose `when` is newer than every old `created_at`, and crash on `CREATE TYPE/TABLE "..." already
* exists`. We adopt the new baseline (insert its hash) so only *future* migrations run.
*/
export async function baselineMigrationsIfNeeded(sql: Sql): Promise<void> {
const entries = readJournal();
const baseline = entries[0]; // idx 0 = the consolidated baseline migration
// 1. Already migrate-tracked?
const journalTable = await sql<{ count: number }[]>`
SELECT count(*)::int AS count
FROM information_schema.tables
WHERE table_schema = 'drizzle' AND table_name = '__drizzle_migrations'
`;
if (journalTable[0].count > 0) {
const rows = await sql<{ count: number }[]>`SELECT count(*)::int AS count FROM drizzle.__drizzle_migrations`;
if (rows[0].count > 0) {
// Has history. If the consolidated baseline hash is missing, this DB was tracked under the
// pre-squash journal — adopt the new baseline so `migrate` won't re-run it. Only the baseline
// entry is reconciled here; genuinely pending future migrations must still run.
const hasBaseline = await sql<{ count: number }[]>`
SELECT count(*)::int AS count FROM drizzle.__drizzle_migrations WHERE hash = ${baseline.hash}
`;
if (hasBaseline[0].count === 0) {
await sql`INSERT INTO drizzle.__drizzle_migrations ("hash", "created_at") VALUES (${baseline.hash}, ${baseline.when})`;
console.log(
'Pre-squash migration history detected — baselined consolidated migration as already applied.'
);
}
return;
}
}
// 2. Fresh database? Let `migrate` create the schema normally.
const publicTables = await sql<{ count: number }[]>`
SELECT count(*)::int AS count
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
`;
if (publicTables[0].count === 0) return;
// 3. Existing schema, no journal → baseline current migrations as already applied.
await sql`CREATE SCHEMA IF NOT EXISTS drizzle`;
await sql`
CREATE TABLE IF NOT EXISTS drizzle.__drizzle_migrations (
id SERIAL PRIMARY KEY,
hash text NOT NULL,
created_at bigint
)
`;
let inserted = 0;
for (const entry of entries) {
const existing = await sql<{ count: number }[]>`
SELECT count(*)::int AS count FROM drizzle.__drizzle_migrations WHERE hash = ${entry.hash}
`;
if (existing[0].count === 0) {
await sql`INSERT INTO drizzle.__drizzle_migrations ("hash", "created_at") VALUES (${entry.hash}, ${entry.when})`;
inserted += 1;
}
}
console.log(
`Existing schema detected without migration history — baselined ${inserted} migration(s) as already applied.`
);
}
// Allow running standalone: `pnpm --filter @cio/db db:baseline`
const invokedDirectly = Boolean(process.argv[1]) && scriptPath === resolve(process.argv[1]);
if (invokedDirectly) {
const connectionString = process.env.DATABASE_URL ?? process.env.PRIVATE_DATABASE_URL ?? '';
if (!connectionString) {
console.error('DATABASE_URL or PRIVATE_DATABASE_URL environment variable is required');
process.exit(1);
}
const sql = postgres(connectionString);
baselineMigrationsIfNeeded(sql)
.then(() => sql.end())
.then(() => process.exit(0))
.catch(async (error) => {
console.error('Baseline failed:', error);
await sql.end();
process.exit(1);
});
}
+17 -4
View File
@@ -5,6 +5,8 @@ import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import postgres from 'postgres';
import { baselineMigrationsIfNeeded } from './baseline';
const connectionString = process.env.DATABASE_URL ?? process.env.PRIVATE_DATABASE_URL ?? '';
const shouldSeed = process.argv.includes('--seed') || process.argv.includes('-s');
const shouldSyncSchema = !process.argv.includes('--skip-schema-sync');
@@ -43,9 +45,9 @@ async function runPnpmCommand(commandLabel: string, args: string[]) {
});
}
async function runSchemaSync() {
console.log('Syncing schema with drizzle-kit push...');
await runPnpmCommand('Schema sync', ['db', 'push']);
async function runMigrations() {
console.log('Applying database migrations with drizzle-kit migrate...');
await runPnpmCommand('Migrate', ['db:migrate']);
}
async function runSeedEssential() {
@@ -88,7 +90,18 @@ async function dbSetup() {
// Note: 'public' role exists by default in PostgreSQL
if (shouldSyncSchema) {
await runSchemaSync();
// Advisory lock so concurrent startups can't both pass the baseline check and double-insert.
// Session-scoped, so held on a dedicated connection across baseline+migrate.
const DB_SETUP_LOCK_KEY = 4242424242;
const lock = postgres(connectionString, { max: 1 });
await lock`SELECT pg_advisory_lock(${DB_SETUP_LOCK_KEY}::bigint)`;
try {
await baselineMigrationsIfNeeded(sql);
await runMigrations();
} finally {
await lock`SELECT pg_advisory_unlock(${DB_SETUP_LOCK_KEY}::bigint)`;
await lock.end();
}
}
await runSeedEssential();