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:
@@ -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<()> {
|
||||||
|
|||||||
Reference in New Issue
Block a user