Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
fix: Convert a string from a jsonb colum to a proper dict
Browse files Browse the repository at this point in the history
  • Loading branch information
rsavoye committed Mar 1, 2024
1 parent b7ce416 commit a5faf30
Showing 1 changed file with 11 additions and 50 deletions.
61 changes: 11 additions & 50 deletions tm_admin/pgsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def __init__(self,
self.table = None
self.yaml = None
if table:
filespec = Path(f"{rootdir}/{table}/{table}.yaml")
self.yaml = YamlFile(filespec)
self.yaml = YamlFile(f"{rootdir}/{table}/{table}.yaml")
self.table = table
self.yaml2py = {'int32': 'int',
'int64': 'int',
Expand Down Expand Up @@ -290,56 +289,18 @@ async def getColumns(self,
# print(sql)
results = await self.execute(sql)

types = dict()
# We only need to get the data types once
for key, value in results[0].items():
if type(value) == str and value[0] == "{":
item = eval(value)
for k, v in item[key][0].items():
obj = eval(f"{self.table.capitalize()}_{key}Table({v})")
xx = obj.data[k]
# It's an internal data structure from a *_class.py file
if type(xx) != int and type(xx) != str:
types[k] = xx

data = list()
raw = False # FIXME: for now force converting the enums
if raw:
for record in results:
# print(record)
for k, v in record.items():
# it's a dictionary masquarading as a string, but for us it's
# a jsonb column
if self.types[k] == 'jsonb':
entry = eval(f"%s" % record.get(k))
if not entry:
continue
new = dict()
# Entry = eval(f"{record.get(k)})
if k in entry:
for item in entry[k]:
for k1, v1 in item.items():
# It's in types_tm.py, so convert the value to the Enum
if k1 in types:
enumval = type(types[k1])
if v1 == 0:
newenum = enumval(1)
else:
newenum = enumval(v1)
new[k1] = newenum
else:
new[k1] = v1
else:
# We see this in old data, just one entry
new = entry
data.append(new)
else:
data.append({k: v})
return data

# SELECT * FROM teams WHERE EXISTS (SELECT TRUE FROM jsonb_array_elements(team_members->'members') x WHERE x->>'function'='MANAGER');
for record in results:
print(record)
entry = dict()
for key, value in record.items():
if type(value) == str and value[0] == "{":
entry[key] = eval(value)
else:
entry[key] = value
data.append(entry)

return results
return data

async def main():
"""This main function lets this class be run standalone by a bash script."""
Expand Down

0 comments on commit a5faf30

Please sign in to comment.