Skip to content

Commit

Permalink
feat: override an existing system if a system with the same name is u… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparkier authored Sep 29, 2023
1 parent 13953c9 commit 537c84e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
30 changes: 30 additions & 0 deletions backend/zeno_backend/database/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,33 @@ def report_org(report_id: int, organization: Organization):
"AND report_id = %s;",
[organization.id, report_id],
)


def system(project_uuid: str, system_name: str):
"""Delete a system from a project.
Args:
project_uuid (str): id of the project to delete a system from.
system_name (str): name of the system to be deleted.
"""
with Database() as db:
columns = db.execute_return(
sql.SQL("SELECT column_id FROM {} WHERE model = %s;").format(
sql.Identifier(f"{project_uuid}_column_map")
),
[system_name],
)
for column in columns:
db.execute(
sql.SQL("ALTER TABLE {} DROP COLUMN {};").format(
sql.Identifier(project_uuid),
sql.Identifier(column[0]),
)
)
db.execute(
sql.SQL("DELETE FROM {} WHERE column_id = %s;").format(
sql.Identifier(f"{project_uuid}_column_map")
),
[column[0]],
)
db.commit()
28 changes: 28 additions & 0 deletions backend/zeno_backend/database/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,3 +1602,31 @@ def filtered_short_string_column_values(
short_ret.append(loc_str)

return short_ret


def system_exists(project_uuid: str, system_name: str) -> bool:
"""Check whether a system exists for a project.
Args:
project_uuid (str): ID of the project.
system_name (str): name of the system.
Returns:
bool: whether the system exists.
Raises:
Exception: something went wrong while checking whether the system exists.
"""
db = Database()
exists = db.connect_execute_return(
sql.SQL("SELECT EXISTS(SELECT 1 FROM {} " "WHERE model = %s);").format(
sql.Identifier(f"{project_uuid}_column_map")
),
[system_name],
)
if len(exists) > 0:
return bool(exists[0][0])
else:
raise Exception("Error while checking whether system exists.")
6 changes: 5 additions & 1 deletion backend/zeno_backend/routers/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from zeno_backend.classes.amplitude import AmplitudeHandler
from zeno_backend.classes.project import Project
from zeno_backend.database import insert, select, update
from zeno_backend.database import delete, insert, select, update

# MUST reflect views in frontend/src/lib/components/instance-views/views/viewMap.ts
VIEWS = [
Expand Down Expand Up @@ -223,6 +223,10 @@ def upload_system(
detail=("ERROR: Unable to read system data: " + str(e)),
) from e

# If a system with the same name already exists for the project, delete it.
if select.system_exists(project, system_name):
delete.system(project, system_name)

try:
insert.system(project, system_df, system_name, output_column, id_column)
except Exception as e:
Expand Down

0 comments on commit 537c84e

Please sign in to comment.