[Room 오류 해결] java.lang.IllegalStateException: Room cannot verify the data integrity.

문제 상황

룸 데이터베이스를 활용하는 중에 아래와 같은 오류가 발생하였습니다.

    java.lang.IllegalStateException: Room cannot verify the data integrity.
    Looks like you've changed schema but forgot to update the version number.
    You can simply fix this by increasing the version number. 
    Expected identity hash: 118073d0ddf4d96739f8efb713ebd556, 
    found: bb91b1f8c6584f92bc4ed367e358f416

 

생성한 데이터베이스 안의 테이블이나 컬럼들이 변경 되면 소스 상에서 변경된 상황을 App에게 명시해주어야 한다고 합니다.

Paging 처리 시 Remotekeys에 관한 컬럼명을 my_remotekey로 변경하였는데, 버전을 명시해주지 않아서 오류가 발생한 것 같습니다.

 

https://developer.android.com/training/data-storage/room/migrating-db-versions?hl=ko

 

Room 데이터베이스 이전  |  Android 개발자  |  Android Developers

Room 라이브러리를 사용하여 데이터베이스를 안전하게 이전하는 방법 알아보기

developer.android.com

 

문제 해결 전략 

모듈을 적용하는 곳에서 변경 사항을 알리는 migration을 추가하였습니다.

    @Singleton
    @Provides
    fun providesMyDatabase(@ApplicationContext context: Context) =
        Room.databaseBuilder(
            context,
            MyDatabase::class.java,
            "myDatabase"
        )
            .addMigrations(
                migrations = arrayOf(object  : Migration(1,2){
                    override fun migrate(database: SupportSQLiteDatabase) {
                        database.execSQL("ALTER TABLE myDatabase RENAME COLUMN remoteKeys to my_remoteKey")
                    }

                })
            )
            .build()

 

위와 같이 변경사항을 명시하는 migaration을 추가하고, 버전을 변경하였습니다.

@Database(
    version = 2,
    entities = [NoticeEntity::class, MyRemoteKeysEntity::class],
    exportSchema = false,
)

 

버전이 1-> 2로 변경되었으므로, Migration(1,2)로 지정 후 컬럼을 작성하였습니다.

SQL을 명시할 때 대소문자나 철자가 틀리지 않도록 주의해야 합니다.!