Skip to content

Commit

Permalink
Use COALELSCE with fallback for Null
Browse files Browse the repository at this point in the history
  • Loading branch information
harminius committed Nov 21, 2024
1 parent 2d850d4 commit f01790f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions server/mergin/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ def workspace(self):
return project_workspace

def cache_latest_files(self) -> None:
"""Get project files from changes (FileHistory) and saved them for later use"""
"""Get project files from changes (FileHistory) and save them for later use."""
if self.latest_version is None:
return

query = f"""
WITH latest_changes AS (
SELECT
fp.id,
fp.project_id,
pv.project_id,
max(pv.name) AS version
FROM
project_version pv
Expand All @@ -118,14 +118,13 @@ def cache_latest_files(self) -> None:
pv.project_id = :project_id
AND pv.name <= :latest_version
GROUP BY
fp.id, fp.project_id
fp.id, pv.project_id
), aggregates AS (
SELECT
project_id,
array_agg(fh.id) AS files_ids
COALESCE(array_agg(fh.id) FILTER (WHERE fh.change != 'delete'), ARRAY[]::INTEGER[]) AS files_ids
FROM latest_changes ch
LEFT OUTER JOIN file_history fh ON (fh.file_path_id = ch.id AND fh.project_version_name = ch.version)
WHERE fh.change != 'delete'
GROUP BY project_id
)
UPDATE latest_project_files pf
Expand Down Expand Up @@ -743,7 +742,7 @@ def __init__(
fh.path: fh.id
for fh in FileHistory.query.filter(
FileHistory.id.in_(
self.project.latest_project_files.file_history_ids or {}
self.project.latest_project_files.file_history_ids
)
).all()
}
Expand Down

0 comments on commit f01790f

Please sign in to comment.