CREATE TABLE "carrito" ( "id" text PRIMARY KEY NOT NULL, "usuario_id" text, "creado_en" timestamp with time zone NOT NULL, "actualizado_en" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "carrito_item" ( "carrito_id" text NOT NULL, "cancion_slug" text NOT NULL, "anadido_en" timestamp with time zone NOT NULL, CONSTRAINT "carrito_item_carrito_id_cancion_slug_pk" PRIMARY KEY("carrito_id","cancion_slug") ); --> statement-breakpoint CREATE TABLE "codigo_acceso" ( "id" text PRIMARY KEY NOT NULL, "email" text NOT NULL, "hash" text NOT NULL, "expira_en" timestamp with time zone NOT NULL, "intentos" integer DEFAULT 0 NOT NULL, "usado_en" timestamp with time zone, "creado_en" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "compra" ( "usuario_id" text NOT NULL, "cancion_slug" text NOT NULL, "pedido_id" text NOT NULL, "comprado_en" timestamp with time zone NOT NULL, CONSTRAINT "compra_usuario_id_cancion_slug_pk" PRIMARY KEY("usuario_id","cancion_slug") ); --> statement-breakpoint CREATE TABLE "favorito" ( "usuario_id" text NOT NULL, "cancion_slug" text NOT NULL, "marcado_en" timestamp with time zone NOT NULL, CONSTRAINT "favorito_usuario_id_cancion_slug_pk" PRIMARY KEY("usuario_id","cancion_slug") ); --> statement-breakpoint CREATE TABLE "limite" ( "clave" text PRIMARY KEY NOT NULL, "intentos" integer NOT NULL, "expira_en" bigint NOT NULL ); --> statement-breakpoint CREATE TABLE "pedido" ( "id" text PRIMARY KEY NOT NULL, "usuario_id" text NOT NULL, "sesion_pago_id" text NOT NULL, "estado" text DEFAULT 'pendiente' NOT NULL, "packs" integer NOT NULL, "total_centimos" integer NOT NULL, "moneda" text DEFAULT 'eur' NOT NULL, "creado_en" timestamp with time zone NOT NULL, "pagado_en" timestamp with time zone, CONSTRAINT "pedido_sesion_pago_id_unique" UNIQUE("sesion_pago_id") ); --> statement-breakpoint CREATE TABLE "pedido_item" ( "pedido_id" text NOT NULL, "cancion_slug" text NOT NULL, "titulo" text NOT NULL, CONSTRAINT "pedido_item_pedido_id_cancion_slug_pk" PRIMARY KEY("pedido_id","cancion_slug") ); --> statement-breakpoint CREATE TABLE "sesion" ( "id" text PRIMARY KEY NOT NULL, "usuario_id" text NOT NULL, "expira_en" timestamp with time zone NOT NULL, "creada_en" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "usuario" ( "id" text PRIMARY KEY NOT NULL, "email" text NOT NULL, "nombre" text, "creado_en" timestamp with time zone NOT NULL, "ultimo_acceso_en" timestamp with time zone, CONSTRAINT "usuario_email_unique" UNIQUE("email") ); --> statement-breakpoint CREATE TABLE "valoracion" ( "usuario_id" text NOT NULL, "cancion_slug" text NOT NULL, "puntuacion" integer NOT NULL, "creada_en" timestamp with time zone NOT NULL, "actualizada_en" timestamp with time zone NOT NULL, CONSTRAINT "valoracion_usuario_id_cancion_slug_pk" PRIMARY KEY("usuario_id","cancion_slug") ); --> statement-breakpoint ALTER TABLE "carrito" ADD CONSTRAINT "carrito_usuario_id_usuario_id_fk" FOREIGN KEY ("usuario_id") REFERENCES "public"."usuario"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "carrito_item" ADD CONSTRAINT "carrito_item_carrito_id_carrito_id_fk" FOREIGN KEY ("carrito_id") REFERENCES "public"."carrito"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "compra" ADD CONSTRAINT "compra_usuario_id_usuario_id_fk" FOREIGN KEY ("usuario_id") REFERENCES "public"."usuario"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "compra" ADD CONSTRAINT "compra_pedido_id_pedido_id_fk" FOREIGN KEY ("pedido_id") REFERENCES "public"."pedido"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "favorito" ADD CONSTRAINT "favorito_usuario_id_usuario_id_fk" FOREIGN KEY ("usuario_id") REFERENCES "public"."usuario"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pedido" ADD CONSTRAINT "pedido_usuario_id_usuario_id_fk" FOREIGN KEY ("usuario_id") REFERENCES "public"."usuario"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pedido_item" ADD CONSTRAINT "pedido_item_pedido_id_pedido_id_fk" FOREIGN KEY ("pedido_id") REFERENCES "public"."pedido"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sesion" ADD CONSTRAINT "sesion_usuario_id_usuario_id_fk" FOREIGN KEY ("usuario_id") REFERENCES "public"."usuario"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "valoracion" ADD CONSTRAINT "valoracion_usuario_id_usuario_id_fk" FOREIGN KEY ("usuario_id") REFERENCES "public"."usuario"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE INDEX "carrito_usuario" ON "carrito" USING btree ("usuario_id");--> statement-breakpoint CREATE INDEX "codigo_acceso_email" ON "codigo_acceso" USING btree ("email");--> statement-breakpoint CREATE INDEX "idx_favorito_usuario" ON "favorito" USING btree ("usuario_id");--> statement-breakpoint CREATE INDEX "limite_expira" ON "limite" USING btree ("expira_en");--> statement-breakpoint CREATE INDEX "pedido_usuario" ON "pedido" USING btree ("usuario_id");--> statement-breakpoint CREATE INDEX "sesion_usuario" ON "sesion" USING btree ("usuario_id");--> statement-breakpoint CREATE INDEX "idx_valoracion_cancion" ON "valoracion" USING btree ("cancion_slug");