我有一个具有多个迁移的应用程序,其中一个是从使用Room之前的版本到引入Room时的版本。我发现它与RoomOpenHelper.onUpgrade
回调一起运行,但是当我使用SafeHelperFactory
时,它将创建一个SQLiteOpenHelper
的实例,该实例本身持有对RoomOpenHelper
的回调。我认为这正在迁移路径中造成冲突。奇怪的是,当我从已经具有Room(未加密)的应用程序版本迁移到具有SafeRoom的版本时,不会发生此问题,当从无Room迁移到Room(未加密)时,它也可以平稳运行。我得到的stacktrace如下(尽管不确定要在迁移上使用多少上下文,我不认为迁移本身就是问题,因为从无房间迁移到另一房间并且一直在为同时,当我尝试介绍SafeRoom时出现问题,请查看函数/方法调用的顺序。)
Process: com.xx.xx.debug, PID: 16780
net.sqlcipher.database.SQLiteException: error in view guest_summary: no such table: main.my_guests: ALTER TABLE my_guests_temp RENAME TO my_guests
at net.sqlcipher.database.SQLiteDatabase.native_execSQL(Native Method)
at net.sqlcipher.database.SQLiteDatabase.execSQL(SQLiteDatabase.java:2417)
at com.commonsware.cwac.saferoom.Database.execSQL(Database.java:371)
at MyDatabaseKt$MIGRATION_15_16$1.migrate(MyDatabase.kt:460)
at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:87)
at com.commonsware.cwac.saferoom.Helper$OpenHelper.onUpgrade(Helper.java:207)
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:182)
at com.commonsware.cwac.saferoom.Helper$OpenHelper.getWritableSupportDatabase(Helper.java:172)
at com.commonsware.cwac.saferoom.Helper.getWritableDatabase(Helper.java:82)
at androidx.room.RoomDatabase.query(RoomDatabase.java:256)
at androidx.room.util.DBUtil.query(DBUtil.java:54)
没有空间的版本是15。我使用Dagger,因此集成SafeRoom的方式如下。
/**
* Provide the single instance of our room database
*/
@Singleton
@Provides
fun provideDatabase(@Named("app") context: Context): MyDatabase {
val databaseState = SQLCipherUtils.getDatabaseState(context, "DatabaseName.db")
Log.d("ROOM", "database status: $databaseState")
if (databaseState == SQLCipherUtils.State.UNENCRYPTED) {
Log.d("ROOM", "start encryption")
val passphrase = "databasekey".toCharArray()
SQLCipherUtils.encrypt(context, "DatabaseName.db", passphrase)
Log.d("ROOM", "finish encryption")
}
val factory = SafeHelperFactory.fromUser(SpannableStringBuilder("databasekey"))
return Room
.databaseBuilder(
app,
MyDatabase::class.java,
"DatabaseName.db"
)
.openHelperFactory(factory)
.allowMainThreadQueries()
.addMigrations(*getMigrationList())
.build()
}
如果需要更多背景信息,我可以提供。如果这是已知问题或已经有可行的解决方案,请重定向我。谢谢。