Skip to content

Commit 6e92cd9

Browse files
Merge pull request #102 from NessieCanCode/fix-missing-tres-table-error
Handle missing TRES table
2 parents a6dc5df + 6dd961c commit 6e92cd9

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

src/slurmdb.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,33 @@ def _get_tres_map(self):
243243
if self._tres_map is None:
244244
self.connect()
245245
self._tres_map = {}
246-
with self._conn.cursor() as cur:
247-
cur.execute("SELECT id, type, name FROM tres")
248-
for row in cur.fetchall():
249-
t_type = row.get("type")
250-
t_name = row.get("name")
251-
if t_type == "gres":
252-
name = f"{t_type}/{t_name}" if t_name else t_type
253-
elif t_name:
254-
name = f"{t_type}/{t_name}"
255-
else:
256-
name = t_type
257-
self._tres_map[row["id"]] = name
246+
for table in ("tres", "tres_table"):
247+
try:
248+
with self._conn.cursor() as cur:
249+
cur.execute(f"SELECT id, type, name FROM {table}")
250+
for row in cur.fetchall():
251+
t_type = row.get("type")
252+
t_name = row.get("name")
253+
if t_type == "gres":
254+
name = f"{t_type}/{t_name}" if t_name else t_type
255+
elif t_name:
256+
name = f"{t_type}/{t_name}"
257+
else:
258+
name = t_type
259+
self._tres_map[row["id"]] = name
260+
break
261+
except Exception as e:
262+
# Older Slurm databases might use a different table name
263+
# or not support TRES at all. If a table is missing,
264+
# fall back to the next option or return an empty map.
265+
if not (
266+
pymysql
267+
and isinstance(e, pymysql.err.ProgrammingError)
268+
and e.args
269+
and e.args[0] == 1146
270+
):
271+
raise
272+
# If both queries failed we leave the map empty.
258273
return self._tres_map
259274

260275
def _tres_to_str(self, tres_str):

test/unit/slurmdb_validation.test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import json
3+
import pymysql
34
from slurmdb import SlurmDB
45
from slurm_schema import extract_schema, extract_schema_from_dump
56

@@ -132,6 +133,60 @@ def fake_connect():
132133
self.assertTrue(conn.closed)
133134
self.assertIsNone(db._conn)
134135

136+
def test_get_tres_map_handles_missing_table(self):
137+
db = SlurmDB()
138+
db.connect = lambda: None
139+
140+
class FakeCursor:
141+
def __enter__(self):
142+
return self
143+
144+
def __exit__(self, exc_type, exc, tb):
145+
pass
146+
147+
def execute(self, query):
148+
raise pymysql.err.ProgrammingError(1146, "Table 'slurm_acct_db.tres' doesn't exist")
149+
150+
def fetchall(self):
151+
return []
152+
153+
class FakeConn:
154+
def cursor(self):
155+
return FakeCursor()
156+
157+
db._conn = FakeConn()
158+
tmap = db._get_tres_map()
159+
self.assertEqual(tmap, {})
160+
161+
def test_get_tres_map_uses_tres_table_when_missing_tres(self):
162+
db = SlurmDB()
163+
db.connect = lambda: None
164+
165+
class FakeCursor:
166+
def __enter__(self):
167+
return self
168+
169+
def __exit__(self, exc_type, exc, tb):
170+
pass
171+
172+
def execute(self, query):
173+
self.query = query
174+
if "tres_table" not in query:
175+
raise pymysql.err.ProgrammingError(1146, "Table 'slurm_acct_db.tres' doesn't exist")
176+
177+
def fetchall(self):
178+
if "tres_table" in self.query:
179+
return [{"id": 1, "type": "cpu", "name": ""}]
180+
return []
181+
182+
class FakeConn:
183+
def cursor(self):
184+
return FakeCursor()
185+
186+
db._conn = FakeConn()
187+
tmap = db._get_tres_map()
188+
self.assertEqual(tmap, {1: "cpu"})
189+
135190
def test_extract_schema_with_context_manager_closes_connection(self):
136191
class FakeCursor:
137192
def __init__(self, dbname):

0 commit comments

Comments
 (0)