0019_project_files.sql 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. -- 0019_project_files — metadata for S3-compatible object storage.
  2. -- The actual object bytes live in MinIO under `projects/<projectId>/<fileId>`.
  3. -- The storage service is the only path that mints presigned PUT/GET URLs
  4. -- against that prefix, so the unique index on object_key catches any
  5. -- code path that tries to register two rows for the same object.
  6. CREATE TABLE IF NOT EXISTS "project_files" (
  7. "id" text PRIMARY KEY NOT NULL,
  8. "project_id" text NOT NULL,
  9. "name" text NOT NULL,
  10. "object_key" text NOT NULL,
  11. "content_type" text NOT NULL,
  12. "size_bytes" text NOT NULL,
  13. "checksum_sha256" text,
  14. "uploaded_by" text,
  15. "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  16. "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  17. "deleted_at" timestamp with time zone,
  18. CONSTRAINT "project_files_project_id_fk"
  19. FOREIGN KEY ("project_id") REFERENCES "projects"("id") ON DELETE CASCADE,
  20. CONSTRAINT "project_files_uploaded_by_fk"
  21. FOREIGN KEY ("uploaded_by") REFERENCES "users"("id") ON DELETE SET NULL
  22. );
  23. -- Per-project list query: hot path is "list non-deleted files for a project".
  24. CREATE INDEX IF NOT EXISTS "project_files_project_idx"
  25. ON "project_files" USING btree ("project_id")
  26. WHERE "deleted_at" IS NULL;
  27. -- Object keys are unique across the whole bucket.
  28. CREATE UNIQUE INDEX IF NOT EXISTS "project_files_object_key_idx"
  29. ON "project_files" USING btree ("object_key");