快速发现 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/

  1. 打开工具后,将 Expected 的信息复制到左边,将 Found 的信息复制到右边
  2. 点击 Go:会将两边的信息转为 json:
  3. 点击 Compare,就可以看到不同之处已经高亮显示了,右上角显示了差异的个数:

最后

当然,还有其他办法,如直接在 As 中建立两个文件存放两个表信息,选中两个文件再使用 Ctrl + D,进行比较;使用 BeyondCompare。但这个更好用。
开发中发现的好工具,记录下来,希望能够帮助到同学们。

参考

https://stackoverflow.com/questions/46372036/room-database-migration-doesnt-properly-handle-alter-table-migration

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

willwaywang6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值