From faa54b2e854870b4dadfcfac333e36c331f0f30e Mon Sep 17 00:00:00 2001 From: gilesb Date: Mon, 12 Jan 2026 08:18:23 +0000 Subject: [PATCH] Add database migration for content_hash -> cid rename - Add DO block to migrate existing columns: - cache_items.content_hash -> cid - item_types.content_hash -> cid - l2_shares.content_hash -> cid - storage_pins.content_hash -> cid - run_cache.output_hash -> output_cid - Fix duplicate key bug in upload response Co-Authored-By: Claude Opus 4.5 --- app/routers/cache.py | 4 ++-- database.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/routers/cache.py b/app/routers/cache.py index abf940b..13b10f5 100644 --- a/app/routers/cache.py +++ b/app/routers/cache.py @@ -221,8 +221,8 @@ async def upload_content( raise HTTPException(400, error) return { - "cid": ipfs_cid, - "cid": cid, # Legacy, for backwards compatibility + "cid": ipfs_cid or cid, + "content_hash": cid, # Legacy, for backwards compatibility "filename": file.filename, "size": len(content), "uploaded": True, diff --git a/database.py b/database.py index 21b5818..dc20af9 100644 --- a/database.py +++ b/database.py @@ -78,6 +78,25 @@ DO $$ BEGIN EXCEPTION WHEN others THEN NULL; END $$; +-- Migration: rename content_hash to cid (for existing databases) +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'cache_items' AND column_name = 'content_hash') THEN + ALTER TABLE cache_items RENAME COLUMN content_hash TO cid; + END IF; + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'item_types' AND column_name = 'content_hash') THEN + ALTER TABLE item_types RENAME COLUMN content_hash TO cid; + END IF; + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'l2_shares' AND column_name = 'content_hash') THEN + ALTER TABLE l2_shares RENAME COLUMN content_hash TO cid; + END IF; + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'storage_pins' AND column_name = 'content_hash') THEN + ALTER TABLE storage_pins RENAME COLUMN content_hash TO cid; + END IF; + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'run_cache' AND column_name = 'output_hash') THEN + ALTER TABLE run_cache RENAME COLUMN output_hash TO output_cid; + END IF; +END $$; + -- Run cache: maps content-addressable run_id to output -- run_id is a hash of (sorted inputs + recipe), making runs deterministic CREATE TABLE IF NOT EXISTS run_cache (