Skip to content

Commit 3f437cd

Browse files
authored
Merge pull request #709 from TotallyNotRobots/sqlalchemy-2.0
refactor: Migrate to sqlalchemy 2.0-style statements
2 parents 2a2dd1b + 2108560 commit 3f437cd

27 files changed

+122
-117
lines changed

.github/workflows/pythonapp.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
env:
4646
PYTHONDEVMODE: 1
4747
PYTHONPATH: .
48+
SQLALCHEMY_WARN_20: 1
4849

4950
- uses: codecov/codecov-action@v4
5051
with:

cloudbot/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def migrate_db(self) -> None:
486486
if not inspector.has_table(table.name):
487487
continue
488488

489-
old_data = old_session.execute(table.select()).fetchall()
489+
old_data = old_session.execute(table.select()).mappings().fetchall()
490490
if not old_data:
491491
continue
492492

cloudbot/util/database.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44

55
from sqlalchemy import MetaData
66
from sqlalchemy.engine import Engine
7-
from sqlalchemy.ext.declarative import declarative_base
8-
from sqlalchemy.orm import close_all_sessions, scoped_session, sessionmaker
7+
from sqlalchemy.orm import (
8+
close_all_sessions,
9+
declarative_base,
10+
scoped_session,
11+
sessionmaker,
12+
)
913

1014
__all__ = ("metadata", "base", "Base", "Session", "configure")
1115

1216

1317
Base = declarative_base()
1418
base = Base
1519
metadata: MetaData = Base.metadata
16-
Session = scoped_session(sessionmaker())
20+
Session = scoped_session(sessionmaker(future=True))
1721

1822

1923
def configure(bind: Engine = None) -> None:

plugins/badwords.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def load_bad(db):
3333
"""- Should run on start of bot to load the existing words into the regex"""
3434
words = []
3535
new_cache = defaultdict(list)
36-
for chan, word in db.execute(select([table.c.chan, table.c.word])):
36+
for chan, word in db.execute(select(table.c.chan, table.c.word)):
3737
new_cache[chan.casefold()].append(word)
3838
words.append(word)
3939

plugins/chain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
def load_cache(db):
2424
new_cache = {}
2525
for row in db.execute(commands.select()):
26-
new_cache[row["hook"]] = row["allowed"]
26+
new_cache[row.hook] = row.allowed
2727

2828
allow_cache.clear()
2929
allow_cache.update(new_cache)

plugins/core/autojoin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
def load_cache(db):
2727
new_cache = defaultdict(set)
2828
for row in db.execute(table.select()):
29-
new_cache[row["conn"]].add(row["chan"])
29+
new_cache[row.conn].add(row.chan)
3030

3131
with db_lock:
3232
chan_cache.clear()

plugins/core/chan_key_db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ def load_keys(conn: IrcClient, db) -> None:
3535
"""
3636
Load channel keys to the client
3737
"""
38-
query = select(
39-
[table.c.chan, table.c.key], table.c.conn == conn.name.lower()
38+
query = select(table.c.chan, table.c.key).where(
39+
table.c.conn == conn.name.lower()
4040
)
4141
conn.clear_channel_keys()
4242
for row in db.execute(query):
43-
conn.set_channel_key(row["chan"], row["key"])
43+
conn.set_channel_key(row.chan, row.key)
4444

4545

4646
@hook.irc_raw("MODE")

plugins/core/ignore.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
def load_cache(db):
3535
new_cache = []
3636
for row in db.execute(table.select()):
37-
conn = row["connection"]
38-
chan = row["channel"]
39-
mask = row["mask"]
37+
conn = row.connection
38+
chan = row.channel
39+
mask = row.mask
4040
new_cache.append((conn, chan, mask))
4141

4242
ignore_cache.clear()
@@ -182,16 +182,15 @@ def listignores(db, conn, chan):
182182
"""- List all active ignores for the current channel"""
183183

184184
rows = db.execute(
185-
select(
186-
[table.c.mask],
185+
select(table.c.mask).where(
187186
and_(
188187
table.c.connection == conn.name.lower(),
189188
table.c.channel == chan.lower(),
190189
),
191190
)
192191
).fetchall()
193192

194-
out = "\n".join(row["mask"] for row in rows) + "\n"
193+
out = "\n".join(row.mask for row in rows) + "\n"
195194

196195
return web.paste(out)
197196

@@ -245,13 +244,13 @@ def list_all_ignores(db, conn, text):
245244
whereclause = and_(whereclause, table.c.channel == text.lower())
246245

247246
rows = db.execute(
248-
select([table.c.channel, table.c.mask], whereclause)
247+
select(table.c.channel, table.c.mask).where(whereclause)
249248
).fetchall()
250249

251250
ignores: Dict[str, List[str]] = OrderedDict()
252251

253252
for row in rows:
254-
ignores.setdefault(row["channel"], []).append(row["mask"])
253+
ignores.setdefault(row.channel, []).append(row.mask)
255254

256255
out = ""
257256
for chan, masks in ignores.items():

plugins/core/optout.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ def clear_optout(db, conn, chan=None):
175175
def load_cache(db):
176176
new_cache = defaultdict(list)
177177
for row in db.execute(optout_table.select()):
178-
new_cache[row["network"]].append(
179-
OptOut(row["chan"], row["hook"], row["allow"])
180-
)
178+
new_cache[row.network].append(OptOut(row.chan, row.hook, row.allow))
181179

182180
for opts in new_cache.values():
183181
opts.sort(reverse=True)

plugins/core/regex_chans.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
def load_cache(db):
3737
new_cache = {}
3838
for row in db.execute(table.select()):
39-
conn = row["connection"]
40-
chan = row["channel"]
41-
status = row["status"]
39+
conn = row.connection
40+
chan = row.channel
41+
status = row.status
4242
if status == ENABLED:
4343
value = True
4444
elif status == DISABLED:

0 commit comments

Comments
 (0)