房间数据库迁移
Room Database migration
如果我有 db 版本 10,我需要多少种方法来进行 Room 迁移?
我查看了以下示例Google Persistence Migration Sample
我发现 Migration 可变参数基于数据库版本 4 的可能场景。
public static UsersDatabase getInstance(Context context) {
synchronized (sLock) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
UsersDatabase.class, "Sample.db")
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_1_4)
.build();
}
return INSTANCE;
}
}
我的问题是,假设我正在使用 db v1 中的 Room,当我的应用程序达到 db v10 时,我需要编写多少种迁移方法?
在 sqlite 中,我们在 onUpgrade
中获取已安装应用程序的当前数据库版本,我们只是在没有 break 语句的情况下通过 switch case 来满足所有数据库升级。
但是,我不确定,但据我所知,我们无法在房间中获取已安装应用程序的当前数据库版本,我们编写了所有可能的迁移方法。
但是如果我有db v10,感觉很不方便,一共写了45个方法!
有没有更好的解决方案?
写入从版本 1 到版本 9 到 v10 的迁移。
public static UsersDatabase getInstance(Context context) {
synchronized (sLock) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
UsersDatabase.class, "Sample.db")
.addMigrations(MIGRATION_1_10, MIGRATION_2_10, MIGRATION_3_10, MIGRATION_4_10, .......)
.build();
}
return INSTANCE;
}
}
一旦您了解了您的数据库架构,您就可以直接从 v1 迁移到 v10,从 v2 迁移到 v10 等等。
Suppose I am using Room from the db v1 and by time my app reaches to db v10, how many migration methods will I have to write?
9.
一种方法是:1->2、2->3、3->4、4->5、5->6、6->7、7->8、8->9,以及9->10.
另一种做法是:1->10, 2->10, 3->10, ..., 9->10.
我猜第一种方法更受欢迎,因为它更容易开发。对于每个新的数据库版本,您只需创建一个额外的对象。
In sqlite, we get current db version of installed app in onUpgrade and we just fall through switch case without break statements so that it satisfies all db upgrades.
这将与您在 Room 中所做的代码行数大致相同。您的每个 case
语句都会变成一个 Migration
对象,处理增量升级(例如,3->4)。
But it feels so inconvenient, inappropriate to write total 45 methods if I have db v10!
Room 知道如何 "stitch together" 根据需要进行单独迁移以达到目标版本。因此,在我概述的第一种方法中,如果应用需要从 3 迁移到 10,Room 可以使用 3->4、4->5、5->6、...、9->10 来获取那里。
如果我有 db 版本 10,我需要多少种方法来进行 Room 迁移?
我查看了以下示例Google Persistence Migration Sample
我发现 Migration 可变参数基于数据库版本 4 的可能场景。
public static UsersDatabase getInstance(Context context) {
synchronized (sLock) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
UsersDatabase.class, "Sample.db")
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_1_4)
.build();
}
return INSTANCE;
}
}
我的问题是,假设我正在使用 db v1 中的 Room,当我的应用程序达到 db v10 时,我需要编写多少种迁移方法?
在 sqlite 中,我们在 onUpgrade
中获取已安装应用程序的当前数据库版本,我们只是在没有 break 语句的情况下通过 switch case 来满足所有数据库升级。
但是,我不确定,但据我所知,我们无法在房间中获取已安装应用程序的当前数据库版本,我们编写了所有可能的迁移方法。
但是如果我有db v10,感觉很不方便,一共写了45个方法!
有没有更好的解决方案?
写入从版本 1 到版本 9 到 v10 的迁移。
public static UsersDatabase getInstance(Context context) {
synchronized (sLock) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
UsersDatabase.class, "Sample.db")
.addMigrations(MIGRATION_1_10, MIGRATION_2_10, MIGRATION_3_10, MIGRATION_4_10, .......)
.build();
}
return INSTANCE;
}
}
一旦您了解了您的数据库架构,您就可以直接从 v1 迁移到 v10,从 v2 迁移到 v10 等等。
Suppose I am using Room from the db v1 and by time my app reaches to db v10, how many migration methods will I have to write?
9.
一种方法是:1->2、2->3、3->4、4->5、5->6、6->7、7->8、8->9,以及9->10.
另一种做法是:1->10, 2->10, 3->10, ..., 9->10.
我猜第一种方法更受欢迎,因为它更容易开发。对于每个新的数据库版本,您只需创建一个额外的对象。
In sqlite, we get current db version of installed app in onUpgrade and we just fall through switch case without break statements so that it satisfies all db upgrades.
这将与您在 Room 中所做的代码行数大致相同。您的每个 case
语句都会变成一个 Migration
对象,处理增量升级(例如,3->4)。
But it feels so inconvenient, inappropriate to write total 45 methods if I have db v10!
Room 知道如何 "stitch together" 根据需要进行单独迁移以达到目标版本。因此,在我概述的第一种方法中,如果应用需要从 3 迁移到 10,Room 可以使用 3->4、4->5、5->6、...、9->10 来获取那里。