From 9d5357f7a6e51f2ecabe08f46dc6da0d09994311 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 4 Dec 2011 14:56:12 +0900 Subject: [PATCH] after a save, revert to unmodified state; sync tweaks --- anki/collection.py | 11 +++++++++++ anki/decks.py | 1 + anki/models.py | 1 + anki/sync.py | 5 +++++ anki/tags.py | 1 + tests/test_sync.py | 7 +++++++ 6 files changed, 26 insertions(+) diff --git a/anki/collection.py b/anki/collection.py index d2eb2ea21..c6cc6d12f 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -88,6 +88,13 @@ conf, models, decks, dconf, tags from col""") self.decks.load(decks, dconf) self.tags.load(tags) + def setMod(self): + """Mark DB modified. + +DB operations and the deck/tag/model managers do this automatically, so this +is only necessary if you modify properties of this object or the conf dict.""" + self.db.mod = True + def flush(self, mod=None): "Flush state to DB, updating mod time." self.mod = intTime(1000) if mod is None else mod @@ -108,6 +115,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", self.flush(mod=mod) self.db.commit() self.lock() + self.db.mod = False self._markOp(name) self._lastSave = time.time() @@ -117,7 +125,10 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", self.save() def lock(self): + # make sure we don't accidentally bump mod time + mod = self.db.mod self.db.execute("update col set mod=mod") + self.db.mod = mod def close(self, save=True): "Disconnect from DB." diff --git a/anki/decks.py b/anki/decks.py index 1cb727760..cf7a3aaca 100644 --- a/anki/decks.py +++ b/anki/decks.py @@ -104,6 +104,7 @@ class DeckManager(object): self.col.db.execute("update col set decks=?, dconf=?", simplejson.dumps(self.decks), simplejson.dumps(self.dconf)) + self.changed = False # Deck save/load ############################################################# diff --git a/anki/models.py b/anki/models.py index d37ae3ffa..c94b13f40 100644 --- a/anki/models.py +++ b/anki/models.py @@ -80,6 +80,7 @@ class ModelManager(object): if self.changed: self.col.db.execute("update col set models = ?", simplejson.dumps(self.models)) + self.changed = False # Retrieving and creating models ############################################################# diff --git a/anki/sync.py b/anki/sync.py index a742e14e4..b33f966db 100644 --- a/anki/sync.py +++ b/anki/sync.py @@ -37,6 +37,9 @@ class Syncer(object): def sync(self): "Returns 'noChanges', 'fullSync', or 'success'." + # if the deck has any pending changes, flush them first and bump mod + # time + self.col.save() # step 1: login & metadata runHook("sync", "login") ret = self.server.meta() @@ -135,7 +138,9 @@ select count() from notes where id not in (select distinct nid from cards)""") assert usn != -1 for m in self.col.models.all(): assert m['usn'] != -1 + self.col.sched.reset() return [ + self.col.sched.repCounts(), self.col.db.scalar("select count() from cards"), self.col.db.scalar("select count() from notes"), self.col.db.scalar("select count() from revlog"), diff --git a/anki/tags.py b/anki/tags.py index 8ad9b6d3e..148079f4c 100644 --- a/anki/tags.py +++ b/anki/tags.py @@ -29,6 +29,7 @@ class TagManager(object): if self.changed: self.col.db.execute("update col set tags=?", simplejson.dumps(self.tags)) + self.changed = False # Registering and fetching tags ############################################################# diff --git a/tests/test_sync.py b/tests/test_sync.py index 761694ec9..5907341d9 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -47,6 +47,7 @@ def setup_basic(): def setup_modified(): setup_basic() # mark deck1 as changed + deck1.setMod() deck1.save() @nose.with_setup(setup_basic) @@ -56,6 +57,7 @@ def test_nochange(): @nose.with_setup(setup_modified) def test_changedSchema(): deck1.scm += 1 + deck1.setMod() assert client.sync() == "fullSync" @nose.with_setup(setup_modified) @@ -84,6 +86,7 @@ def test_sync(): assert client.sync() == "noChanges" # if we bump mod time, everything is copied across again because of the # 600 second sync leeway. but the decks should remain the same. + deck1.setMod() deck1.save() assert client.sync() == "success" check(2) @@ -168,6 +171,7 @@ def test_tags(): deck2.tags.register(["xyz"]) assert deck1.tags.all() != deck2.tags.all() deck1.save() + time.sleep(0.1) deck2.save() assert client.sync() == "success" assert deck1.tags.all() == deck2.tags.all() @@ -182,6 +186,7 @@ def test_decks(): time.sleep(0.1) deck2.decks.id("new2") deck1.save() + time.sleep(0.1) deck2.save() assert client.sync() == "success" assert deck1.tags.all() == deck2.tags.all() @@ -199,6 +204,7 @@ def test_conf(): test_sync() assert deck2.conf['topDeck'] == 1 deck1.conf['topDeck'] = 2 + deck1.setMod() deck1.save() assert client.sync() == "success" assert deck2.conf['topDeck'] == 2 @@ -221,6 +227,7 @@ def test_threeway(): deck1.save() # at time 2, client 2 syncs to server time.sleep(1) + deck3.setMod() deck3.save() assert client2.sync() == "success" # at time 3, client 1 syncs, adding the older note