From 6faf3c25a1543083ff2358d96005332b776a3243 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 29 Jun 2022 10:05:42 +1000 Subject: [PATCH] Fix DB check in new backend https://github.com/ankidroid/Anki-Android/pull/11644#issuecomment-1169380381 Closes #11735 --- .../main/java/com/ichi2/anki/DatabaseCheck.kt | 46 +++++++++++++++++++ .../main/java/com/ichi2/anki/DeckPicker.kt | 6 ++- .../java/com/ichi2/libanki/CollectionV16.kt | 6 +++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 AnkiDroid/src/main/java/com/ichi2/anki/DatabaseCheck.kt diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/DatabaseCheck.kt b/AnkiDroid/src/main/java/com/ichi2/anki/DatabaseCheck.kt new file mode 100644 index 000000000000..b5b0ba31e022 --- /dev/null +++ b/AnkiDroid/src/main/java/com/ichi2/anki/DatabaseCheck.kt @@ -0,0 +1,46 @@ +/*************************************************************************************** + * Copyright (c) 2022 Ankitects Pty Ltd * + * * + * This program is free software; you can redistribute it and/or modify it under * + * the terms of the GNU General Public License as published by the Free Software * + * Foundation; either version 3 of the License, or (at your option) any later * + * version. * + * * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY * + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * + * PARTICULAR PURPOSE. See the GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License along with * + * this program. If not, see . * + ****************************************************************************************/ + +package com.ichi2.anki + +import timber.log.Timber + +fun DeckPicker.handleDatabaseCheck() { + val col = CollectionHelper.getInstance().getCol(baseContext).newBackend + val deckPicker = this + catchingLifecycleScope(this) { + val problems = runInBackgroundWithProgress(col, { + if (it.hasDatabaseCheck()) { + // TODO: show progress in GUI + it.databaseCheck.run { + if (stageTotal > 0) { + Timber.i("$stage: $stageCurrent/$stageTotal") + } else { + Timber.i("$stage") + } + } + } + }) { + col.fixIntegrity() + } + val message = if (problems.isNotEmpty()) { + problems.joinToString("\n") + } else { + col.tr.databaseCheckRebuilt() + } + deckPicker.showSimpleMessageDialog(message) + } +} diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt b/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt index 9280981e8312..9e01a919b6eb 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt @@ -1370,7 +1370,11 @@ open class DeckPicker : private fun performIntegrityCheck() { Timber.i("performIntegrityCheck()") - TaskManager.launchCollectionTask(CheckDatabase(), CheckDatabaseListener()) + if (BackendFactory.defaultLegacySchema) { + TaskManager.launchCollectionTask(CheckDatabase(), CheckDatabaseListener()) + } else { + handleDatabaseCheck() + } } private fun mediaCheckListener(): MediaCheckListener { diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/CollectionV16.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/CollectionV16.kt index dfb40f4e212c..0d73fef29275 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/CollectionV16.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/CollectionV16.kt @@ -130,4 +130,10 @@ class CollectionV16( fun i18nResourcesRaw(input: ByteArray): ByteArray { return backend.i18nResourcesRaw(input = input) } + + /** Fixes and optimizes the database. If any errors are encountered, a list of + * problems is returned. Throws if DB is unreadable. */ + fun fixIntegrity(): List { + return backend.checkDatabase() + } }