diff --git a/src/main/frontend/worker/sync/download.cljs b/src/main/frontend/worker/sync/download.cljs index 59063dc4d8..54b399a99d 100644 --- a/src/main/frontend/worker/sync/download.cljs +++ b/src/main/frontend/worker/sync/download.cljs @@ -379,6 +379,7 @@ [repo reset? graph-id graph-e2ee? & [total-datoms]] (let [graph-e2ee? (if (nil? graph-e2ee?) true (true? graph-e2ee?))] (-> (p/let [close-db-f (require-thread-api-f! :thread-api/db-sync-close-db) + unlink-db-f (require-thread-api-f! :thread-api/unsafe-unlink-db) invalidate-search-db-f (require-thread-api-f! :thread-api/db-sync-invalidate-search-db) create-or-open-db-f (require-thread-api-f! :thread-api/create-or-open-db) _ (when-let [state @*import-state] @@ -386,6 +387,7 @@ (close-db-f (:repo state))) _ (reset! *import-state nil) _ (when reset? (close-db-f repo)) + _ (when reset? (unlink-db-f repo)) _ (when reset? (invalidate-search-db-f repo)) import-id (str (random-uuid)) aes-key (when graph-e2ee? diff --git a/src/test/frontend/worker/db_worker_test.cljs b/src/test/frontend/worker/db_worker_test.cljs index 24ff9bae6f..169bb92bd7 100644 --- a/src/test/frontend/worker/db_worker_test.cljs +++ b/src/test/frontend/worker/db_worker_test.cljs @@ -159,21 +159,28 @@ (p/resolved {:error error})))) (defn- with-fake-create-or-open-db - [repo conn f] - (let [thread-apis-prev @thread-api/*thread-apis] - (vreset! thread-api/*thread-apis - (assoc thread-apis-prev - :thread-api/create-or-open-db - (fn [_repo _opts] - (swap! worker-state/*datascript-conns assoc repo conn) - (p/resolved nil)) - :thread-api/db-sync-close-db - (fn [_repo] nil) - :thread-api/db-sync-invalidate-search-db - (fn [_repo] (p/resolved nil)))) - (-> (f) - (p/finally (fn [] - (vreset! thread-api/*thread-apis thread-apis-prev)))))) + ([repo conn f] + (with-fake-create-or-open-db repo conn {} f)) + ([repo conn + {:keys [create-or-open-db-f close-db-f invalidate-search-db-f unlink-db-f]} + f] + (let [thread-apis-prev @thread-api/*thread-apis + create-or-open-db-f (or create-or-open-db-f + (fn [_repo _opts] + (swap! worker-state/*datascript-conns assoc repo conn) + (p/resolved nil))) + close-db-f (or close-db-f (fn [_repo] nil)) + invalidate-search-db-f (or invalidate-search-db-f (fn [_repo] (p/resolved nil))) + unlink-db-f (or unlink-db-f (fn [_repo] nil))] + (vreset! thread-api/*thread-apis + (assoc thread-apis-prev + :thread-api/create-or-open-db create-or-open-db-f + :thread-api/db-sync-close-db close-db-f + :thread-api/db-sync-invalidate-search-db invalidate-search-db-f + :thread-api/unsafe-unlink-db unlink-db-f)) + (-> (f) + (p/finally (fn [] + (vreset! thread-api/*thread-apis thread-apis-prev))))))) (deftest db-sync-import-prepare-replaces-active-import-state-test (async done @@ -198,6 +205,48 @@ (is false (str error)) (done))))))))))) +(deftest db-sync-import-prepare-reset-unlinks-db-before-reopen-test + (async done + (restoring-worker-state + (fn [] + (let [prepare (@thread-api/*thread-apis :thread-api/db-sync-import-prepare) + conn (d/create-conn db-schema/schema) + calls (atom [])] + (with-fake-create-or-open-db + test-repo conn + {:close-db-f (fn [repo] + (swap! calls conj [:close repo]) + nil) + :unlink-db-f (fn [repo] + (swap! calls conj [:unlink repo]) + nil) + :invalidate-search-db-f (fn [repo] + (swap! calls conj [:invalidate-search repo]) + (p/resolved nil)) + :create-or-open-db-f (fn [repo opts] + (swap! calls conj [:create-or-open repo opts]) + (swap! worker-state/*datascript-conns assoc repo conn) + (p/resolved nil))} + (fn [] + (-> (prepare test-repo true "graph-1" false) + (p/then (fn [_] + (let [ops (mapv first @calls) + idx (fn [op] + (first (keep-indexed (fn [i v] + (when (= op v) i)) + ops)))] + (is (some? (idx :close))) + (is (some? (idx :unlink))) + (is (some? (idx :invalidate-search))) + (is (some? (idx :create-or-open))) + (is (< (idx :close) (idx :unlink))) + (is (< (idx :unlink) (idx :invalidate-search))) + (is (< (idx :invalidate-search) (idx :create-or-open)))) + (done))) + (p/catch (fn [error] + (is false (str error)) + (done))))))))))) + (deftest db-sync-import-finalize-rejects-stale-import-id-test (async done (restoring-worker-state