Skip to content

Commit 6827878

Browse files
committed
Update SQLAlchemy module to enbale developers to customize table names
1 parent b972d97 commit 6827878

File tree

4 files changed

+123
-63
lines changed

4 files changed

+123
-63
lines changed

integration_tests/samples/oauth/oauth_v2_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async def oauth_callback(req: Request):
113113

114114

115115
# ---------------------
116-
# Flask App for Slack events
116+
# Sanic App for Slack events
117117
# ---------------------
118118

119119
import json

slack_sdk/oauth/installation_store/sqlalchemy/__init__.py

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,102 @@
88
from slack_sdk.oauth.installation_store.models.installation import Installation
99

1010
import sqlalchemy
11-
from sqlalchemy import Table, Column, Integer, String, DateTime, Index, and_, desc
11+
from sqlalchemy import (
12+
Table,
13+
Column,
14+
Integer,
15+
String,
16+
DateTime,
17+
Index,
18+
and_,
19+
desc,
20+
MetaData,
21+
)
1222

1323

1424
class SQLAlchemyInstallationStore(InstallationStore):
15-
engine: Engine
25+
default_bots_table_name: str = "slack_bots"
26+
default_installations_table_name: str = "slack_installations"
27+
1628
client_id: str
29+
engine: Engine
30+
metadata: MetaData
31+
installations: Table
1732

18-
metadata = sqlalchemy.MetaData()
19-
installations: Table = sqlalchemy.Table(
20-
"installations",
21-
metadata,
22-
Column("id", Integer, primary_key=True, autoincrement=True),
23-
Column("client_id", String, nullable=False),
24-
Column("app_id", String, nullable=False),
25-
Column("enterprise_id", String),
26-
Column("team_id", String),
27-
Column("bot_token", String),
28-
Column("bot_id", String),
29-
Column("bot_user_id", String),
30-
Column("bot_scopes", String),
31-
Column("user_id", String, nullable=False),
32-
Column("user_token", String),
33-
Column("user_scopes", String),
34-
Column("incoming_webhook_url", String),
35-
Column("incoming_webhook_channel_id", String),
36-
Column("incoming_webhook_configuration_url", String),
37-
Column(
38-
"installed_at", DateTime, nullable=False, default=sqlalchemy.sql.func.now()
39-
),
40-
Index("installations_idx", "client_id", "enterprise_id", "team_id", "user_id",),
41-
)
33+
@classmethod
34+
def build_installations_table(cls, metadata: MetaData, table_name: str) -> Table:
35+
return sqlalchemy.Table(
36+
table_name,
37+
metadata,
38+
Column("id", Integer, primary_key=True, autoincrement=True),
39+
Column("client_id", String, nullable=False),
40+
Column("app_id", String, nullable=False),
41+
Column("enterprise_id", String),
42+
Column("team_id", String),
43+
Column("bot_token", String),
44+
Column("bot_id", String),
45+
Column("bot_user_id", String),
46+
Column("bot_scopes", String),
47+
Column("user_id", String, nullable=False),
48+
Column("user_token", String),
49+
Column("user_scopes", String),
50+
Column("incoming_webhook_url", String),
51+
Column("incoming_webhook_channel_id", String),
52+
Column("incoming_webhook_configuration_url", String),
53+
Column(
54+
"installed_at",
55+
DateTime,
56+
nullable=False,
57+
default=sqlalchemy.sql.func.now(),
58+
),
59+
Index(
60+
"installations_idx",
61+
"client_id",
62+
"enterprise_id",
63+
"team_id",
64+
"user_id",
65+
"installed_at",
66+
),
67+
)
4268

43-
bots = Table(
44-
"bots",
45-
metadata,
46-
Column("id", Integer, primary_key=True, autoincrement=True),
47-
Column("client_id", String, nullable=False),
48-
Column("app_id", String, nullable=False),
49-
Column("enterprise_id", String),
50-
Column("team_id", String),
51-
Column("bot_token", String),
52-
Column("bot_id", String),
53-
Column("bot_user_id", String),
54-
Column("bot_scopes", String),
55-
Column(
56-
"installed_at", DateTime, nullable=False, default=sqlalchemy.sql.func.now()
57-
),
58-
Index("bots_idx", "client_id", "enterprise_id", "team_id",),
59-
)
69+
@classmethod
70+
def build_bots_table(cls, metadata: MetaData, table_name: str) -> Table:
71+
return Table(
72+
table_name,
73+
metadata,
74+
Column("id", Integer, primary_key=True, autoincrement=True),
75+
Column("client_id", String, nullable=False),
76+
Column("app_id", String, nullable=False),
77+
Column("enterprise_id", String),
78+
Column("team_id", String),
79+
Column("bot_token", String),
80+
Column("bot_id", String),
81+
Column("bot_user_id", String),
82+
Column("bot_scopes", String),
83+
Column(
84+
"installed_at",
85+
DateTime,
86+
nullable=False,
87+
default=sqlalchemy.sql.func.now(),
88+
),
89+
Index("bots_idx", "client_id", "enterprise_id", "team_id", "installed_at"),
90+
)
6091

6192
def __init__(
6293
self,
6394
client_id: str,
6495
engine: Engine,
96+
bots_table_name: str = default_bots_table_name,
97+
installations_table_name: str = default_installations_table_name,
6598
logger: Logger = logging.getLogger(__name__),
6699
):
100+
self.metadata = sqlalchemy.MetaData()
101+
self.bots = self.build_bots_table(
102+
metadata=self.metadata, table_name=bots_table_name
103+
)
104+
self.installations = self.build_installations_table(
105+
metadata=self.metadata, table_name=installations_table_name
106+
)
67107
self.client_id = client_id
68108
self._logger = logger
69109
self.engine = engine

slack_sdk/oauth/installation_store/sqlite3/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def logger(self) -> Logger:
3434
def init(self):
3535
try:
3636
with sqlite3.connect(database=self.database) as conn:
37-
cur = conn.execute("select count(1) from installations;")
37+
cur = conn.execute("select count(1) from slack_installations;")
3838
row_num = cur.fetchone()[0]
3939
self.logger.debug(
4040
f"{row_num} installations are stored in {self.database}"
@@ -52,7 +52,7 @@ def create_tables(self):
5252
with sqlite3.connect(database=self.database) as conn:
5353
conn.execute(
5454
"""
55-
create table installations (
55+
create table slack_installations (
5656
id integer primary key autoincrement,
5757
client_id text not null,
5858
app_id text not null,
@@ -74,12 +74,18 @@ def create_tables(self):
7474
)
7575
conn.execute(
7676
"""
77-
create index installations_idx on installations (client_id, enterprise_id, team_id, user_id);
77+
create index slack_installations_idx on slack_installations (
78+
client_id,
79+
enterprise_id,
80+
team_id,
81+
user_id,
82+
installed_at
83+
);
7884
"""
7985
)
8086
conn.execute(
8187
"""
82-
create table bots (
88+
create table slack_bots (
8389
id integer primary key autoincrement,
8490
client_id text not null,
8591
app_id text not null,
@@ -95,7 +101,12 @@ def create_tables(self):
95101
)
96102
conn.execute(
97103
"""
98-
create index bots_idx on bots (client_id, enterprise_id, team_id);
104+
create index slack_bots_idx on slack_bots (
105+
client_id,
106+
enterprise_id,
107+
team_id,
108+
installed_at
109+
);
99110
"""
100111
)
101112
self.logger.debug(f"Tables have been created (database: {self.database})")
@@ -108,7 +119,7 @@ def save(self, installation: Installation):
108119
with self.connect() as conn:
109120
conn.execute(
110121
"""
111-
insert into bots (
122+
insert into slack_bots (
112123
client_id,
113124
app_id,
114125
enterprise_id,
@@ -143,7 +154,7 @@ def save(self, installation: Installation):
143154
)
144155
conn.execute(
145156
"""
146-
insert into installations (
157+
insert into slack_installations (
147158
client_id,
148159
app_id,
149160
enterprise_id,
@@ -197,7 +208,7 @@ def save(self, installation: Installation):
197208
],
198209
)
199210
self.logger.debug(
200-
f"New rows in bots and installations) have been created (database: {self.database})"
211+
f"New rows in slack_bots and slack_installations) have been created (database: {self.database})"
201212
)
202213
conn.commit()
203214

@@ -224,7 +235,7 @@ def find_bot(
224235
bot_scopes,
225236
installed_at
226237
from
227-
bots
238+
slack_bots
228239
where
229240
client_id = ?
230241
and

slack_sdk/oauth/state_store/sqlalchemy/__init__.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,41 @@
66

77
from ..state_store import OAuthStateStore
88
import sqlalchemy
9-
from sqlalchemy import Table, Column, Integer, String, DateTime, and_
9+
from sqlalchemy import Table, Column, Integer, String, DateTime, and_, MetaData
1010
from sqlalchemy.engine import Engine
1111

1212

1313
class SQLAlchemyOAuthStateStore(OAuthStateStore):
14-
engine: Engine
14+
default_table_name: str = "slack_oauth_states"
15+
1516
expiration_seconds: int
17+
engine: Engine
18+
metadata: MetaData
19+
oauth_states: Table
1620

17-
metadata = sqlalchemy.MetaData()
18-
oauth_states: Table = sqlalchemy.Table(
19-
"oauth_states",
20-
metadata,
21-
Column("id", Integer, primary_key=True, autoincrement=True),
22-
Column("state", String, nullable=False),
23-
Column("expire_at", DateTime, nullable=False),
24-
)
21+
@classmethod
22+
def build_oauth_states_table(cls, metadata: MetaData, table_name: str) -> Table:
23+
return sqlalchemy.Table(
24+
table_name,
25+
metadata,
26+
metadata,
27+
Column("id", Integer, primary_key=True, autoincrement=True),
28+
Column("state", String, nullable=False),
29+
Column("expire_at", DateTime, nullable=False),
30+
)
2531

2632
def __init__(
2733
self,
2834
expiration_seconds: int,
2935
engine: Engine,
3036
logger: Logger = logging.getLogger(__name__),
37+
table_name: str = default_table_name,
3138
):
3239
self.expiration_seconds = expiration_seconds
3340
self._logger = logger
3441
self.engine = engine
42+
self.metadata = MetaData()
43+
self.oauth_states = self.build_oauth_states_table(self.metadata, table_name)
3544

3645
@property
3746
def logger(self) -> Logger:

0 commit comments

Comments
 (0)