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

Commit

Permalink
fix: Add function for updating and append jsonb columns
Browse files Browse the repository at this point in the history
  • Loading branch information
rsavoye committed Mar 8, 2024
1 parent 33fa0fa commit cc8b503
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions tm_admin/pgsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ async def insertRecords(self,
return False

data = dict()
jcol = dict()
for entry in records:
for key, value in entry.data.items():
val = self.types[key]
# print(type(val), value)
if not value:
continue
if type(value) == list:
Expand All @@ -157,11 +159,23 @@ async def insertRecords(self,
elif type(value) == dict:
# It's for a jsonb column
# a dict uses single quotes, postgres wants double quotes.
newval = str(value).replace("'", '"')
data[key] = f"{newval}"
continue
# newval = str(value).replace("'", '"')
breakpoint()
for entry in value[key]:
for k, v in entry.items():
if str(type(v))[:5] == "<enum":
jcol[k] = v.name
else:
jcol[k] = v
print(jcol)
# continue
# This is a bit ugly. When passing a geometry we need to get rid of the
# double quote around this value.
elif val == "bool":
if value == True:
data[key] = f"true"
else:
data[key] = f"false"
elif val == "Polygon":
data[key] = f"XST_GeomFromText('{value}')X"
elif val == "Point":
Expand Down Expand Up @@ -347,6 +361,66 @@ async def getColumns(self,

return data

async def updateJsonb(self,
history: list,
task_id: int,
project_id: int,
columns: str,
):
"""
FIXME: a work in progress
Update a json column. Note that this will replacew any existing data
in the columns.
Args:
history (list): The task history to update
task_id (int): The task to update
project_id (int): The project this task is in
column (str): The column to update.
Returns:
(bool): If it worked
"""
# log.warning(f"updateHistory(): unimplemented!")

data = str()
for entry in history:
for k, v in entry.items():
if str(type(v))[:5] == "<enum":
data += f'" {v.name}", '
else:
data += f'" {v}", '
#asc = str(entry).replace("'", '"').replace("\\'", "'")
sql = "UPDATE %s SET %s = '{\"%s\": [%s]}' WHERE id=%d AND project_id=%d" % (self.table, column, column, data[:-2], task_id, project_id)
print(sql)
result = await self.execute(sql)

async def appendJsonb(self,
data: list,
column: str,
where: dict,
):
"""
FIXME: a work in progress
Update the task history column.
Args:
data (list): The data for this column to update
column (str): The column to update.
Returns:
(bool): If it worked
"""
# log.warning(f"updateHistory(): unimplemented!")

for entry in data:
# SQL wants the string value
entry['action'] = entry['action'].name
asc = str(entry).replace("'", '"').replace("\\'", "'").replace('""', '"')
sql = "UPDATE tasks SET history = history||'[%s]'::jsonb WHERE id=%d AND project_id=%d" % (asc, task_id, project_id)
# print(sql)
result = await self.execute(sql)

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

0 comments on commit cc8b503

Please sign in to comment.