Sync: prepare tables a plugin creates after start-up
Synced tables get their sync columns and triggers at start-up. A table a plugin created later (on first use) had none, and every sync failed with "no such column: _dev". Each sync now prepares such tables first (syncdb.unprepared) and reads their file from the start once, since the batches carrying their rows were passed by. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6b657f6767
commit
ce8d9ffbb1
2 changed files with 27 additions and 0 deletions
|
|
@ -272,12 +272,29 @@ def pull(st):
|
||||||
return got
|
return got
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_new(st):
|
||||||
|
"""Tables made since start-up (a plugin creates some on first use) → stamped now, and their file read from
|
||||||
|
the start once ("behind") — the batches with their rows were passed by while they weren't there."""
|
||||||
|
for name, path, tables in specs():
|
||||||
|
if not os.path.exists(path):
|
||||||
|
continue
|
||||||
|
new = syncdb.unprepared(path, tables)
|
||||||
|
if not new:
|
||||||
|
continue
|
||||||
|
print(f"sync: {name}: preparing {', '.join(new)} (made since start-up)", flush=True)
|
||||||
|
syncdb.prepare(path, tables, st["device"])
|
||||||
|
rel = os.path.relpath(path, DATA)
|
||||||
|
if st.get("pulled") and rel not in (st.get("behind") or {}): # applying again changes nothing else
|
||||||
|
st["behind"] = dict(st.get("behind") or {}, **{rel: 0})
|
||||||
|
|
||||||
|
|
||||||
def sync_now():
|
def sync_now():
|
||||||
with _lock:
|
with _lock:
|
||||||
st = load_state()
|
st = load_state()
|
||||||
if not configured(st):
|
if not configured(st):
|
||||||
raise RuntimeError("sync isn't set up (python3 sync.py setup …)")
|
raise RuntimeError("sync isn't set up (python3 sync.py setup …)")
|
||||||
st["key_bytes"] = base64.b64decode(st["key"])
|
st["key_bytes"] = base64.b64decode(st["key"])
|
||||||
|
prepare_new(st)
|
||||||
res = {"pulled": pull(st), "pushed": push(st)}
|
res = {"pulled": pull(st), "pushed": push(st)}
|
||||||
status.update(last=time.time(), error=None, result=res)
|
status.update(last=time.time(), error=None, result=res)
|
||||||
return res
|
return res
|
||||||
|
|
|
||||||
|
|
@ -181,6 +181,16 @@ def changes(path, tables, name):
|
||||||
con.close()
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
|
def unprepared(path, tables):
|
||||||
|
"""The synced tables made since prepare() ran (a plugin creates some on first use): there, but without
|
||||||
|
the sync columns. Read only."""
|
||||||
|
con = connect(path)
|
||||||
|
try:
|
||||||
|
return [t for t in tables if (cs := _cols(con, t)) and "_uid" not in cs]
|
||||||
|
finally:
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
def is_empty(path, tables):
|
def is_empty(path, tables):
|
||||||
"""Nothing synced in this file yet (no rows, no delete markers)? Read only."""
|
"""Nothing synced in this file yet (no rows, no delete markers)? Read only."""
|
||||||
con = connect(path)
|
con = connect(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue