Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185CREATE TABLE "crumb_tags" ( "crumb_id" uuid NOT NULL, "tag_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "crumb_tags_crumb_id_tag_id_pk" PRIMARY KEY("crumb_id","tag_id"));--> statement-breakpointCREATE TABLE "crumbs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "author_id" uuid NOT NULL, "name" text NOT NULL, "note" text, "place_id" uuid, "event_id" uuid, "source_id" uuid, "source" text DEFAULT 'manual' NOT NULL, "status" text DEFAULT 'saved' NOT NULL, "planned_for" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "crumbs_exclusive_target" CHECK (num_nonnulls("crumbs"."place_id", "crumbs"."event_id") <= 1));--> statement-breakpointCREATE TABLE "events" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "place_id" uuid, "name" text NOT NULL, "starts_at" timestamp with time zone, "ends_at" timestamp with time zone, "url" text, "status" text DEFAULT 'scheduled' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "friendships" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "requester_id" uuid NOT NULL, "addressee_id" uuid NOT NULL, "status" text DEFAULT 'pending' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "friendships_no_self" CHECK ("friendships"."requester_id" <> "friendships"."addressee_id"));--> statement-breakpointCREATE TABLE "place_merges" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "loser_id" uuid NOT NULL, "winner_id" uuid NOT NULL, "reason" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "place_sources" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "place_id" uuid NOT NULL, "provider" text NOT NULL, "osm_type" text, "osm_id" text, "raw" jsonb NOT NULL, "query_text" text, "fetched_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "places" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "latitude" double precision, "longitude" double precision, "address" text, "category" text, "status" text DEFAULT 'provisional' NOT NULL, "merged_into_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "places_merged_iff_redirect" CHECK (("places"."status" = 'merged') = ("places"."merged_into_id" is not null)));--> statement-breakpointCREATE TABLE "sessions" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "token_hash" text NOT NULL, "expires_at" timestamp with time zone NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "sources" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "kind" text NOT NULL, "url" text, "image_ref" text, "shared_by_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "tags" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "emoji" text DEFAULT '📍' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "trail_collaborators" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "trail_id" uuid NOT NULL, "user_id" uuid NOT NULL, "inviter_id" uuid, "status" text DEFAULT 'pending' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "trail_collaborators_no_self_invite" CHECK ("trail_collaborators"."user_id" <> "trail_collaborators"."inviter_id"));--> statement-breakpointCREATE TABLE "trail_crumbs" ( "trail_id" uuid NOT NULL, "crumb_id" uuid NOT NULL, "position" integer NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "trail_crumbs_trail_id_crumb_id_pk" PRIMARY KEY("trail_id","crumb_id"));--> statement-breakpointCREATE TABLE "trails" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "author_id" uuid NOT NULL, "name" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointCREATE TABLE "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "email" text NOT NULL, "handle" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointALTER TABLE "crumb_tags" ADD CONSTRAINT "crumb_tags_crumb_id_crumbs_id_fk" FOREIGN KEY ("crumb_id") REFERENCES "public"."crumbs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "crumb_tags" ADD CONSTRAINT "crumb_tags_tag_id_tags_id_fk" FOREIGN KEY ("tag_id") REFERENCES "public"."tags"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpointALTER TABLE "crumbs" ADD CONSTRAINT "crumbs_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "crumbs" ADD CONSTRAINT "crumbs_place_id_places_id_fk" FOREIGN KEY ("place_id") REFERENCES "public"."places"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpointALTER TABLE "crumbs" ADD CONSTRAINT "crumbs_event_id_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."events"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpointALTER TABLE "crumbs" ADD CONSTRAINT "crumbs_source_id_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."sources"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpointALTER TABLE "events" ADD CONSTRAINT "events_place_id_places_id_fk" FOREIGN KEY ("place_id") REFERENCES "public"."places"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpointALTER TABLE "friendships" ADD CONSTRAINT "friendships_requester_id_users_id_fk" FOREIGN KEY ("requester_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "friendships" ADD CONSTRAINT "friendships_addressee_id_users_id_fk" FOREIGN KEY ("addressee_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "place_merges" ADD CONSTRAINT "place_merges_loser_id_places_id_fk" FOREIGN KEY ("loser_id") REFERENCES "public"."places"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpointALTER TABLE "place_merges" ADD CONSTRAINT "place_merges_winner_id_places_id_fk" FOREIGN KEY ("winner_id") REFERENCES "public"."places"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpointALTER TABLE "place_sources" ADD CONSTRAINT "place_sources_place_id_places_id_fk" FOREIGN KEY ("place_id") REFERENCES "public"."places"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "places" ADD CONSTRAINT "places_merged_into_id_places_id_fk" FOREIGN KEY ("merged_into_id") REFERENCES "public"."places"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpointALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "sources" ADD CONSTRAINT "sources_shared_by_id_users_id_fk" FOREIGN KEY ("shared_by_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpointALTER TABLE "trail_collaborators" ADD CONSTRAINT "trail_collaborators_trail_id_trails_id_fk" FOREIGN KEY ("trail_id") REFERENCES "public"."trails"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "trail_collaborators" ADD CONSTRAINT "trail_collaborators_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "trail_collaborators" ADD CONSTRAINT "trail_collaborators_inviter_id_users_id_fk" FOREIGN KEY ("inviter_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpointALTER TABLE "trail_crumbs" ADD CONSTRAINT "trail_crumbs_trail_id_trails_id_fk" FOREIGN KEY ("trail_id") REFERENCES "public"."trails"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "trail_crumbs" ADD CONSTRAINT "trail_crumbs_crumb_id_crumbs_id_fk" FOREIGN KEY ("crumb_id") REFERENCES "public"."crumbs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "trails" ADD CONSTRAINT "trails_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointCREATE INDEX "crumb_tags_tag_id_idx" ON "crumb_tags" USING btree ("tag_id");--> statement-breakpointCREATE INDEX "crumbs_author_id_idx" ON "crumbs" USING btree ("author_id");--> statement-breakpointCREATE INDEX "crumbs_created_at_idx" ON "crumbs" USING btree ("created_at");--> statement-breakpointCREATE INDEX "crumbs_place_id_idx" ON "crumbs" USING btree ("place_id");--> statement-breakpointCREATE INDEX "crumbs_event_id_idx" ON "crumbs" USING btree ("event_id");--> statement-breakpointCREATE INDEX "crumbs_source_id_idx" ON "crumbs" USING btree ("source_id");--> statement-breakpointCREATE INDEX "events_place_id_idx" ON "events" USING btree ("place_id");--> statement-breakpointCREATE INDEX "events_starts_at_idx" ON "events" USING btree ("starts_at");--> statement-breakpointCREATE UNIQUE INDEX "friendships_pair_idx" ON "friendships" USING btree ("requester_id","addressee_id");--> statement-breakpointCREATE INDEX "friendships_addressee_id_idx" ON "friendships" USING btree ("addressee_id");--> statement-breakpointCREATE INDEX "friendships_requester_id_idx" ON "friendships" USING btree ("requester_id");--> statement-breakpointCREATE UNIQUE INDEX "place_sources_provider_osm_idx" ON "place_sources" USING btree ("provider","osm_type","osm_id") WHERE "place_sources"."osm_id" is not null;--> statement-breakpointCREATE INDEX "place_sources_place_id_idx" ON "place_sources" USING btree ("place_id");--> statement-breakpointCREATE INDEX "places_lat_lng_idx" ON "places" USING btree ("latitude","longitude");--> statement-breakpointCREATE INDEX "places_status_idx" ON "places" USING btree ("status") WHERE "places"."status" = 'provisional';--> statement-breakpointCREATE UNIQUE INDEX "sessions_token_hash_idx" ON "sessions" USING btree ("token_hash");--> statement-breakpointCREATE INDEX "sessions_user_id_idx" ON "sessions" USING btree ("user_id");--> statement-breakpointCREATE UNIQUE INDEX "sources_url_idx" ON "sources" USING btree ("url") WHERE "sources"."url" is not null;--> statement-breakpointCREATE UNIQUE INDEX "tags_name_idx" ON "tags" USING btree ("name");--> statement-breakpointCREATE UNIQUE INDEX "trail_collaborators_trail_user_idx" ON "trail_collaborators" USING btree ("trail_id","user_id");--> statement-breakpointCREATE INDEX "trail_collaborators_user_id_idx" ON "trail_collaborators" USING btree ("user_id");--> statement-breakpointCREATE UNIQUE INDEX "trail_crumbs_trail_position_idx" ON "trail_crumbs" USING btree ("trail_id","position");--> statement-breakpointCREATE INDEX "trail_crumbs_crumb_id_idx" ON "trail_crumbs" USING btree ("crumb_id");--> statement-breakpointCREATE INDEX "trails_author_id_idx" ON "trails" USING btree ("author_id");--> statement-breakpointCREATE INDEX "trails_created_at_idx" ON "trails" USING btree ("created_at");--> statement-breakpointCREATE UNIQUE INDEX "users_email_lower_idx" ON "users" USING btree ("email");--> statement-breakpointCREATE UNIQUE INDEX "users_handle_lower_idx" ON "users" USING btree ("handle");