Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce push_finished signal #331

Merged
merged 8 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 4 additions & 2 deletions server/mergin/sync/public_api_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
from .errors import StorageLimitHit
from ..utils import format_time_delta

push_triggered = signal("push_triggered")
push_finished = signal("push_finished")
# TODO: Move to database events to handle all commits to project versions
project_version_created = signal("project_version_created")


Expand Down Expand Up @@ -732,7 +733,6 @@ def project_push(namespace, project_name):
if not ws:
abort(404)

push_triggered.send(project)
# fixme use get_latest
pv = ProjectVersion.query.filter_by(
project_id=project.id, name=project.latest_version
Expand Down Expand Up @@ -874,6 +874,7 @@ def project_push(namespace, project_name):
f"Transaction id: {upload.id}. No upload."
)
project_version_created.send(pv)
push_finished.send(pv)
return jsonify(ProjectSchema().dump(project)), 200
except IntegrityError as err:
db.session.rollback()
Expand Down Expand Up @@ -1084,6 +1085,7 @@ def push_finish(transaction_id):
f"Push finished for project: {project.id}, project version: {v_next_version}, transaction id: {transaction_id}."
)
project_version_created.send(pv)
push_finished.send(pv)
except (psycopg2.Error, FileNotFoundError, DataSyncError, IntegrityError) as err:
db.session.rollback()
logging.exception(
Expand Down
28 changes: 22 additions & 6 deletions server/mergin/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import shutil
from typing import Tuple
import pysqlite3
import uuid
import math
Expand Down Expand Up @@ -213,10 +214,7 @@ def file_info(project_dir, path, chunk_size=1024):
}


def upload_file_to_project(project, filename, client):
"""Add test file to project - start, upload and finish push process"""
file = os.path.join(test_project_dir, filename)
assert os.path.exists(file)
def mock_changes_data(project, filename) -> dict:
changes = {
"added": [file_info(test_project_dir, filename)],
"updated": [],
Expand All @@ -226,13 +224,29 @@ def upload_file_to_project(project, filename, client):
"version": ProjectVersion.to_v_name(project.latest_version),
"changes": changes,
}
return data


def push_file_start(project, filename, client, mocked_changes_data=None) -> Tuple:
MarcelGeo marked this conversation as resolved.
Show resolved Hide resolved
file = os.path.join(test_project_dir, filename)
assert os.path.exists(file)
data = mocked_changes_data or mock_changes_data(project, filename)
resp = client.post(
f"/v1/project/push/{project.workspace.name}/{project.name}",
data=json.dumps(data, cls=DateTimeEncoder).encode("utf-8"),
headers=json_headers,
)
return resp


def upload_file_to_project(project, filename, client):
"""Add test file to project - start, upload and finish push process"""
file = os.path.join(test_project_dir, filename)
data = mock_changes_data(project, filename)
changes = data.get("changes")
resp = push_file_start(project, filename, client, data)
print(resp.json)
MarcelGeo marked this conversation as resolved.
Show resolved Hide resolved
upload_id = resp.json["transaction"]
changes = data["changes"]
file_meta = changes["added"][0]
for chunk_id in file_meta["chunks"]:
url = f"/v1/project/push/chunk/{upload_id}/{chunk_id}"
Expand All @@ -241,7 +255,9 @@ def upload_file_to_project(project, filename, client):
client.post(
url, data=f_data, headers={"Content-Type": "application/octet-stream"}
)
assert client.post(f"/v1/project/push/finish/{upload_id}").status_code == 200
push_finish = client.post(f"/v1/project/push/finish/{upload_id}")
assert resp.status_code == 200
assert push_finish.status_code == 200
MarcelGeo marked this conversation as resolved.
Show resolved Hide resolved


def gpkgs_are_equal(file1, file2):
Expand Down
Loading