lexicalunit/spellbot

SQL error: division by zero

lexicalunit opened this issue · 1 comments

Describe the bug

Saw this pop up twice so far.

Nov 02 16:01:17 lexicalunit-spellbot app/postgres.30014 [ROSE] [11-1]  sql_error_code = 22012 ERROR:  division by zero
Nov 02 16:01:17 lexicalunit-spellbot app/postgres.30014 [ROSE] [11-2]  sql_error_code = 22012 STATEMENT:  SELECT guild_awards.id AS guild_awards_id, guild_awards.guild_xid AS guild_awards_guild_xid, guild_awards.count AS guild_awards_count, guild_awards.repeating AS guild_awards_repeating, guild_awards.role AS guild_awards_role, guild_awards.message AS guild_awards_message
Nov 02 16:01:17 lexicalunit-spellbot app/postgres.30014 [ROSE] [11-3] 	FROM guild_awards
Nov 02 16:01:17 lexicalunit-spellbot app/postgres.30014 [ROSE] [11-4] 	WHERE guild_awards.guild_xid = 860389159469711380 AND (guild_awards.count = 10 OR 10 % guild_awards.count = 0 AND guild_awards.repeating IS true)
[2021-11-03 00:01:16][spellbot.interactions][ERROR] - error: rolling back database session due to unhandled exception: DataError: (psycopg2.errors.DivisionByZero) division by zero
[SQL: SELECT guild_awards.id AS guild_awards_id, guild_awards.guild_xid AS guild_awards_guild_xid, guild_awards.count AS guild_awards_count, guild_awards.repeating AS guild_awards_repeating, guild_awards.role AS guild_awards_role, guild_awards.message AS guild_awards_message 
FROM guild_awards 
WHERE guild_awards.guild_xid = %(guild_xid_1)s AND (guild_awards.count = %(count_1)s OR %(count_2)s %% guild_awards.count = %(param_1)s AND guild_awards.repeating IS true)]
[parameters: {'guild_xid_1': 860389159469711380, 'count_1': 10, 'count_2': 10, 'param_1': 0}]
(Background on this error at: https://sqlalche.me/e/14/9h9h)
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1799, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.DivisionByZero: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 482, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.9/site-packages/spellbot/interactions/__init__.py", line 90, in create
    yield interaction
  File "/usr/local/lib/python3.9/site-packages/spellbot/cogs/lfg.py", line 91, in lfg
    await interaction.execute(friends=friends, seats=seats, format=format)
  File "/usr/local/lib/python3.9/site-packages/spellbot/interactions/lfg_interaction.py", line 145, in execute
    await self._handle_direct_messages()
  File "/usr/local/lib/python3.9/site-packages/spellbot/interactions/lfg_interaction.py", line 376, in _handle_direct_messages
    new_roles = await self.services.awards.give_awards(self.ctx.guild_id, player_xids)
  File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 444, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
    return await fut
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/spellbot/services/awards.py", line 44, in give_awards
    next_award = award_q.filter(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2788, in one_or_none
    return self._iter().one_or_none()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2846, in _iter
    result = self.session.execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1689, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1611, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1478, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2023, in _handle_dbapi_exception
    util.raise_(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1799, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.errors.DivisionByZero) division by zero

I believe this was due to an invalid guild award set with count 0. This is no longer possible and any instance of a guild award with count 0 has been deleted.