Db: Add wiping of pin state for subtrees
authorChristian Kamm <mail@ckamm.de>
Fri, 21 Dec 2018 09:35:07 +0000 (10:35 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:30 +0000 (10:58 +0100)
src/common/syncjournaldb.cpp
src/common/syncjournaldb.h
test/testsyncjournaldb.cpp

index f008175f9ff9d065a04df593125d48f9dfde75c6..d112c64e7d543ac18e091a27a0be52c33abb661b 100644 (file)
@@ -2136,6 +2136,20 @@ void SyncJournalDb::setPinStateForPath(const QByteArray &path, PinState state)
     query.exec();
 }
 
+void SyncJournalDb::wipePinStateForPathAndBelow(const QByteArray &path)
+{
+    QMutexLocker lock(&_mutex);
+    if (!checkConnect())
+        return;
+
+    auto &query = _wipePinStateQuery;
+    ASSERT(query.initOrReset(QByteArrayLiteral(
+            "DELETE FROM flags WHERE " IS_PREFIX_PATH_OR_EQUAL("?1", "path") ";"),
+        _db));
+    query.bindValue(1, path);
+    query.exec();
+}
+
 void SyncJournalDb::commit(const QString &context, bool startTrans)
 {
     QMutexLocker lock(&_mutex);
index ca589b1d3d57fad4e5d3c95fe1b606bb30d6ba2b..525800a088c6b056deaa44e55940861859527028 100644 (file)
@@ -294,6 +294,13 @@ public:
      */
     void setPinStateForPath(const QByteArray &path, PinState state);
 
+    /**
+     * Wipes pin states for a path and below.
+     *
+     * Used when the user asks a subtree to have a particular pin state.
+     */
+    void wipePinStateForPathAndBelow(const QByteArray &path);
+
     /**
      * Only used for auto-test:
      * when positive, will decrease the counter for every database operation.
@@ -361,6 +368,7 @@ private:
     SqlQuery _getRawPinStateQuery;
     SqlQuery _getEffectivePinStateQuery;
     SqlQuery _setPinStateQuery;
+    SqlQuery _wipePinStateQuery;
 
     /* Storing etags to these folders, or their parent folders, is filtered out.
      *
index 03c7a747515ae2a3ec42134313f6130385613bf5..55eca73adec925fd64b75a0e5e12708d9252f6e6 100644 (file)
@@ -336,6 +336,14 @@ private slots:
             }
             return *state;
         };
+        auto getRaw = [&](const QByteArray &path) -> PinState {
+            auto state = _db.rawPinStateForPath(path);
+            if (!state) {
+                QTest::qFail("couldn't read pin state", __FILE__, __LINE__);
+                return PinState::Inherited;
+            }
+            return *state;
+        };
 
         // Make a thrice-nested setup
         make("local", PinState::AlwaysLocal);
@@ -394,6 +402,14 @@ private slots:
         QCOMPARE(get("online"), PinState::OnlineOnly);
         QCOMPARE(get("inherit"), PinState::AlwaysLocal);
         QCOMPARE(get("nonexistant"), PinState::AlwaysLocal);
+
+        // Wiping
+        QCOMPARE(getRaw("local/local"), PinState::AlwaysLocal);
+        _db.wipePinStateForPathAndBelow("local/local");
+        QCOMPARE(getRaw("local"), PinState::AlwaysLocal);
+        QCOMPARE(getRaw("local/local"), PinState::Inherited);
+        QCOMPARE(getRaw("local/local/local"), PinState::Inherited);
+        QCOMPARE(getRaw("local/local/online"), PinState::Inherited);
     }
 
 private: