Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database Version Always 0 #49

Open
slice-happysingh opened this issue Oct 2, 2024 · 3 comments
Open

Database Version Always 0 #49

slice-happysingh opened this issue Oct 2, 2024 · 3 comments

Comments

@slice-happysingh
Copy link

slice-happysingh commented Oct 2, 2024

Issue: Database Version Always 0

Description:
I am encountering an issue where the database version is always returned as 0. This results in the database being recreated every time the app is reopened, leading to data loss.

The getVersion() function is used as shown below:

public int getVersion() {
    return ((Long) DatabaseUtils.longForQuery(this, "PRAGMA user_version;", null)).intValue();
}


final int version = db.getVersion();
if (version != mNewVersion) {
    if (db.isReadOnly()) {
        throw new SQLiteException("Can't upgrade read-only database from version " +
                db.getVersion() + " to " + mNewVersion + ": " + mName);
    }

    if (version > 0 && version < mMinimumSupportedVersion) {
        File databaseFile = new File(db.getPath());
        onBeforeDelete(db);
        db.close();
        if (SQLiteDatabase.deleteDatabase(databaseFile)) {
            mIsInitializing = false;
            return getDatabaseLocked(writable);
        } else {
            throw new IllegalStateException("Unable to delete obsolete database "
                    + mName + " with version " + version);
        }
    } else {
        db.beginTransaction();
        try {
            if (version == 0) {
                onCreate(db);
            } else {
                if (version > mNewVersion) {
                    onDowngrade(db, version, mNewVersion);
                } else {
                    onUpgrade(db, version, mNewVersion);
                }
            }
            db.setVersion(mNewVersion);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}
@slice-happysingh
Copy link
Author

Here is Database file

@Database(
    entities = [SyncEntity::class, BatchEntity::class, SMSEntity::class],
    version = 1,
    exportSchema = false
)
@TypeConverters(DataSyncSMSConverters::class, DataSyncSMSMetaDataConverters::class)
abstract class DataSyncDatabase : RoomDatabase() {

    companion object {

        @Volatile
        private var INSTANCE: DataSyncDatabase? = null

        fun getInstance(context: Context): DataSyncDatabase =
            INSTANCE
                ?: synchronized(this) { INSTANCE ?: buildDatabase(context).also { INSTANCE = it } }

        private fun buildDatabase(context: Context): DataSyncDatabase {
            System.loadLibrary("sqlcipher")
            val password = "Password1!"
            val databaseFile = context.getDatabasePath(DATABASE_NAME)
            val factory = SupportOpenHelperFactory(
                password.toByteArray(StandardCharsets.UTF_8),
                null,
                true,
                1
            )
            return databaseBuilder(context, DataSyncDatabase::class.java, databaseFile.absolutePath)
                .openHelperFactory(factory).build()
        }
    }

    abstract fun dataSyncSmsDao(): DataSyncSMSDao
    
    
    

@slice-happysingh
Copy link
Author

slice-happysingh commented Oct 4, 2024

Update: Issue Resolved

The problem was caused by the database filename. I was initially using database.db as the filename. Changing it to database (without the .db extension) fixed the issue. Now, the database version is correctly returned, and data is no longer lost when the app is reopened.

Suggestion:

It would be helpful if the repository could handle this scenario or provide a warning when certain filename extensions cause issues with version tracking. This would prevent similar issues for other users.

@developernotes

@developernotes
Copy link
Member

Hi @slice-happysingh,

I am happy to hear that you were able to resolve the issue. Have you confirmed this behavior without SQLCipher for Android, but with the Room API? SQLCipher for Android does not prevent the PRAGMA user_version from being set when the file contains an extension in the name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants