SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
CREATE EXTENSION IF NOT EXISTS "pgsodium";
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE EXTENSION IF NOT EXISTS "pg_graphql" WITH SCHEMA "graphql";
CREATE EXTENSION IF NOT EXISTS "pg_stat_statements" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "pgjwt" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "supabase_vault" WITH SCHEMA "vault";
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "extensions";
CREATE OR REPLACE FUNCTION "public"."create_profile"() RETURNS "trigger"
LANGUAGE "plpgsql"
AS $$
BEGIN
INSERT INTO public.profiles (email, first_name, last_name, role, user_id)
VALUES (NEW.email, NEW.first_name, NEW.last_name, 'user', NEW.id); -- Set user_id to the new user's id
RETURN NEW;
END;
$$;
ALTER FUNCTION "public"."create_profile"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."is_admin"() RETURNS boolean
LANGUAGE "plpgsql" STABLE
AS $$
BEGIN
RETURN EXISTS (
SELECT 1 FROM public.profiles
WHERE user_id = auth.uid() AND role = 'admin'
);
END;
$$;
ALTER FUNCTION "public"."is_admin"() OWNER TO "postgres";
SET default_tablespace = '';
SET default_table_access_method = "heap";
CREATE TABLE IF NOT EXISTS "public"."distributors" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"description" "text",
"phone" "text",
"company" "text",
"name" "text"
);
ALTER TABLE "public"."distributors" OWNER TO "postgres";
ALTER TABLE "public"."distributors" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."distributors_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."events" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"title" "text",
"description" "text",
"from_date" timestamp with time zone,
"to_date" timestamp with time zone,
"price" double precision
);
ALTER TABLE "public"."events" OWNER TO "postgres";
ALTER TABLE "public"."events" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."events_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."profiles" (
"id" bigint NOT NULL,
"email" "text" NOT NULL,
"first_name" "text" NOT NULL,
"last_name" "text" NOT NULL,
"birthday" "date",
"user_id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL
);
ALTER TABLE "public"."profiles" OWNER TO "postgres";
ALTER TABLE "public"."profiles" ALTER COLUMN "id" ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME "public"."profiles_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."quiz_answers" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"team_name" "text",
"question_number" bigint,
"answer" "text",
"correct" boolean,
"points" integer DEFAULT 0,
"quiz_id" bigint
);
ALTER TABLE "public"."quiz_answers" OWNER TO "postgres";
ALTER TABLE "public"."quiz_answers" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."quiz_answers_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."quizes" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"name" "text",
"date" timestamp with time zone
);
ALTER TABLE "public"."quizes" OWNER TO "postgres";
ALTER TABLE "public"."quizes" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."quizes_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."reservations" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"person_name" "text",
"phone" "text",
"from_date" timestamp with time zone,
"to_date" timestamp with time zone,
"persons" integer,
"caparo" bigint,
"description" "text",
"event_id" bigint,
"approved" boolean DEFAULT false
);
ALTER TABLE "public"."reservations" OWNER TO "postgres";
ALTER TABLE "public"."reservations" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."reservations_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."schedules" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"start_time" timestamp with time zone,
"end_time" timestamp with time zone,
"user_id" "uuid" DEFAULT "gen_random_uuid"()
);
ALTER TABLE "public"."schedules" OWNER TO "postgres";
ALTER TABLE "public"."schedules" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."schedules_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."tasks" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"name" "text",
"start_date" "text",
"interval" integer,
"difficulty" integer,
"shift" "text",
"time" "text" DEFAULT '9:30'::"text"
);
ALTER TABLE "public"."tasks" OWNER TO "postgres";
ALTER TABLE "public"."tasks" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."tasks_id_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."user_roles" (
"user_id" "uuid" NOT NULL,
"role" "text" NOT NULL
);
ALTER TABLE "public"."user_roles" OWNER TO "postgres";
ALTER TABLE ONLY "public"."distributors"
ADD CONSTRAINT "distributors_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."events"
ADD CONSTRAINT "events_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."profiles"
ADD CONSTRAINT "profiles_email_key" UNIQUE ("email");
ALTER TABLE ONLY "public"."profiles"
ADD CONSTRAINT "profiles_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."profiles"
ADD CONSTRAINT "profiles_user_id_key" UNIQUE ("user_id");
ALTER TABLE ONLY "public"."quiz_answers"
ADD CONSTRAINT "quiz_answers_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."quizes"
ADD CONSTRAINT "quizes_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."reservations"
ADD CONSTRAINT "reservations_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."schedules"
ADD CONSTRAINT "schedules_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."tasks"
ADD CONSTRAINT "tasks_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."user_roles"
ADD CONSTRAINT "user_roles_pkey" PRIMARY KEY ("user_id");
ALTER TABLE ONLY "public"."user_roles"
ADD CONSTRAINT "user_roles_user_id_key" UNIQUE ("user_id");
ALTER TABLE ONLY "public"."profiles"
ADD CONSTRAINT "profiles_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE;
ALTER TABLE ONLY "public"."quiz_answers"
ADD CONSTRAINT "quiz_answers_quiz_id_fkey" FOREIGN KEY ("quiz_id") REFERENCES "public"."quizes"("id") ON DELETE CASCADE;
ALTER TABLE ONLY "public"."reservations"
ADD CONSTRAINT "reservations_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "public"."events"("id");
ALTER TABLE ONLY "public"."schedules"
ADD CONSTRAINT "schedules_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id");
ALTER TABLE ONLY "public"."user_roles"
ADD CONSTRAINT "user_roles_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."users"("id") ON DELETE CASCADE;
ALTER TABLE ONLY "public"."user_roles"
ADD CONSTRAINT "user_roles_user_id_fkey1" FOREIGN KEY ("user_id") REFERENCES "public"."profiles"("user_id");
CREATE POLICY "Allow admins to create user roles" ON "public"."user_roles" FOR INSERT TO "authenticated" WITH CHECK ((EXISTS ( SELECT 1
FROM "public"."user_roles" "user_roles_1"
WHERE (("user_roles_1"."user_id" = ( SELECT "auth"."uid"() AS "uid")) AND ("user_roles_1"."role" = 'admin'::"text")))));
CREATE POLICY "Allow admins to read profiles" ON "public"."profiles" FOR SELECT TO "authenticated" USING ((EXISTS ( SELECT 1
FROM "public"."user_roles"
WHERE (("user_roles"."user_id" = "auth"."uid"()) AND ("user_roles"."role" = 'admin'::"text")))));
CREATE POLICY "Allow admins to update profiles" ON "public"."profiles" FOR UPDATE TO "authenticated" USING ((EXISTS ( SELECT 1
FROM "public"."user_roles"
WHERE (("user_roles"."user_id" = ( SELECT "auth"."uid"() AS "uid")) AND ("user_roles"."role" = 'admin'::"text"))))) WITH CHECK ((EXISTS ( SELECT 1
FROM "public"."user_roles"
WHERE (("user_roles"."user_id" = ( SELECT "auth"."uid"() AS "uid")) AND ("user_roles"."role" = 'admin'::"text")))));
CREATE POLICY "Allow admins to update user roles" ON "public"."user_roles" FOR UPDATE TO "authenticated" WITH CHECK ((EXISTS ( SELECT 1
FROM "public"."user_roles" "user_roles_1"
WHERE (("user_roles_1"."user_id" = ( SELECT "auth"."uid"() AS "uid")) AND ("user_roles_1"."role" = 'admin'::"text")))));
CREATE POLICY "Allow authenticated users to select user roles" ON "public"."user_roles" FOR SELECT TO "authenticated" USING (true);
CREATE POLICY "Manage events" ON "public"."events" TO "authenticated" USING (true) WITH CHECK (true);
CREATE POLICY "Manage reservations" ON "public"."reservations" TO "authenticated" USING (true) WITH CHECK (true);
CREATE POLICY "View events" ON "public"."events" FOR SELECT USING (true);
CREATE POLICY "can create reservations" ON "public"."reservations" FOR INSERT WITH CHECK (true);
CREATE POLICY "can delete answers" ON "public"."quiz_answers" FOR SELECT USING (true);
CREATE POLICY "can fetch tasks" ON "public"."tasks" FOR SELECT USING (true);
CREATE POLICY "can manage distributors" ON "public"."distributors" TO "authenticated" USING (true) WITH CHECK (true);
CREATE POLICY "can manage quizes" ON "public"."quizes" TO "authenticated" USING (true) WITH CHECK (true);
CREATE POLICY "can manage schedule" ON "public"."schedules" TO "authenticated" USING (true) WITH CHECK (true);
CREATE POLICY "can manage tasks" ON "public"."tasks" TO "authenticated" USING (true) WITH CHECK (true);
CREATE POLICY "can read schedule" ON "public"."schedules" FOR SELECT USING (true);
CREATE POLICY "can view quizes" ON "public"."quizes" FOR SELECT USING (true);
CREATE POLICY "can view reservations" ON "public"."reservations" FOR SELECT USING (true);
CREATE POLICY "create answer" ON "public"."quiz_answers" FOR INSERT WITH CHECK (true);
ALTER TABLE "public"."distributors" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."events" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "manage answers" ON "public"."quiz_answers" TO "authenticated" USING (true) WITH CHECK (true);
ALTER TABLE "public"."profiles" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."quiz_answers" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."quizes" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."reservations" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."schedules" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."tasks" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."user_roles" ENABLE ROW LEVEL SECURITY;
ALTER PUBLICATION "supabase_realtime" OWNER TO "postgres";
ALTER PUBLICATION "supabase_realtime" ADD TABLE ONLY "public"."quizes";
ALTER PUBLICATION "supabase_realtime" ADD TABLE ONLY "public"."reservations";
GRANT USAGE ON SCHEMA "public" TO "postgres";
GRANT USAGE ON SCHEMA "public" TO "anon";
GRANT USAGE ON SCHEMA "public" TO "authenticated";
GRANT USAGE ON SCHEMA "public" TO "service_role";
GRANT ALL ON FUNCTION "public"."create_profile"() TO "anon";
GRANT ALL ON FUNCTION "public"."create_profile"() TO "authenticated";
GRANT ALL ON FUNCTION "public"."create_profile"() TO "service_role";
GRANT ALL ON FUNCTION "public"."is_admin"() TO "anon";
GRANT ALL ON FUNCTION "public"."is_admin"() TO "authenticated";
GRANT ALL ON FUNCTION "public"."is_admin"() TO "service_role";
GRANT ALL ON TABLE "public"."distributors" TO "anon";
GRANT ALL ON TABLE "public"."distributors" TO "authenticated";
GRANT ALL ON TABLE "public"."distributors" TO "service_role";
GRANT ALL ON SEQUENCE "public"."distributors_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."distributors_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."distributors_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."events" TO "anon";
GRANT ALL ON TABLE "public"."events" TO "authenticated";
GRANT ALL ON TABLE "public"."events" TO "service_role";
GRANT ALL ON SEQUENCE "public"."events_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."events_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."events_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."profiles" TO "anon";
GRANT ALL ON TABLE "public"."profiles" TO "authenticated";
GRANT ALL ON TABLE "public"."profiles" TO "service_role";
GRANT ALL ON SEQUENCE "public"."profiles_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."profiles_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."profiles_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."quiz_answers" TO "anon";
GRANT ALL ON TABLE "public"."quiz_answers" TO "authenticated";
GRANT ALL ON TABLE "public"."quiz_answers" TO "service_role";
GRANT ALL ON SEQUENCE "public"."quiz_answers_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."quiz_answers_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."quiz_answers_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."quizes" TO "anon";
GRANT ALL ON TABLE "public"."quizes" TO "authenticated";
GRANT ALL ON TABLE "public"."quizes" TO "service_role";
GRANT ALL ON SEQUENCE "public"."quizes_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."quizes_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."quizes_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."reservations" TO "anon";
GRANT ALL ON TABLE "public"."reservations" TO "authenticated";
GRANT ALL ON TABLE "public"."reservations" TO "service_role";
GRANT ALL ON SEQUENCE "public"."reservations_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."reservations_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."reservations_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."schedules" TO "anon";
GRANT ALL ON TABLE "public"."schedules" TO "authenticated";
GRANT ALL ON TABLE "public"."schedules" TO "service_role";
GRANT ALL ON SEQUENCE "public"."schedules_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."schedules_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."schedules_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."tasks" TO "anon";
GRANT ALL ON TABLE "public"."tasks" TO "authenticated";
GRANT ALL ON TABLE "public"."tasks" TO "service_role";
GRANT ALL ON SEQUENCE "public"."tasks_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."tasks_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."tasks_id_seq" TO "service_role";
GRANT ALL ON TABLE "public"."user_roles" TO "anon";
GRANT ALL ON TABLE "public"."user_roles" TO "authenticated";
GRANT ALL ON TABLE "public"."user_roles" TO "service_role";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "postgres";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "anon";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "authenticated";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "service_role";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "postgres";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "anon";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "authenticated";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "service_role";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "postgres";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "anon";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "authenticated";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "service_role";
RESET ALL;