Skip to content
Merged
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
12 changes: 9 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 rows_affected = 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,12 @@ impl PostgresPort for PostgresAdapter {
],
)
.map_err(|error| CoreError::Io(error.to_string()))?;
if rows_affected == 0 {
return Err(CoreError::Conflict(format!(
"memory id '{}' already exists in a different tenant/user scope",
record.id
)));
}
Ok(())
}

Expand Down
Loading