Sugar ORM 迁移:创建新的 table drops/re-creates 现有的 table

Sugar ORM Migration: Create new table drops/re-creates existing tables

我在应用程序的第一个版本中使用了 SugarORM。 现在,我正在开发第二个版本,它向数据库添加了新的 tables。

根据 SugarORM documentation "Sugar will automatically create tables for new entities, so your migration script only needs to cater for alterations to existing tables."

这是我所做的:

  1. 增加了 AndroidManifest 中的数据库版本 <meta-data android:name="VERSION" android:value="2" />
  2. 创造了新记录class。 public class NewModel extends SugarRecord<NewModel>

当 运行 应用程序时,Sugar 创建了新的 table,但不幸的是,它也 dropped/created 现有的 tables,擦除本地保存的所有数据!

我还尝试通过添加包含我的 CREATE TABLE NEW_MODEL 语句的迁移脚本文件 2.sql 自己创建新的 table。同样不幸的是,它抛出了一个异常 "Table already exists" 因为 Sugar 创建了新的 table 然后尝试 运行 我的脚本!

有什么建议吗?

我找到了一个解决方案,其中我必须使用新版本号创建一个空的迁移脚本文件!现在,它创建了新的 table 而没有 dropping/creating 我的旧 tables.

所以,总结一下:

  1. 增加AndroidManifest中的数据库版本号。
  2. 创建新的 Sugar 记录 class。
  3. 添加一个以新数据库版本命名的空迁移脚本文件,放在 assets/sugar_upgrades 下。 (例如 2.sql)。