Propagate user app ownership conflict.

This commit is contained in:
2026-10-09 10:16:23 +02:00
parent 51cd5b9bd9
commit 381c229bd4
33 changed files with 779 additions and 138 deletions
@@ -6,12 +6,24 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "accounts",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "accounts",
"name": "name"
}
}
}
],
"parameters": {
@@ -6,12 +6,24 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "user_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "user_id"
}
}
}
],
"parameters": {
@@ -6,12 +6,24 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "user_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "user_id"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "users",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "users",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,32 +6,68 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "account_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "account_id"
}
}
},
{
"ordinal": 2,
"name": "amount_cents",
"type_info": "Int8"
"type_info": "Int8",
"origin": {
"Table": {
"table": "transactions",
"name": "amount_cents"
}
}
},
{
"ordinal": 3,
"name": "direction",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "transactions",
"name": "direction"
}
}
},
{
"ordinal": 4,
"name": "date",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "transactions",
"name": "date"
}
}
},
{
"ordinal": 5,
"name": "description",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "transactions",
"name": "description"
}
}
}
],
"parameters": {
@@ -6,57 +6,123 @@
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "id"
}
}
},
{
"ordinal": 2,
"name": "name!",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "name"
}
}
},
{
"ordinal": 3,
"name": "account_id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "account_id"
}
}
},
{
"ordinal": 4,
"name": "amount_cents!",
"type_info": "Int8"
"type_info": "Int8",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "amount_cents"
}
}
},
{
"ordinal": 5,
"name": "direction!",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "direction"
}
}
},
{
"ordinal": 6,
"name": "starts_at!",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "starts_at"
}
}
},
{
"ordinal": 7,
"name": "ends_at?",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "ends_at"
}
}
},
{
"ordinal": 8,
"name": "recurrence_every!",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "recurrence_every"
}
}
},
{
"ordinal": 9,
"name": "recurrence_unit!",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "recurrence_unit"
}
}
},
{
"ordinal": 10,
"name": "weekend_adjustment?",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "weekend_adjustment"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "users",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,37 +6,79 @@
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "id"
}
}
},
{
"ordinal": 2,
"name": "account_id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "account_id"
}
}
},
{
"ordinal": 3,
"name": "amount_cents!",
"type_info": "Int8"
"type_info": "Int8",
"origin": {
"Table": {
"table": "transactions",
"name": "amount_cents"
}
}
},
{
"ordinal": 4,
"name": "direction!",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "transactions",
"name": "direction"
}
}
},
{
"ordinal": 5,
"name": "date!",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "transactions",
"name": "date"
}
}
},
{
"ordinal": 6,
"name": "description!",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "transactions",
"name": "description"
}
}
}
],
"parameters": {
@@ -6,27 +6,57 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "app_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "app_id"
}
}
},
{
"ordinal": 2,
"name": "secret_hash",
"type_info": "Bytea"
"type_info": "Bytea",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "secret_hash"
}
}
},
{
"ordinal": 3,
"name": "expires_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "expires_at"
}
}
},
{
"ordinal": 4,
"name": "created_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -0,0 +1,53 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO user_apps (id, user_id)\n VALUES ($1, $2)\n ON CONFLICT (id) DO UPDATE SET\n id = EXCLUDED.id\n WHERE user_apps.user_id = $2\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.user_id AS \"user_id!\"\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "id"
}
}
},
{
"ordinal": 2,
"name": "user_id!",
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_apps",
"name": "user_id"
}
}
}
],
"parameters": {
"Left": [
"Uuid",
"Uuid"
]
},
"nullable": [
false,
false,
false
]
},
"hash": "977a6afaf7511915973fd20ab3a3fb03aeda84d0d460c9db1b5b1e7025493879"
}
@@ -6,12 +6,24 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "accounts",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "accounts",
"name": "name"
}
}
}
],
"parameters": {
@@ -6,7 +6,8 @@
{
"ordinal": 0,
"name": "is_valid!",
"type_info": "Bool"
"type_info": "Bool",
"origin": "Expression"
}
],
"parameters": {
@@ -6,32 +6,68 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "account_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "transactions",
"name": "account_id"
}
}
},
{
"ordinal": 2,
"name": "amount_cents",
"type_info": "Int8"
"type_info": "Int8",
"origin": {
"Table": {
"table": "transactions",
"name": "amount_cents"
}
}
},
{
"ordinal": 3,
"name": "direction",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "transactions",
"name": "direction"
}
}
},
{
"ordinal": 4,
"name": "date",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "transactions",
"name": "date"
}
}
},
{
"ordinal": 5,
"name": "description",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "transactions",
"name": "description"
}
}
}
],
"parameters": {
@@ -6,17 +6,35 @@
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "accounts",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "accounts",
"name": "id"
}
}
},
{
"ordinal": 2,
"name": "name!",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "accounts",
"name": "name"
}
}
}
],
"parameters": {
@@ -6,32 +6,68 @@
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "id"
}
}
},
{
"ordinal": 2,
"name": "app_id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "app_id"
}
}
},
{
"ordinal": 3,
"name": "secret_hash!",
"type_info": "Bytea"
"type_info": "Bytea",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "secret_hash"
}
}
},
{
"ordinal": 4,
"name": "expires_at!",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "expires_at"
}
}
},
{
"ordinal": 5,
"name": "created_at!",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "accounts",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,27 +6,57 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "app_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "app_id"
}
}
},
{
"ordinal": 2,
"name": "secret_hash",
"type_info": "Bytea"
"type_info": "Bytea",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "secret_hash"
}
}
},
{
"ordinal": 3,
"name": "expires_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "expires_at"
}
}
},
{
"ordinal": 4,
"name": "created_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "user_app_secrets",
"name": "created_at"
}
}
}
],
"parameters": {
@@ -1,35 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO user_apps (id, user_id)\n VALUES ($1, $2)\n ON CONFLICT (id) DO UPDATE SET\n id = EXCLUDED.id\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.user_id AS \"user_id!\"\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
},
{
"ordinal": 2,
"name": "user_id!",
"type_info": "Uuid"
}
],
"parameters": {
"Left": [
"Uuid",
"Uuid"
]
},
"nullable": [
false,
false,
false
]
},
"hash": "e4cc5672787e94ee849cb175416e92f053d6a36609af5f6a244a2c236e3ffb21"
}
@@ -6,52 +6,112 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "name"
}
}
},
{
"ordinal": 2,
"name": "account_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "account_id"
}
}
},
{
"ordinal": 3,
"name": "amount_cents",
"type_info": "Int8"
"type_info": "Int8",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "amount_cents"
}
}
},
{
"ordinal": 4,
"name": "direction",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "direction"
}
}
},
{
"ordinal": 5,
"name": "starts_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "starts_at"
}
}
},
{
"ordinal": 6,
"name": "ends_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "ends_at"
}
}
},
{
"ordinal": 7,
"name": "recurrence_every",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "recurrence_every"
}
}
},
{
"ordinal": 8,
"name": "recurrence_unit",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "recurrence_unit"
}
}
},
{
"ordinal": 9,
"name": "weekend_adjustment",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "weekend_adjustment"
}
}
}
],
"parameters": {
@@ -6,12 +6,24 @@
{
"ordinal": 0,
"name": "old_id?",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "users",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "id!",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "users",
"name": "id"
}
}
}
],
"parameters": {
@@ -6,52 +6,112 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "name"
}
}
},
{
"ordinal": 2,
"name": "account_id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "account_id"
}
}
},
{
"ordinal": 3,
"name": "amount_cents",
"type_info": "Int8"
"type_info": "Int8",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "amount_cents"
}
}
},
{
"ordinal": 4,
"name": "direction",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "direction"
}
}
},
{
"ordinal": 5,
"name": "starts_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "starts_at"
}
}
},
{
"ordinal": 6,
"name": "ends_at",
"type_info": "Timestamptz"
"type_info": "Timestamptz",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "ends_at"
}
}
},
{
"ordinal": 7,
"name": "recurrence_every",
"type_info": "Int4"
"type_info": "Int4",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "recurrence_every"
}
}
},
{
"ordinal": 8,
"name": "recurrence_unit",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "recurrence_unit"
}
}
},
{
"ordinal": 9,
"name": "weekend_adjustment",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "recurring_transactions",
"name": "weekend_adjustment"
}
}
}
],
"parameters": {
@@ -6,12 +6,24 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "accounts",
"name": "id"
}
}
},
{
"ordinal": 1,
"name": "name",
"type_info": "Text"
"type_info": "Text",
"origin": {
"Table": {
"table": "accounts",
"name": "name"
}
}
}
],
"parameters": {
@@ -6,7 +6,13 @@
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
"type_info": "Uuid",
"origin": {
"Table": {
"table": "tokens",
"name": "id"
}
}
}
],
"parameters": {
+16
View File
@@ -12,6 +12,22 @@ impl<T> Upsert<T> {
}
}
pub enum GuardedUpsert<T> {
Created(T),
Updated(T),
Rejected,
}
impl<T> GuardedUpsert<T> {
pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<GuardedUpsert<U>, E> {
Ok(match self {
Self::Created(value) => GuardedUpsert::Created(f(value)?),
Self::Updated(value) => GuardedUpsert::Updated(f(value)?),
Self::Rejected => GuardedUpsert::Rejected,
})
}
}
pub enum Delete {
Deleted,
NotFound,
+10 -5
View File
@@ -2,7 +2,7 @@ use uuid::Uuid;
use super::{
DatabaseError,
dtos::{Delete, Upsert, UserAppRow},
dtos::{Delete, GuardedUpsert, UserAppRow},
};
pub struct Query {
@@ -60,7 +60,7 @@ where
pub async fn upsert<'e, E>(
executor: E,
app: UserAppRow,
) -> Result<Upsert<UserAppRow>, DatabaseError>
) -> Result<GuardedUpsert<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
@@ -70,6 +70,7 @@ where
VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE SET
id = EXCLUDED.id
WHERE user_apps.user_id = $2
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
new_row.id AS "id!",
@@ -78,19 +79,23 @@ where
app.id,
app.user_id,
)
.fetch_one(executor)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
let Some(row) = row else {
return Ok(GuardedUpsert::Rejected);
};
let app = UserAppRow {
id: row.id,
user_id: row.user_id,
};
Ok(if row.old_id.is_some() {
Upsert::Updated(app)
GuardedUpsert::Updated(app)
} else {
Upsert::Created(app)
GuardedUpsert::Created(app)
})
}
+9
View File
@@ -50,6 +50,15 @@ impl IntoResponse for AppError {
Self::Repository(RepositoryError::Decode(error)) => {
tracing::error!(%error, "invalid stored data");
}
Self::Repository(RepositoryError::OwnershipConflict {
relation: ReferenceRelation::UserApp,
}) => {
tracing::warn!("user app ownership conflict");
return error_response(StatusCode::CONFLICT, "User app belongs to another user");
}
Self::Repository(RepositoryError::OwnershipConflict { relation }) => {
tracing::error!(%relation, "unhandled ownership conflict");
}
Self::Repository(RepositoryError::ReferenceViolation {
relation: ReferenceRelation::User,
kind: ReferenceViolationKind::Missing,
+3
View File
@@ -21,6 +21,9 @@ pub enum RepositoryError {
#[error(transparent)]
Decode(#[from] DecodeError),
#[error("{relation} is owned by another user")]
OwnershipConflict { relation: ReferenceRelation },
#[error("reference violation ({kind}: {relation})")]
ReferenceViolation {
relation: ReferenceRelation,
+56 -7
View File
@@ -1,8 +1,12 @@
use uuid::Uuid;
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
use super::{QueryConversionError, ReferenceRelation, RepositoryError, dtos::Upsert};
use crate::{
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
database::{
Constraint, DatabaseError,
dtos::{GuardedUpsert, UserAppRow},
user_apps as db,
},
domain::UserApp,
repository::DecodeError,
};
@@ -58,13 +62,23 @@ pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>,
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let app = db::upsert(executor, encode(app))
let outcome = db::upsert(executor, encode(app))
.await
.map_err(classify_upsert_error)?
.try_map(decode)?
.into();
.map_err(classify_upsert_error)?;
Ok(app)
map_upsert(outcome)
}
fn map_upsert(outcome: GuardedUpsert<UserAppRow>) -> Result<Upsert<UserApp>, RepositoryError> {
Ok(match outcome.try_map(decode)? {
GuardedUpsert::Created(app) => Upsert::Created(app),
GuardedUpsert::Updated(app) => Upsert::Updated(app),
GuardedUpsert::Rejected => {
return Err(RepositoryError::OwnershipConflict {
relation: ReferenceRelation::UserApp,
});
}
})
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
@@ -138,6 +152,41 @@ mod tests {
assert!(matches!(error, RepositoryError::Database(_)));
}
#[test]
fn maps_created_upsert() {
let row = row();
let id = row.id;
let Ok(Upsert::Created(app)) = map_upsert(GuardedUpsert::Created(row)) else {
panic!("expected created upsert");
};
assert_eq!(app.id, id);
}
#[test]
fn maps_updated_upsert() {
let row = row();
let id = row.id;
let Ok(Upsert::Updated(app)) = map_upsert(GuardedUpsert::Updated(row)) else {
panic!("expected updated upsert");
};
assert_eq!(app.id, id);
}
#[test]
fn maps_rejected_upsert_to_ownership_conflict() {
let Err(error) = map_upsert(GuardedUpsert::Rejected) else {
panic!("expected ownership conflict");
};
assert!(matches!(
error,
RepositoryError::OwnershipConflict {
relation: ReferenceRelation::UserApp,
}
));
}
fn row() -> UserAppRow {
UserAppRow {
id: Uuid::new_v4(),