1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-03-13 12:10:08 +01:00

test the imported database if its valid otherwise restore the old database, see #1037

This commit is contained in:
oliexdev 2025-03-03 12:58:56 +01:00
parent 3e36fae2de
commit c746ef8dec

View File

@ -470,11 +470,17 @@ public class OpenScale {
reopenDatabase(false); reopenDatabase(false);
getScaleUserList().get(0); // call it to test if the imported database works otherwise a runtime exception is thrown
if (!getScaleUserList().isEmpty()) { if (!getScaleUserList().isEmpty()) {
selectScaleUser(getScaleUserList().get(0).getId()); selectScaleUser(getScaleUserList().get(0).getId());
} }
} catch (SQLiteDatabaseCorruptException e) { } catch (RuntimeException e) {
copyFile(Uri.fromFile(tmpExportFile), Uri.fromFile(exportFile)); Timber.d("import database corrupted, restore old database");
File restoreExportFile = context.getApplicationContext().getDatabasePath("openScale_restore.db");
copyFile(Uri.fromFile(tmpExportFile), Uri.fromFile(restoreExportFile));
importDatabase(Uri.fromFile(restoreExportFile));
restoreExportFile.delete();
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} finally { } finally {
tmpExportFile.delete(); tmpExportFile.delete();