Library sync OsmAnd-style: brick is the server, a client (the car's maptool, SYNC_SERVER_URL) syncs on its Sync button — pulls the server's rows, pushes its own with the source files the server lacks, newer updated_at wins; /api/sync/* guarded by SYNC_TOKEN (written into brick's .env by CI). TRUSTED_NETWORKS logs in the car's LAN (no Gitea offline); TAKEOUT_HOST_DIR for a server without the Takeout; bin/pull-maps.sh pulls map data one way while at home
CI / check (push) Successful in 22s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-27 23:22:32 +01:00
1 parent 850a5946ad
commit dbe742321a
13 files changed
+460 -5

No files matched your search

+81 -1
View File
@@ -209,7 +209,7 @@ module Maptool
type, source = connect { |db| db.get_first_row("SELECT type, source_path FROM items WHERE id = ? AND deleted_at IS NULL", [id]) }
return nil unless source
type == "layer" ? @gpx_store.resolve_file(source) : File.join(@sources_dir, source)
source_file(type, source)
end
# --- writes -------------------------------------------------------------
@@ -457,6 +457,86 @@ module Maptool
raise InvalidValue
end
# --- server ⇄ server sync (home ⇄ car) -----------------------------------
SYNC_FOLDER_COLUMNS = %w[id parent_id name created_at updated_at deleted_at].freeze
SYNC_ITEM_COLUMNS = %w[id folder_id parent_id type name geometry properties fingerprint source_path created_at updated_at deleted_at].freeze
# Whole rows written after `since`, tombstones included, at most `limit`
# of each table; "next" is the rev to ask from next time (below "rev"
# while there's more).
def sync_changes(since, limit: 500)
since = Integer(since)
connect do |db|
db.transaction do
rev = db.get_first_value("SELECT rev FROM meta")
tables = { "folders" => SYNC_FOLDER_COLUMNS, "items" => SYNC_ITEM_COLUMNS }.to_h do |table, columns|
rows = db.execute("SELECT #{columns.join(', ')}, rev FROM #{table} WHERE rev > ? ORDER BY rev LIMIT ?", [since, limit])
[table, rows.map { |row| (columns + ["rev"]).zip(row).to_h }]
end
full = tables.values.select { |rows| rows.size == limit }.map { |rows| rows.last["rev"] - 1 }
tables.merge("rev" => rev, "next" => [rev, *full].min)
end
end
end
# Takes the other server's rows (sync_changes): one newer than ours —
# or not here yet — replaces ours, else ours stays (last writer wins by
# updated_at; an echo of our own change ties and is skipped). Returns
# the source files (items' source_path) not here yet.
# ponytail: the losing version isn't kept; a history table if a lost
# edit ever matters.
def apply_sync(changes)
missing = []
write do |db, rev, _now|
{ "folders" => SYNC_FOLDER_COLUMNS, "items" => SYNC_ITEM_COLUMNS }.each do |table, columns|
(changes[table] || []).each do |row|
ours = db.get_first_value("SELECT updated_at FROM #{table} WHERE id = ?", [row["id"]])
next if ours && ours >= row["updated_at"].to_s
db.execute("INSERT OR REPLACE INTO #{table} (#{columns.join(', ')}, rev) VALUES (#{(['?'] * (columns.size + 1)).join(', ')})",
[*row.values_at(*columns), rev])
missing << row if table == "items" && row["source_path"] && !row["deleted_at"] && !File.exist?(source_file(row["type"], row["source_path"]))
end
end
end
missing
end
# Where an item's source file lives: a legacy layer's in GpxStore,
# an upload's under sources/.
# The path comes from the other server too: never outside sources/.
def source_file(type, source_path)
return @gpx_store.resolve_file(source_path) if type == "layer"
root = File.expand_path(@sources_dir)
path = File.expand_path(File.join(root, source_path.to_s))
raise InvalidValue unless path.start_with?("#{root}/")
path
end
# An item's source file (tombstoned or not: the other server may still
# need it), nil without one.
def item_source(id)
type, source = connect { |db| db.get_first_row("SELECT type, source_path FROM items WHERE id = ?", [id]) }
source && source_file(type, source)
end
# An item's type and source_path (where its file belongs), nil without one.
def source_row(id)
type, source = connect { |db| db.get_first_row("SELECT type, source_path FROM items WHERE id = ?", [id]) }
source && { "type" => type, "source_path" => source }
end
# The other server's copy of a source file, written into place.
def store_source(item, bytes)
path = source_file(item["type"], item["source_path"])
FileUtils.mkdir_p(File.dirname(path))
File.binwrite("#{path}.part", bytes)
File.rename("#{path}.part", path)
end
# A folder's own items (not its subfolders') as one GPX with points and
# tracks together — what Locus imports into one folder.
def export_gpx(path)