Skip to content

Commit ea8461c

Browse files
Merge pull request #99 from NessieCanCode/fix-unknown-column-error-in-sql-query
Handle job_name column in SlurmDB queries
2 parents 00363da + b87a2de commit ea8461c

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/slurmdb.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,13 @@ def fetch_usage_records(self, start_time, end_time):
241241
if not cur.fetchone():
242242
cpu_col = "cpus_req"
243243

244+
job_name_col = "job_name"
245+
cur.execute(f"SHOW COLUMNS FROM {job_table} LIKE %s", (job_name_col,))
246+
if not cur.fetchone():
247+
job_name_col = "name"
248+
244249
query = (
245-
f"SELECT j.id_job AS jobid, j.name AS job_name, j.account, j.partition, "
250+
f"SELECT j.id_job AS jobid, j.{job_name_col} AS job_name, j.account, j.partition, "
246251
f"a.user AS user_name, j.time_start, j.time_end, j.tres_req, j.tres_alloc, "
247252
f"j.{cpu_col} AS cpus_alloc, j.state FROM {job_table} AS j "
248253
f"LEFT JOIN {assoc_table} AS a ON j.id_assoc = a.id_assoc "

test/unit/slurmdb_validation.test.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import json
33
from slurmdb import SlurmDB
4-
from slurm_schema import extract_schema
4+
from slurm_schema import extract_schema, extract_schema_from_dump
55

66
class SlurmDBValidationTests(unittest.TestCase):
77
def test_invalid_cluster_rejected(self):
@@ -181,7 +181,14 @@ def fake_connect():
181181
def test_fetch_usage_records_uses_cpus_req_if_alloc_missing(self):
182182
with open('test/example_slurm_schema_for_testing.json') as fh:
183183
schema = json.load(fh)
184+
schema_sql = extract_schema_from_dump('test/example_slurmdb_for_testing.sql')
184185
job_cols = schema.get('localcluster_job_table', [])
186+
job_cols_sql = schema_sql.get('localcluster_job_table', [])
187+
# ensure schema JSON and SQL dump agree on CPU columns
188+
self.assertIn('cpus_req', job_cols)
189+
self.assertIn('cpus_req', job_cols_sql)
190+
self.assertNotIn('cpus_alloc', job_cols)
191+
self.assertNotIn('cpus_alloc', job_cols_sql)
185192

186193
class FakeCursor:
187194
def __init__(self):
@@ -222,7 +229,60 @@ def cursor(self):
222229
db.connect = lambda: None
223230
db.fetch_usage_records(0, 0)
224231
queries = db._conn.cursor_obj.queries
225-
self.assertIn("j.cpus_req AS cpus_alloc", queries[1])
232+
self.assertIn("j.cpus_req AS cpus_alloc", queries[-1])
233+
234+
def test_fetch_usage_records_uses_job_name_column(self):
235+
with open('test/example_slurm_schema_for_testing.json') as fh:
236+
schema = json.load(fh)
237+
schema_sql = extract_schema_from_dump('test/example_slurmdb_for_testing.sql')
238+
job_cols = schema.get('localcluster_job_table', [])
239+
job_cols_sql = schema_sql.get('localcluster_job_table', [])
240+
# ensure schema JSON and SQL dump agree on job name column
241+
self.assertIn('job_name', job_cols)
242+
self.assertIn('job_name', job_cols_sql)
243+
self.assertNotIn('name', job_cols)
244+
self.assertNotIn('name', job_cols_sql)
245+
246+
class FakeCursor:
247+
def __init__(self):
248+
self.queries = []
249+
250+
def execute(self, query, params=None):
251+
self.queries.append(query)
252+
if query.lower().startswith("show columns"):
253+
column = params[0] if params else None
254+
if column in job_cols:
255+
self._fetchone = {'Field': column}
256+
else:
257+
self._fetchone = None
258+
else:
259+
self._fetchall = []
260+
261+
def fetchone(self):
262+
return getattr(self, "_fetchone", None)
263+
264+
def fetchall(self):
265+
return getattr(self, "_fetchall", [])
266+
267+
def __enter__(self):
268+
return self
269+
270+
def __exit__(self, exc_type, exc, tb):
271+
pass
272+
273+
class FakeConn:
274+
def __init__(self):
275+
self.cursor_obj = FakeCursor()
276+
277+
def cursor(self):
278+
return self.cursor_obj
279+
280+
db = SlurmDB(cluster="localcluster")
281+
db._conn = FakeConn()
282+
db.connect = lambda: None
283+
db.fetch_usage_records(0, 0)
284+
queries = db._conn.cursor_obj.queries
285+
self.assertIn("j.job_name AS job_name", queries[-1])
226286

227287
if __name__ == '__main__':
228288
unittest.main()

0 commit comments

Comments
 (0)