Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions slack_sdk/oauth/installation_store/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
)

Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 suggestion (non-blocking): Same here.

Suggested change
await conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0)))
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)

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")
42 changes: 42 additions & 0 deletions tests/slack_sdk/oauth/installation_store/test_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from datetime import datetime

import sqlalchemy
from sqlalchemy.engine import Engine
Expand Down Expand Up @@ -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)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 suggestion (non-blocking): Could we drop the indexes after forcing the tie? On SQLite the index scan already returns the newest bot, so this test stays green if find_bot loses desc(c.id).

Suggested change
conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0)))
conn.execute(table.update().values(installed_at=datetime(2026, 11, 19, 10, 0, 0)))
for index in table.indexes:
index.drop(conn)

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")
Loading