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..11a2790d2 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,45 @@ 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))) + 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") + + 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..dfe8594bf 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,44 @@ 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))) + 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") + + 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")