CREATE TABLE "document_import_sources" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "tenant_id" bigint NOT NULL, "created_by" uuid, "name" text NOT NULL, "provider" text DEFAULT 'imap' NOT NULL, "enabled" boolean DEFAULT true NOT NULL, "mailbox_address_encrypted" jsonb, "password_encrypted" jsonb, "imap_host_encrypted" jsonb, "imap_port" integer DEFAULT 993 NOT NULL, "imap_secure" boolean DEFAULT true NOT NULL, "mailbox_path" text DEFAULT 'INBOX' NOT NULL, "target_folder_id" uuid, "default_filetype_id" uuid, "mark_as_seen" boolean DEFAULT true NOT NULL, "last_synced_at" timestamp with time zone, "last_error" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "document_import_states" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "source_id" uuid NOT NULL, "mailbox_path" text NOT NULL, "uid_validity" bigint, "highest_uid" bigint DEFAULT 0 NOT NULL, "delta_link_encrypted" jsonb, "updated_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "document_import_items" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "tenant_id" bigint NOT NULL, "source_id" uuid NOT NULL, "remote_message_id" text NOT NULL, "attachment_key" text NOT NULL, "attachment_checksum" text NOT NULL, "filename" text, "status" text NOT NULL, "error" text, "file_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "document_import_sources" ADD CONSTRAINT "document_import_sources_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE cascade; --> statement-breakpoint ALTER TABLE "document_import_sources" ADD CONSTRAINT "document_import_sources_created_by_auth_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."auth_users"("id") ON DELETE set null; --> statement-breakpoint ALTER TABLE "document_import_sources" ADD CONSTRAINT "document_import_sources_target_folder_id_folders_id_fk" FOREIGN KEY ("target_folder_id") REFERENCES "public"."folders"("id") ON DELETE set null; --> statement-breakpoint ALTER TABLE "document_import_sources" ADD CONSTRAINT "document_import_sources_default_filetype_id_filetags_id_fk" FOREIGN KEY ("default_filetype_id") REFERENCES "public"."filetags"("id") ON DELETE set null; --> statement-breakpoint ALTER TABLE "document_import_states" ADD CONSTRAINT "document_import_states_source_id_document_import_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."document_import_sources"("id") ON DELETE cascade ON UPDATE cascade; --> statement-breakpoint ALTER TABLE "document_import_items" ADD CONSTRAINT "document_import_items_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE cascade; --> statement-breakpoint ALTER TABLE "document_import_items" ADD CONSTRAINT "document_import_items_source_id_document_import_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."document_import_sources"("id") ON DELETE cascade ON UPDATE cascade; --> statement-breakpoint ALTER TABLE "document_import_items" ADD CONSTRAINT "document_import_items_file_id_files_id_fk" FOREIGN KEY ("file_id") REFERENCES "public"."files"("id") ON DELETE set null; --> statement-breakpoint CREATE INDEX "document_import_sources_tenant_idx" ON "document_import_sources" USING btree ("tenant_id"); --> statement-breakpoint CREATE UNIQUE INDEX "document_import_states_source_mailbox_key" ON "document_import_states" USING btree ("source_id", "mailbox_path"); --> statement-breakpoint CREATE UNIQUE INDEX "document_import_items_remote_attachment_key" ON "document_import_items" USING btree ("source_id", "remote_message_id", "attachment_key"); --> statement-breakpoint CREATE INDEX "document_import_items_checksum_idx" ON "document_import_items" USING btree ("source_id", "attachment_checksum");