From 611441662fe76e5380bb931df221f834d24f5f6a Mon Sep 17 00:00:00 2001 From: Accusys Date: Tue, 19 May 2026 14:22:40 +0800 Subject: [PATCH] fix: register_resource - use ON CONFLICT (resource_id) DO UPDATE instead of RETURNING id - resources table uses resource_id as PK (no auto-increment id column) - Make register idempotent: duplicate registration updates status + heartbeat - Added Phase 1 API test script (15 endpoints, 100% pass) --- src/core/db/postgres_db.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/db/postgres_db.rs b/src/core/db/postgres_db.rs index f466000..5faebc3 100644 --- a/src/core/db/postgres_db.rs +++ b/src/core/db/postgres_db.rs @@ -2530,11 +2530,11 @@ impl PostgresDb { } } - pub async fn register_resource(&self, resource: super::postgres_db::ResourceRecord) -> Result { + pub async fn register_resource(&self, resource: super::postgres_db::ResourceRecord) -> Result<()> { let table = schema::table_name("resources"); - let id: i64 = sqlx::query_scalar(&format!( + sqlx::query(&format!( "INSERT INTO {} (resource_id, resource_type, category, capabilities, config, metadata, status) \ - VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id", table + VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (resource_id) DO UPDATE SET status = EXCLUDED.status, last_heartbeat = NOW()", table )) .bind(&resource.resource_id) .bind(&resource.resource_type) @@ -2543,8 +2543,8 @@ impl PostgresDb { .bind(&resource.config) .bind(&resource.metadata) .bind("online") - .fetch_one(&self.pool).await?; - Ok(id) + .execute(&self.pool).await?; + Ok(()) } pub async fn heartbeat_resource(&self, resource_id: &str, status: &str) -> Result<()> {