Reputation: 432
Whenever I want to open my Couchbase lite database on an empty data/com.my.app/files/databaseName.cblite2
folder i.e.:
DatabaseConfiguration config = new DatabaseConfiguration();
database = new Database(databaseName, config);
I get the following error:
CouchbaseLiteException{CouchbaseLite,21,'file/data is not in the requested format'}
How can I enforce that a new database is created instead? So in case loading succeeds I would like to get the old database and in case the data is corrupt (error 21) I would like to get a new Couchbase database.
Background:
In order to test some functionality of my App with instrumentation tests, I hava a test rule that moves the local Couchbase files db.sqlite3
, db.sqlite3-shm
and db.sqlite3-wal
to some backup location (@Before
) and restores the original database after the test (@After
).
Unfortunatly, whenever the database is empty, I get the above exception with code 21, which according to Couchbase Documentation means that the database exists but has the wrong format.
When I inspect the folder structure in Android Studio's 'Device File Explorer' I see that loading succeeds if the folder data/com.my.app/files/databaseName.cblite2
either contains a db.sqlite3
file or if it completely missing. However, if the folder is empty, the exception it thrown.
When I manually delete the folder by setting a breakpoint, the test works just fine. Is there I way to enforce the creation of the database?
Edit:
As manually deleting the folder prevents the error, my workaround is to delete the databaseName.cblite2
folder from the file system in case it is empty. While this is not the most elegant solution I found it acceptable for testing.
Upvotes: 1
Views: 159
Reputation: 6705
This is behaving as designed.
Couchbase considers the presence of the SQLite-created database directory as evidence that the database exists. Deleting the contents of the folder is not a reasonable thing to do. To delete the database you should, as you note in your edit, delete the directory and its contents.
Better yet, use db.delete()
Upvotes: 1