快速发现 Room 升级失败时表单错误的工具
前言
最近,在项目中使用到了 Room 数据库,还是挺好用的。碰到的问题是,由于之前数据库版本考虑不足,导致上线后需要修改数据表,都需要进行数据库升级。自测时,发现升级错误,而由于表的字段比较多,查看错误之处很费眼。发现一个好用的工具,确实不错。
正文
比如现在,有这么一个升级错误信息:
io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | Migration didn't properly handle VideoInfo(com.wql.downloader.beans.VideoInfo).
Expected:
TableInfo{name='VideoInfo', columns={file_path=Column{name='file_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, size=Column{name='size', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, stop_by_network=Column{name='stop_by_network', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, video_url=Column{name='video_url', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, duration=Column{name='duration', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, page_url=Column{name='page_url', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='VideoInfo', columns={file_path=Column{name='file_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, size=Column{name='size', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, stop_by_network=Column{name='stop_by_network', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, video_url=Column{name='video_url', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, duration=Column{name='duration', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, page_url=Column{name='page_url', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:281)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:255)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:119)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by: java.lang.IllegalStateException: Migration didn't properly handle VideoInfo(com.wql.downloader.beans.VideoInfo).
Expected:
TableInfo{name='VideoInfo', columns={file_path=Column{name='file_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, size=Column{name='size', type='INTEGER',
从日志中,可以看到 Room 希望的表信息和实际的表信息有差异,导致的错误。如何快速定位差异呢?
可以使用这个小工具:https://hrankit.github.io/RoomSQLiteDifferenceFinder/。
- 打开工具后,将 Expected 的信息复制到左边,将 Found 的信息复制到右边
- 点击 Go:会将两边的信息转为 json:
- 点击 Compare,就可以看到不同之处已经高亮显示了,右上角显示了差异的个数:
最后
当然,还有其他办法,如直接在 As 中建立两个文件存放两个表信息,选中两个文件再使用 Ctrl + D,进行比较;使用 BeyondCompare。但这个更好用。
开发中发现的好工具,记录下来,希望能够帮助到同学们。