Fix foreign key constraint when cleaning up upstream OAuth 2.0 links

https://sentry.tools.element.io/organizations/element/issues/11222736/
This commit is contained in:
Quentin Gliech
2026-01-20 14:29:27 +01:00
parent ab90e89685
commit e7c1f126be
3 changed files with 32 additions and 26 deletions

View File

@@ -0,0 +1,24 @@
{
"db_name": "PostgreSQL",
"query": "\n WITH\n to_delete AS (\n SELECT upstream_oauth_link_id\n FROM upstream_oauth_links\n WHERE user_id IS NULL\n AND ($1::uuid IS NULL OR upstream_oauth_link_id > $1)\n AND upstream_oauth_link_id <= $2\n ORDER BY upstream_oauth_link_id\n LIMIT $3\n ),\n deleted_sessions AS (\n DELETE FROM upstream_oauth_authorization_sessions\n USING to_delete\n WHERE upstream_oauth_authorization_sessions.upstream_oauth_link_id = to_delete.upstream_oauth_link_id\n )\n DELETE FROM upstream_oauth_links\n USING to_delete\n WHERE upstream_oauth_links.upstream_oauth_link_id = to_delete.upstream_oauth_link_id\n RETURNING upstream_oauth_links.upstream_oauth_link_id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "upstream_oauth_link_id",
"type_info": "Uuid"
}
],
"parameters": {
"Left": [
"Uuid",
"Uuid",
"Int8"
]
},
"nullable": [
false
]
},
"hash": "623097fc45ffa5d6e09fedfbdbe5e42662e9854430bcd9e53598debf99c9ca37"
}

View File

@@ -1,24 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n WITH to_delete AS (\n SELECT upstream_oauth_link_id\n FROM upstream_oauth_links\n WHERE user_id IS NULL\n AND ($1::uuid IS NULL OR upstream_oauth_link_id > $1)\n AND upstream_oauth_link_id <= $2\n ORDER BY upstream_oauth_link_id\n LIMIT $3\n )\n DELETE FROM upstream_oauth_links\n USING to_delete\n WHERE upstream_oauth_links.upstream_oauth_link_id = to_delete.upstream_oauth_link_id\n RETURNING upstream_oauth_links.upstream_oauth_link_id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "upstream_oauth_link_id",
"type_info": "Uuid"
}
],
"parameters": {
"Left": [
"Uuid",
"Uuid",
"Int8"
]
},
"nullable": [
false
]
},
"hash": "67b4a124ea3e12902dc4256cf95950508f7eb24f4c9d62b34815c8e8940e4676"
}

View File

@@ -466,7 +466,8 @@ impl UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'_> {
// `MAX(uuid)` isn't a thing in Postgres, so we aggregate on the client side. // `MAX(uuid)` isn't a thing in Postgres, so we aggregate on the client side.
let res = sqlx::query_scalar!( let res = sqlx::query_scalar!(
r#" r#"
WITH to_delete AS ( WITH
to_delete AS (
SELECT upstream_oauth_link_id SELECT upstream_oauth_link_id
FROM upstream_oauth_links FROM upstream_oauth_links
WHERE user_id IS NULL WHERE user_id IS NULL
@@ -474,7 +475,12 @@ impl UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'_> {
AND upstream_oauth_link_id <= $2 AND upstream_oauth_link_id <= $2
ORDER BY upstream_oauth_link_id ORDER BY upstream_oauth_link_id
LIMIT $3 LIMIT $3
) ),
deleted_sessions AS (
DELETE FROM upstream_oauth_authorization_sessions
USING to_delete
WHERE upstream_oauth_authorization_sessions.upstream_oauth_link_id = to_delete.upstream_oauth_link_id
)
DELETE FROM upstream_oauth_links DELETE FROM upstream_oauth_links
USING to_delete USING to_delete
WHERE upstream_oauth_links.upstream_oauth_link_id = to_delete.upstream_oauth_link_id WHERE upstream_oauth_links.upstream_oauth_link_id = to_delete.upstream_oauth_link_id