Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions server/mergin/sync/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..app import db
from .models import Project, ProjectAccess, ProjectVersion
from .utils import split_project_path
from ..auth.models import User


def add_commands(app: Flask):
Expand All @@ -24,10 +25,21 @@ def project():
@project.command()
@click.argument("name")
@click.argument("namespace")
@click.argument("user")
def create(name, namespace, user): # pylint: disable=W0612
@click.argument("username")
def create(name, namespace, username): # pylint: disable=W0612
"""Create blank project"""
workspace = current_app.ws_handler.get_by_name(namespace)
if not workspace:
print("ERROR: Workspace not found")
return
user = User.query.filter_by(username=username).first()
if not user:
print("ERROR: User not found")
return
p = Project.query.filter_by(name=name, workspace_id=workspace.id).first()
if p:
print("ERROR: Project name already exists")
return
project_params = dict(
creator=user,
name=name,
Expand Down
9 changes: 4 additions & 5 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
2 changes: 1 addition & 1 deletion server/mergin/tests/test_db_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_remove_project(client, diff_project):
LatestProjectFiles.query.filter_by(project_id=project_id)
.first()
.file_history_ids
is None
== []
)

# try to remove the deleted project
Expand Down
18 changes: 18 additions & 0 deletions server/mergin/tests/test_project_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,24 @@ def test_delete_diff_file(client):
assert fh.path == "base.gpkg" and fh.diff is None


def test_cache_files_ids(client):
"""Test caching latest project files when it is None"""
user = User.query.filter_by(username="mergin").first()
test_workspace = create_workspace()
project = create_project("no_file_history", test_workspace, user)
db.session.commit()
assert project.latest_project_files.file_history_ids is not None
project.latest_project_files.file_history_ids = None
db.session.commit()
assert project.latest_project_files.file_history_ids is None
# uploading to project caches
filename = "test.txt"
upload_file_to_project(project, filename, client)
fp = ProjectFilePath.query.filter_by(project_id=project.id, path=filename).first()
fh = FileHistory.query.filter_by(file_path_id=fp.id).first()
assert project.latest_project_files.file_history_ids == [fh.id]


def test_signals(client):
workspace = create_workspace()
user = User.query.filter(User.username == "mergin").first()
Expand Down