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)
This commit is contained in:
Accusys
2026-05-19 14:22:40 +08:00
parent 3d2bacb07f
commit 611441662f

View File

@@ -2530,11 +2530,11 @@ impl PostgresDb {
} }
} }
pub async fn register_resource(&self, resource: super::postgres_db::ResourceRecord) -> Result<i64> { pub async fn register_resource(&self, resource: super::postgres_db::ResourceRecord) -> Result<()> {
let table = schema::table_name("resources"); 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) \ "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_id)
.bind(&resource.resource_type) .bind(&resource.resource_type)
@@ -2543,8 +2543,8 @@ impl PostgresDb {
.bind(&resource.config) .bind(&resource.config)
.bind(&resource.metadata) .bind(&resource.metadata)
.bind("online") .bind("online")
.fetch_one(&self.pool).await?; .execute(&self.pool).await?;
Ok(id) Ok(())
} }
pub async fn heartbeat_resource(&self, resource_id: &str, status: &str) -> Result<()> { pub async fn heartbeat_resource(&self, resource_id: &str, status: &str) -> Result<()> {