From a4cfb8b41741b0f94d317dd4b087267fed7c787a Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Thu, 19 Nov 2026 17:09:18 -0500 Subject: [PATCH] fix(oauth): return the latest sqlite3 installation when saves share a second installed_at defaults to current_timestamp (1 s resolution), so saves in the same second tie and the lookups could return the older row. Break ties on the autoincrement id. Co-Authored-By: Claude --- .../installation_store/sqlite3/__init__.py | 8 ++-- .../oauth/installation_store/test_sqlite3.py | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/slack_sdk/oauth/installation_store/sqlite3/__init__.py b/slack_sdk/oauth/installation_store/sqlite3/__init__.py index 67da8eef4..aa46f5d33 100644 --- a/slack_sdk/oauth/installation_store/sqlite3/__init__.py +++ b/slack_sdk/oauth/installation_store/sqlite3/__init__.py @@ -322,7 +322,7 @@ def find_bot( enterprise_id = ? and team_id = ? - order by installed_at desc + order by installed_at desc, id desc limit 1 """, [self.client_id, enterprise_id or "", team_id or ""], @@ -425,7 +425,7 @@ def find_installation( enterprise_id = ? and team_id = ? - order by installed_at desc + order by installed_at desc, id desc limit 1 """, [self.client_id, enterprise_id or "", team_id], @@ -446,7 +446,7 @@ def find_installation( team_id = ? and user_id = ? - order by installed_at desc + order by installed_at desc, id desc limit 1 """, [self.client_id, enterprise_id or "", team_id, user_id], @@ -511,7 +511,7 @@ def find_installation( team_id = ? and bot_token is not null - order by installed_at desc + order by installed_at desc, id desc limit 1 """, [self.client_id, enterprise_id or "", team_id], diff --git a/tests/slack_sdk/oauth/installation_store/test_sqlite3.py b/tests/slack_sdk/oauth/installation_store/test_sqlite3.py index e45d640a7..ab0bb3b5a 100644 --- a/tests/slack_sdk/oauth/installation_store/test_sqlite3.py +++ b/tests/slack_sdk/oauth/installation_store/test_sqlite3.py @@ -330,3 +330,47 @@ def test_issue_1441_mixing_user_and_bot_installations(self): self.assertIsNone(installation) installation = store.find_installation(enterprise_id=None, team_id="T111") self.assertIsNone(installation) + + def test_find_returns_latest_save_within_the_same_second(self): + store = SQLite3InstallationStore(database="logs/test.db", client_id="111.222") + store.delete_all(enterprise_id="E333", team_id="T333") + + for suffix in ["old", "new"]: + store.save( + Installation( + app_id="A333", + enterprise_id="E333", + team_id="T333", + user_id="U333", + bot_id="B333", + bot_token=f"xoxb-{suffix}", + bot_scopes=["chat:write"], + bot_user_id="U444", + user_token=f"xoxp-{suffix}", + ) + ) + store.save( + Installation( + app_id="A333", + enterprise_id="E333", + team_id="T333", + user_id="U555", + user_token="xoxp-user-only", + ) + ) + with store.connect() as conn: + for table in ["slack_installations", "slack_bots"]: + conn.execute(f"update {table} set installed_at = '2026-11-19 10:00:00' where enterprise_id = 'E333'") + + bot = store.find_bot(enterprise_id="E333", team_id="T333") + self.assertEqual(bot.bot_token, "xoxb-new") + + installation = store.find_installation(enterprise_id="E333", team_id="T333") + self.assertEqual(installation.user_token, "xoxp-user-only") + self.assertEqual(installation.bot_token, "xoxb-new") + + installation = store.find_installation(enterprise_id="E333", team_id="T333", user_id="U333") + self.assertEqual(installation.user_token, "xoxp-new") + self.assertEqual(installation.bot_token, "xoxb-new") + + store.delete_all(enterprise_id="E333", team_id="T333")