Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/adapters/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PostgresPort for PostgresAdapter {
let last_accessed_at =
optional_epoch_seconds(record.last_accessed_at.as_deref(), "last_accessed_at")?;

client
let affected_rows = client
.execute(
r#"
INSERT INTO nextral_memories (
Expand All @@ -89,8 +89,6 @@ impl PostgresPort for PostgresAdapter {
$22, $23, $24
)
ON CONFLICT (id) DO UPDATE SET
tenant_id = EXCLUDED.tenant_id,
user_id = EXCLUDED.user_id,
session_id = EXCLUDED.session_id,
content = EXCLUDED.content,
content_type = EXCLUDED.content_type,
Expand All @@ -111,6 +109,8 @@ impl PostgresPort for PostgresAdapter {
access_count = EXCLUDED.access_count,
status = EXCLUDED.status,
schema_version = EXCLUDED.schema_version
WHERE nextral_memories.tenant_id = EXCLUDED.tenant_id
AND nextral_memories.user_id = EXCLUDED.user_id
"#,
&[
&record.id,
Expand Down Expand Up @@ -140,6 +140,11 @@ impl PostgresPort for PostgresAdapter {
],
)
.map_err(|error| CoreError::Io(error.to_string()))?;
if affected_rows == 0 {
return Err(CoreError::Conflict(
"memory id already exists for a different tenant or user".to_string(),
));
}
Ok(())
}

Expand Down
Loading