From a273f76d6dcd12b7ff0e4e6275ff5366c9e7550a Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Fri, 20 Nov 2026 10:57:25 -0500 Subject: [PATCH 1/2] fix(oauth): return the latest sqlalchemy installation when saves share a second MySQL DATETIME has 1 s precision, so saves in the same second tie on installed_at and find_bot/find_installation could return the older row. Break ties on the autoincrement id, sync and async, as #2071 does for SQLite3. Co-Authored-By: Claude --- .../installation_store/sqlalchemy/__init__.py | 8 ++-- .../test_async_sqlalchemy.py | 41 +++++++++++++++++++ .../installation_store/test_sqlalchemy.py | 40 ++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/slack_sdk/oauth/installation_store/sqlalchemy/__init__.py b/slack_sdk/oauth/installation_store/sqlalchemy/__init__.py index 942602b36..d207bf1f8 100644 --- a/slack_sdk/oauth/installation_store/sqlalchemy/__init__.py +++ b/slack_sdk/oauth/installation_store/sqlalchemy/__init__.py @@ -221,7 +221,7 @@ def find_bot( c.bot_token.is_not(None), # the latest one that has a bot token ) ) - .order_by(desc(c.installed_at)) + .order_by(desc(c.installed_at), desc(c.id)) .limit(1) ) @@ -256,7 +256,7 @@ def find_installation( c.user_id == user_id, ) - query = self.installations.select().where(where_clause).order_by(desc(c.installed_at)).limit(1) + query = self.installations.select().where(where_clause).order_by(desc(c.installed_at), desc(c.id)).limit(1) installation: Optional[Installation] = None with self.engine.connect() as conn: @@ -505,7 +505,7 @@ async def async_find_bot( c.bot_token.is_not(None), # the latest one that has a bot token ) ) - .order_by(desc(c.installed_at)) + .order_by(desc(c.installed_at), desc(c.id)) .limit(1) ) @@ -540,7 +540,7 @@ async def async_find_installation( c.user_id == user_id, ) - query = self.installations.select().where(where_clause).order_by(desc(c.installed_at)).limit(1) + query = self.installations.select().where(where_clause).order_by(desc(c.installed_at), desc(c.id)).limit(1) installation: Optional[Installation] = None async with self.engine.connect() as conn: diff --git a/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py b/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py index 6370ffb77..21062af86 100644 --- a/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py +++ b/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py @@ -1,5 +1,6 @@ import os import unittest +from datetime import datetime from tests.helpers import async_test from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine @@ -328,3 +329,43 @@ async def test_timezone_aware_datetime_compatibility(self): await self.store.async_save(installation) found = await self.store.async_find_installation(enterprise_id="E111", team_id="T111") self.assertIsNotNone(found) + + @async_test + async def test_find_returns_latest_save_within_the_same_second(self): + for suffix in ["old", "new"]: + await self.store.async_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}", + ) + ) + await self.store.async_save( + Installation( + app_id="A333", + enterprise_id="E333", + team_id="T333", + user_id="U555", + user_token="xoxp-user-only", + ) + ) + async with self.engine.begin() as conn: + for table in [self.store.installations, self.store.bots]: + await conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0))) + + bot = await self.store.async_find_bot(enterprise_id="E333", team_id="T333") + self.assertEqual(bot.bot_token, "xoxb-new") + + installation = await self.store.async_find_installation(enterprise_id="E333", team_id="T333") + self.assertEqual(installation.user_token, "xoxp-user-only") + self.assertEqual(installation.bot_token, "xoxb-new") + + installation = await self.store.async_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") diff --git a/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py b/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py index 75568e94c..139615c13 100644 --- a/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py +++ b/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py @@ -1,5 +1,6 @@ import os import unittest +from datetime import datetime import sqlalchemy from sqlalchemy.engine import Engine @@ -320,3 +321,42 @@ def test_timezone_aware_datetime_compatibility(self): self.store.save(installation) found = self.store.find_installation(enterprise_id="E111", team_id="T111") self.assertIsNotNone(found) + + def test_find_returns_latest_save_within_the_same_second(self): + for suffix in ["old", "new"]: + self.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}", + ) + ) + self.store.save( + Installation( + app_id="A333", + enterprise_id="E333", + team_id="T333", + user_id="U555", + user_token="xoxp-user-only", + ) + ) + with self.engine.begin() as conn: + for table in [self.store.installations, self.store.bots]: + conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0))) + + bot = self.store.find_bot(enterprise_id="E333", team_id="T333") + self.assertEqual(bot.bot_token, "xoxb-new") + + installation = self.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 = self.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") From 76832368b97f8d9f82d69227aa245d6117d836c7 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Fri, 20 Nov 2026 12:06:08 -0500 Subject: [PATCH 2/2] test(oauth): drop indexes so the same-second tie test covers every sqlalchemy lookup On SQLite a reverse index scan already breaks installed_at ties by rowid, so find_bot and the user_id lookup passed without the id tie-break. Dropping the indexes forces a sort, and removing any one of the four desc(id) tie-breaks now fails the test. Co-Authored-By: Claude --- .../slack_sdk/oauth/installation_store/test_async_sqlalchemy.py | 2 ++ tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py b/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py index 21062af86..11a2790d2 100644 --- a/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py +++ b/tests/slack_sdk/oauth/installation_store/test_async_sqlalchemy.py @@ -358,6 +358,8 @@ async def test_find_returns_latest_save_within_the_same_second(self): async with self.engine.begin() as conn: for table in [self.store.installations, self.store.bots]: await conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0))) + for index in table.indexes: + await conn.run_sync(index.drop) bot = await self.store.async_find_bot(enterprise_id="E333", team_id="T333") self.assertEqual(bot.bot_token, "xoxb-new") diff --git a/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py b/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py index 139615c13..dfe8594bf 100644 --- a/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py +++ b/tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py @@ -349,6 +349,8 @@ def test_find_returns_latest_save_within_the_same_second(self): with self.engine.begin() as conn: for table in [self.store.installations, self.store.bots]: conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0))) + for index in table.indexes: + index.drop(conn) bot = self.store.find_bot(enterprise_id="E333", team_id="T333") self.assertEqual(bot.bot_token, "xoxb-new")