Skip to content

Commit

Permalink
Merge pull request #709 from TotallyNotRobots/sqlalchemy-2.0
Browse files Browse the repository at this point in the history
refactor: Migrate to sqlalchemy 2.0-style statements
  • Loading branch information
linuxdaemon authored May 31, 2024
2 parents 2a2dd1b + 2108560 commit 3f437cd
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 117 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
env:
PYTHONDEVMODE: 1
PYTHONPATH: .
SQLALCHEMY_WARN_20: 1

- uses: codecov/codecov-action@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion cloudbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def migrate_db(self) -> None:
if not inspector.has_table(table.name):
continue

old_data = old_session.execute(table.select()).fetchall()
old_data = old_session.execute(table.select()).mappings().fetchall()
if not old_data:
continue

Expand Down
10 changes: 7 additions & 3 deletions cloudbot/util/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

from sqlalchemy import MetaData
from sqlalchemy.engine import Engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import close_all_sessions, scoped_session, sessionmaker
from sqlalchemy.orm import (
close_all_sessions,
declarative_base,
scoped_session,
sessionmaker,
)

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


Base = declarative_base()
base = Base
metadata: MetaData = Base.metadata
Session = scoped_session(sessionmaker())
Session = scoped_session(sessionmaker(future=True))


def configure(bind: Engine = None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion plugins/badwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load_bad(db):
"""- Should run on start of bot to load the existing words into the regex"""
words = []
new_cache = defaultdict(list)
for chan, word in db.execute(select([table.c.chan, table.c.word])):
for chan, word in db.execute(select(table.c.chan, table.c.word)):
new_cache[chan.casefold()].append(word)
words.append(word)

Expand Down
2 changes: 1 addition & 1 deletion plugins/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
def load_cache(db):
new_cache = {}
for row in db.execute(commands.select()):
new_cache[row["hook"]] = row["allowed"]
new_cache[row.hook] = row.allowed

allow_cache.clear()
allow_cache.update(new_cache)
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/autojoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
def load_cache(db):
new_cache = defaultdict(set)
for row in db.execute(table.select()):
new_cache[row["conn"]].add(row["chan"])
new_cache[row.conn].add(row.chan)

with db_lock:
chan_cache.clear()
Expand Down
6 changes: 3 additions & 3 deletions plugins/core/chan_key_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def load_keys(conn: IrcClient, db) -> None:
"""
Load channel keys to the client
"""
query = select(
[table.c.chan, table.c.key], table.c.conn == conn.name.lower()
query = select(table.c.chan, table.c.key).where(
table.c.conn == conn.name.lower()
)
conn.clear_channel_keys()
for row in db.execute(query):
conn.set_channel_key(row["chan"], row["key"])
conn.set_channel_key(row.chan, row.key)


@hook.irc_raw("MODE")
Expand Down
15 changes: 7 additions & 8 deletions plugins/core/ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
def load_cache(db):
new_cache = []
for row in db.execute(table.select()):
conn = row["connection"]
chan = row["channel"]
mask = row["mask"]
conn = row.connection
chan = row.channel
mask = row.mask
new_cache.append((conn, chan, mask))

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

rows = db.execute(
select(
[table.c.mask],
select(table.c.mask).where(
and_(
table.c.connection == conn.name.lower(),
table.c.channel == chan.lower(),
),
)
).fetchall()

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

return web.paste(out)

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

rows = db.execute(
select([table.c.channel, table.c.mask], whereclause)
select(table.c.channel, table.c.mask).where(whereclause)
).fetchall()

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

for row in rows:
ignores.setdefault(row["channel"], []).append(row["mask"])
ignores.setdefault(row.channel, []).append(row.mask)

out = ""
for chan, masks in ignores.items():
Expand Down
4 changes: 1 addition & 3 deletions plugins/core/optout.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ def clear_optout(db, conn, chan=None):
def load_cache(db):
new_cache = defaultdict(list)
for row in db.execute(optout_table.select()):
new_cache[row["network"]].append(
OptOut(row["chan"], row["hook"], row["allow"])
)
new_cache[row.network].append(OptOut(row.chan, row.hook, row.allow))

for opts in new_cache.values():
opts.sort(reverse=True)
Expand Down
6 changes: 3 additions & 3 deletions plugins/core/regex_chans.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
def load_cache(db):
new_cache = {}
for row in db.execute(table.select()):
conn = row["connection"]
chan = row["channel"]
status = row["status"]
conn = row.connection
chan = row.channel
status = row.status
if status == ENABLED:
value = True
elif status == DISABLED:
Expand Down
59 changes: 31 additions & 28 deletions plugins/duckhunt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
and_,
desc,
)
from sqlalchemy.orm import Session
from sqlalchemy.sql import select

from cloudbot import hook
Expand Down Expand Up @@ -136,14 +137,13 @@ def get_config(conn, field, default):


@hook.on_start()
def load_optout(db):
def load_optout(db: Session):
"""load a list of channels duckhunt should be off in. Right now I am being lazy and not
differentiating between networks this should be cleaned up later."""
new_data = defaultdict(list)
chans = db.execute(optout.select())
for row in chans:
chan = row["chan"]
new_data[row["network"].casefold()].append(chan.casefold())
new_data[row.network.casefold()].append(row.chan.casefold())

opt_out.clear()
opt_out.update(new_data)
Expand All @@ -157,11 +157,11 @@ def is_opt_out(network, chan):
def load_status(db):
rows = db.execute(status_table.select())
for row in rows:
net = row["network"]
chan = row["chan"]
net = row.network
chan = row.chan
status = get_state_table(net, chan)
status.game_on = row["active"]
status.no_duck_kick = row["duck_kick"]
status.game_on = row.active
status.no_duck_kick = row.duck_kick
if status.game_on:
set_ducktime(chan, net)

Expand Down Expand Up @@ -404,7 +404,7 @@ def dbupdate(nick, chan, db, conn, shoot, friend):

def update_score(nick, chan, db, conn, shoot=0, friend=0):
score = db.execute(
select([table.c.shot, table.c.befriend])
select(table.c.shot, table.c.befriend)
.where(table.c.network == conn.name)
.where(table.c.chan == chan.lower())
.where(table.c.name == nick.lower())
Expand Down Expand Up @@ -544,8 +544,10 @@ def get_scores(db, score_type, network, chan=None):
if chan is not None:
clause = and_(clause, table.c.chan == chan.lower())

query = select([table.c.name, table.c[score_type]], clause).order_by(
desc(table.c[score_type])
query = (
select(table.c.name, table.c[score_type])
.where(clause)
.order_by(desc(table.c[score_type]))
)

scores = db.execute(query).fetchall()
Expand Down Expand Up @@ -728,8 +730,7 @@ def hunt_opt_out(text, chan, db, conn):
if not is_opt_out(conn.name, channel):
return f"Duck hunt is already enabled in {channel}."

delete = optout.delete(optout.c.chan == channel.lower())
db.execute(delete)
db.execute(optout.delete().where(optout.c.chan == channel.lower()))
db.commit()
load_optout(db)

Expand All @@ -746,13 +747,13 @@ def duck_merge(text, conn, db, message):
return "Please specify two nicks for this command."

oldnickscore = db.execute(
select([table.c.name, table.c.chan, table.c.shot, table.c.befriend])
select(table.c.name, table.c.chan, table.c.shot, table.c.befriend)
.where(table.c.network == conn.name)
.where(table.c.name == oldnick)
).fetchall()

newnickscore = db.execute(
select([table.c.name, table.c.chan, table.c.shot, table.c.befriend])
select(table.c.name, table.c.chan, table.c.shot, table.c.befriend)
.where(table.c.network == conn.name)
.where(table.c.name == newnick)
).fetchall()
Expand All @@ -767,16 +768,16 @@ def duck_merge(text, conn, db, message):
new_chans = []

for row in newnickscore:
new_chans.append(row["chan"])
chan_data = duckmerge[row["chan"]]
chan_data["shot"] = row["shot"]
chan_data["befriend"] = row["befriend"]
new_chans.append(row.chan)
chan_data = duckmerge[row.chan]
chan_data["shot"] = row.shot
chan_data["befriend"] = row.befriend

for row in oldnickscore:
chan_name = row["chan"]
chan_name = row.chan
chan_data1 = duckmerge[chan_name]
shot: int = row["shot"]
_friends: int = row["befriend"]
shot: int = row.shot
_friends: int = row.befriend
chan_data1["shot"] += shot
chan_data1["befriend"] += _friends
total_kills += shot
Expand Down Expand Up @@ -833,7 +834,8 @@ def ducks_user(text, nick, chan, conn, db, message):
ducks: Dict[str, int] = defaultdict(int)
scores = db.execute(
select(
[table.c.name, table.c.chan, table.c.shot, table.c.befriend],
table.c.name, table.c.chan, table.c.shot, table.c.befriend
).where(
and_(
table.c.network == conn.name,
table.c.name == name,
Expand All @@ -849,13 +851,13 @@ def ducks_user(text, nick, chan, conn, db, message):
if scores:
has_hunted_in_chan = False
for row in scores:
if row["chan"].lower() == chan.lower():
if row.chan.lower() == chan.lower():
has_hunted_in_chan = True
ducks["chankilled"] += row["shot"]
ducks["chanfriends"] += row["befriend"]
ducks["chankilled"] += row.shot
ducks["chanfriends"] += row.befriend

ducks["killed"] += row["shot"]
ducks["friend"] += row["befriend"]
ducks["killed"] += row.shot
ducks["friend"] += row.befriend
ducks["chans"] += 1

# Check if the user has only participated in the hunt in this channel
Expand Down Expand Up @@ -897,7 +899,8 @@ def duck_stats(chan, conn, db, message):
"""- Prints duck statistics for the entire channel and totals for the network."""
scores = db.execute(
select(
[table.c.name, table.c.chan, table.c.shot, table.c.befriend],
table.c.name, table.c.chan, table.c.shot, table.c.befriend
).where(
table.c.network == conn.name,
)
).fetchall()
Expand Down
6 changes: 3 additions & 3 deletions plugins/factoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def load_cache(db):
new_cache.clear()
for row in db.execute(table.select()):
# assign variables
chan = row["chan"]
word = row["word"]
data = row["data"]
chan = row.chan
word = row.word
data = row.data
new_cache[chan][word] = data

factoid_cache.clear()
Expand Down
6 changes: 3 additions & 3 deletions plugins/grab.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def load_cache(db):
new_cache = grab_cache.copy()
new_cache.clear()
for row in db.execute(table.select().order_by(table.c.time)):
name = row["name"].lower()
quote = row["quote"]
chan = row["chan"]
name = row.name.lower()
quote = row.quote
chan = row.chan
new_cache.setdefault(chan, {}).setdefault(name, []).append(quote)

with cache_lock:
Expand Down
2 changes: 1 addition & 1 deletion plugins/herald.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def load_cache(db):
new_cache = herald_cache.copy()
new_cache.clear()
for row in db.execute(table.select()):
new_cache[row["chan"]][row["name"]] = row["quote"]
new_cache[row.chan][row.name] = row.quote

herald_cache.clear()
herald_cache.update(new_cache)
Expand Down
7 changes: 4 additions & 3 deletions plugins/hookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ def hookup(db, chan):

times = time.time() - 86400
results = db.execute(
select(
[seen_table.c.name],
select(seen_table.c.name)
.where(
and_(seen_table.c.chan == chan, seen_table.c.time > times),
).order_by(seen_table.c.time)
)
.order_by(seen_table.c.time)
).fetchall()

if not results or len(results) < 2:
Expand Down
2 changes: 1 addition & 1 deletion plugins/horoscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

def get_sign(db, nick):
row = db.execute(
select([table.c.sign]).where(table.c.nick == nick.lower())
select(table.c.sign).where(table.c.nick == nick.lower())
).fetchone()
if not row:
return None
Expand Down
Loading

0 comments on commit 3f437cd

Please sign in to comment.