简体   繁体   English

使用现有布尔列类型进行房间迁移

[英]room migration using existing boolean column types

What i found out so far 到目前为止我发现了什么

All the @entity annotated classes are processed during compiletime and an Implementation for Database class is generated. 在编译期间处理所有@entity带注释的类,并生成Implementation for Database类。 Then before accessing the db, validateMigration method of this generated class is called. 然后在访问db之前,调用此生成的类的validateMigration方法。 This validateMigration method verifies with the existing db schema via raw query 此validateMigration方法通过原始查询使用现有的db模式进行验证

PRAGMA table_info mytable name

(see L208 of android.arch.persistence.room.util.TableInfo.java) (参见android.arch.persistence.room.util.TableInfo.java的L208)

Now the problem 现在问题

My sqlite3 db has some columns with column type as BOOLEAN. 我的sqlite3 db有一些列类型为BOOLEAN的列。 (which slqite internally handles to int). (slqite内部处理int)。 Now when i create room entities say 现在,当我创建房间实体说

public someEntity {
     @columnInfo(name="someName")
     public Boolean myValue;
}

The room's create table query will be 房间的创建表查询将是

Create Table someEntity ( myValue INTEGER)

Where as when we query the existing db with PRAGMA table_info someEntity we get 当我们使用PRAGMA table_info someEntity查询现有数据库时,我们得到了PRAGMA table_info someEntity

1|myValue|BOOLEAN|0||0

As explained above room verifies the ( sqlite to room ) migration by comparing field name, column type etc. And since the column types dont match (BOOLEAN and INTEGER) it throws an error saying migration failed. 如上所述,room通过比较字段名称,列类型等来验证(sqlite到room)迁移。由于列类型不匹配(BOOLEAN和INTEGER),它会抛出一个错误,表示迁移失败。

Can anyone suggest a workaround to this ? 任何人都可以建议一个解决方法吗? Can we make room create BOOLEAN column type in sqlite ? 我们可以在sqlite中创建空间创建BOOLEAN列类型吗? (Also afaik we can't change/alter column types of existing tables.) (另外,我们无法更改/更改现有表的列类型。)

PS: I also see a similar issue with VARCHAR - Using an existing VARCHAR column with Room PS:我也看到了与VARCHAR类似的问题 - 在Room中使用现有的VARCHAR列

Define the migration for the new attribute newAttribute with both a DEFAULT value and as NOT NULL . 使用DEFAULT值和NOT NULL定义新属性newAttribute的迁移。

Code

database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL")

Full Code 完整代码

@Database(entities = arrayOf(ModelName::class), version = 2)
@TypeConverters(Converters::class)
abstract class DatabaseName : RoomDatabase() {

    abstract fun daoName(): DaoName

    companion object {

        private var INSTANCE: DatabaseName? = null

        fun getAppDatabase(context: Context): DatabaseName {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.applicationContext,
                        DatabaseName::class.java, DATABASE_NAME)
                        .addMigrations(MIGRATION_1_2)
                        .build()
            }
            return INSTANCE as DatabaseName
        }

        val MIGRATION_1_2: Migration = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL")
            }
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM