fix(chalice): fixed delete/update metadata used in conditional recording (#3068)
This commit is contained in:
parent
256c065153
commit
d79665cbea
3 changed files with 98 additions and 9 deletions
|
|
@ -98,17 +98,23 @@ def __edit(project_id, col_index, colname, new_name):
|
|||
if col_index not in list(old_metas.keys()):
|
||||
return {"errors": ["custom field not found"]}
|
||||
|
||||
with pg_client.PostgresClient() as cur:
|
||||
if old_metas[col_index]["key"] != new_name:
|
||||
if old_metas[col_index]["key"] != new_name:
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(f"""UPDATE public.projects
|
||||
SET {colname} = %(value)s
|
||||
WHERE project_id = %(project_id)s
|
||||
AND deleted_at ISNULL
|
||||
RETURNING {colname};""",
|
||||
RETURNING {colname},
|
||||
(SELECT {colname} FROM projects WHERE project_id = %(project_id)s) AS old_{colname};""",
|
||||
{"project_id": project_id, "value": new_name})
|
||||
cur.execute(query=query)
|
||||
new_name = cur.fetchone()[colname]
|
||||
row = cur.fetchone()
|
||||
new_name = row[colname]
|
||||
old_name = row['old_' + colname]
|
||||
old_metas[col_index]["key"] = new_name
|
||||
projects.rename_metadata_condition(project_id=project_id,
|
||||
old_metadata_key=old_name,
|
||||
new_metadata_key=new_name)
|
||||
return {"data": old_metas[col_index]}
|
||||
|
||||
|
||||
|
|
@ -121,8 +127,8 @@ def edit(tenant_id, project_id, index: int, new_name: str):
|
|||
def delete(tenant_id, project_id, index: int):
|
||||
index = int(index)
|
||||
old_segments = get(project_id)
|
||||
old_segments = [k["index"] for k in old_segments]
|
||||
if index not in old_segments:
|
||||
old_indexes = [k["index"] for k in old_segments]
|
||||
if index not in old_indexes:
|
||||
return {"errors": ["custom field not found"]}
|
||||
|
||||
with pg_client.PostgresClient() as cur:
|
||||
|
|
@ -132,7 +138,8 @@ def delete(tenant_id, project_id, index: int):
|
|||
WHERE project_id = %(project_id)s AND deleted_at ISNULL;""",
|
||||
{"project_id": project_id})
|
||||
cur.execute(query=query)
|
||||
|
||||
projects.delete_metadata_condition(project_id=project_id,
|
||||
metadata_key=old_segments[old_indexes.index(index)]["key"])
|
||||
return {"data": get(project_id)}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -413,7 +413,6 @@ def update_project_conditions(project_id, conditions):
|
|||
create_project_conditions(project_id, to_be_created)
|
||||
|
||||
if to_be_updated:
|
||||
logger.debug(to_be_updated)
|
||||
update_project_condition(project_id, to_be_updated)
|
||||
|
||||
return get_conditions(project_id)
|
||||
|
|
@ -428,3 +427,45 @@ def get_projects_ids(tenant_id):
|
|||
cur.execute(query=query)
|
||||
rows = cur.fetchall()
|
||||
return [r["project_id"] for r in rows]
|
||||
|
||||
|
||||
def delete_metadata_condition(project_id, metadata_key):
|
||||
sql = """\
|
||||
UPDATE public.projects_conditions
|
||||
SET filters=(SELECT COALESCE(jsonb_agg(elem), '[]'::jsonb)
|
||||
FROM jsonb_array_elements(filters) AS elem
|
||||
WHERE NOT (elem ->> 'type' = 'metadata'
|
||||
AND elem ->> 'source' = %(metadata_key)s))
|
||||
WHERE project_id = %(project_id)s
|
||||
AND jsonb_typeof(filters) = 'array'
|
||||
AND EXISTS (SELECT 1
|
||||
FROM jsonb_array_elements(filters) AS elem
|
||||
WHERE elem ->> 'type' = 'metadata'
|
||||
AND elem ->> 'source' = %(metadata_key)s);"""
|
||||
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(sql, {"project_id": project_id, "metadata_key": metadata_key})
|
||||
cur.execute(query)
|
||||
|
||||
|
||||
def rename_metadata_condition(project_id, old_metadata_key, new_metadata_key):
|
||||
sql = """\
|
||||
UPDATE public.projects_conditions
|
||||
SET filters = (SELECT jsonb_agg(CASE
|
||||
WHEN elem ->> 'type' = 'metadata' AND elem ->> 'source' = %(old_metadata_key)s
|
||||
THEN elem || ('{"source": "'||%(new_metadata_key)s||'"}')::jsonb
|
||||
ELSE elem END)
|
||||
FROM jsonb_array_elements(filters) AS elem)
|
||||
WHERE project_id = %(project_id)s
|
||||
AND jsonb_typeof(filters) = 'array'
|
||||
AND EXISTS (SELECT 1
|
||||
FROM jsonb_array_elements(filters) AS elem
|
||||
WHERE elem ->> 'type' = 'metadata'
|
||||
AND elem ->> 'source' = %(old_metadata_key)s);"""
|
||||
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(sql, {"project_id": project_id, "old_metadata_key": old_metadata_key,
|
||||
"new_metadata_key": new_metadata_key})
|
||||
cur.execute(query)
|
||||
|
||||
# TODO: make project conditions use metadata-column-name instead of metadata-key
|
||||
|
|
|
|||
|
|
@ -427,7 +427,6 @@ def update_project_conditions(project_id, conditions):
|
|||
create_project_conditions(project_id, to_be_created)
|
||||
|
||||
if to_be_updated:
|
||||
print(to_be_updated)
|
||||
update_project_condition(project_id, to_be_updated)
|
||||
|
||||
return get_conditions(project_id)
|
||||
|
|
@ -486,3 +485,45 @@ def is_authorized_batch(project_ids, tenant_id):
|
|||
cur.execute(query=query)
|
||||
rows = cur.fetchall()
|
||||
return [r["project_id"] for r in rows]
|
||||
|
||||
|
||||
def delete_metadata_condition(project_id, metadata_key):
|
||||
sql = """\
|
||||
UPDATE public.projects_conditions
|
||||
SET filters=(SELECT COALESCE(jsonb_agg(elem), '[]'::jsonb)
|
||||
FROM jsonb_array_elements(filters) AS elem
|
||||
WHERE NOT (elem ->> 'type' = 'metadata'
|
||||
AND elem ->> 'source' = %(metadata_key)s))
|
||||
WHERE project_id = %(project_id)s
|
||||
AND jsonb_typeof(filters) = 'array'
|
||||
AND EXISTS (SELECT 1
|
||||
FROM jsonb_array_elements(filters) AS elem
|
||||
WHERE elem ->> 'type' = 'metadata'
|
||||
AND elem ->> 'source' = %(metadata_key)s);"""
|
||||
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(sql, {"project_id": project_id, "metadata_key": metadata_key})
|
||||
cur.execute(query)
|
||||
|
||||
|
||||
def rename_metadata_condition(project_id, old_metadata_key, new_metadata_key):
|
||||
sql = """\
|
||||
UPDATE public.projects_conditions
|
||||
SET filters = (SELECT jsonb_agg(CASE
|
||||
WHEN elem ->> 'type' = 'metadata' AND elem ->> 'source' = %(old_metadata_key)s
|
||||
THEN elem || ('{"source": "'||%(new_metadata_key)s||'"}')::jsonb
|
||||
ELSE elem END)
|
||||
FROM jsonb_array_elements(filters) AS elem)
|
||||
WHERE project_id = %(project_id)s
|
||||
AND jsonb_typeof(filters) = 'array'
|
||||
AND EXISTS (SELECT 1
|
||||
FROM jsonb_array_elements(filters) AS elem
|
||||
WHERE elem ->> 'type' = 'metadata'
|
||||
AND elem ->> 'source' = %(old_metadata_key)s);"""
|
||||
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(sql, {"project_id": project_id, "old_metadata_key": old_metadata_key,
|
||||
"new_metadata_key": new_metadata_key})
|
||||
cur.execute(query)
|
||||
|
||||
# TODO: make project conditions use metadata-column-name instead of metadata-key
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue