Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fix loss of changes when several connections sync different GeoPackages of the same Mergin Maps project: each connection now uses its own working directory (`<working_dir>/<project>/<modified schema>`) instead of one shared checkout per project (#163). Existing installations re-download the project checkout on the next start; the database schemas are not affected.

## 2.3.0

- Add `include_tables` connection option to sync only the listed tables (mutually exclusive with `skip_tables`)
Expand Down
35 changes: 19 additions & 16 deletions dbsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,21 @@ def revert_local_changes(
return leftovers


def _get_work_dir(conn_cfg):
"""Return the local working directory (project checkout) for a connection.

Each connection gets its own checkout of the Mergin Maps project, keyed on the
name of the 'modified' schema (unique per connection). Previously all connections
to the same project shared one checkout: the first connection to see a new server
version pulled it - updating *every* GeoPackage in the checkout - but only computed
and applied the changeset for its own sync file. The remaining connections then saw
local_version == server_version and did nothing, so their changes never reached
the database and no error was logged (see issue #163).
"""
project_name = conn_cfg.mergin_project.split("/")[1]
return os.path.join(config.working_dir, project_name, conn_cfg.modified)


def pull(conn_cfg, mc):
"""Downloads any changes from Mergin Maps and applies them to the database"""

Expand All @@ -678,10 +693,7 @@ def pull(conn_cfg, mc):
include_tables = get_include_tables(conn_cfg)

project_name = conn_cfg.mergin_project.split("/")[1]
work_dir = os.path.join(
config.working_dir,
project_name,
)
work_dir = _get_work_dir(conn_cfg)
Comment on lines -681 to +696

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the addition itself seems fine, I think that the previous definition of work_dir needs to be removed, to keep the code clean and readable. It applies in all changes in this file.

gpkg_full_path = os.path.join(
work_dir,
conn_cfg.sync_file,
Expand Down Expand Up @@ -839,10 +851,7 @@ def status(conn_cfg, mc):

project_name = conn_cfg.mergin_project.split("/")[1]

work_dir = os.path.join(
config.working_dir,
project_name,
)
work_dir = _get_work_dir(conn_cfg)
gpkg_full_path = os.path.join(
work_dir,
conn_cfg.sync_file,
Expand Down Expand Up @@ -951,10 +960,7 @@ def push(conn_cfg, mc):
if os.path.exists(tmp_changeset_file):
os.remove(tmp_changeset_file)

work_dir = os.path.join(
config.working_dir,
project_name,
)
work_dir = _get_work_dir(conn_cfg)
gpkg_full_path = os.path.join(
work_dir,
conn_cfg.sync_file,
Expand Down Expand Up @@ -1081,10 +1087,7 @@ def init(
conn_cfg.modified,
)

work_dir = os.path.join(
config.working_dir,
project_name,
)
work_dir = _get_work_dir(conn_cfg)
gpkg_full_path = os.path.join(
work_dir,
conn_cfg.sync_file,
Expand Down
4 changes: 4 additions & 0 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ A single GeoPackage file in a Mergin Maps project is treated as an equivalent of
GeoPackage and a database schema can contain multiple tables with data - and the DB Sync tool keeps
content of the tables in the database and in the GeoPackage the same.

A project with several GeoPackage files needs one connection per file (see the `connections` list in the
configuration). Each connection keeps its own local checkout of the project in
`<working_dir>/<project name>/<modified schema>`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed your changes — good job.

We're suggesting a different convention for the folder name. For backward compatibility with previous versions of db-sync, we're suggesting using - between the project name and the modified schema:

<working-dir>/<project name>-<modified schema>


There are two ways how the synchronization can be started:
1. Init from GeoPackage: if you have a Mergin Maps project with an existing GeoPackage, the tool will
create the destination schema and tables in the database (and populate those with data from GeoPackage).
Expand Down