rafi0101 / Android-Room-Database-Backup

Simple tool to backup and restore your room database in Android
MIT License
239 stars 19 forks source link

Database Closes Unexpectedly When Restoring Incorrect Backup Files (Encrypted/Non-Encrypted) #37

Open abhishekabhi789 opened 2 months ago

abhishekabhi789 commented 2 months ago

Describe the bug

When the restore action fails due to choosing a non-encrypted file while roomBackup is expecting an encrypted file (backupIsEncrypted(true)) (exitCode 7) or due to choosing an encrypted file while roomBackup is expecting a non-encrypted file (backupIsEncrypted(false)) (exitCode 9), the database might close, leading to a crash during subsequent operations such as deleting an entry from the list.

  SQLiteLog                de....abasebackup.sample  E  (1) no such table: room_table_modification_log in "SELECT * FROM room_table_modification_log WHERE invalidated = 1;"
  ROOM                     de....abasebackup.sample  E  Cannot run invalidation tracker. Is the db closed?
                                                                                       android.database.sqlite.SQLiteException: no such table: room_table_modification_log (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM room_table_modification_log WHERE invalidated = 1;

To Reproduce

Steps to reproduce the behavior:

  1. Take an encrypted backup in the demo app.
  2. Disable the Encrypt Backup from Properties.
  3. Restore the encrypted backup file.
  4. It will fail with exitCode: 9.
  5. Now delete an entry from the Fruits List and observe the log entries for crash details.

The other scenario (exitCode 7) also causes a crash.

Expected behavior

When an operation fails in this manner, the database should be handled the same way as after exitCode 0 (success), i.e., no crash.

Screenshots

Not applicable, issue is 100% reproducible.

Smartphone (please complete the following information):

Tested on AVD API 33 and on Huawei Y5 API 23, the same behavior was observed.

Additional context

This issue is very likely to happen if an app offers both encrypted and non-encrypted backup options. If a user inadvertently chooses the wrong file, it may lead to a crash afterward.

rafi0101 commented 2 days ago

Yes I can reproduce it. I will think about your referenced commit and a fix for this. Thank you.