简体   繁体   English

房间数据库迁移

[英]Room Database migration

How many methods will I have to make for Room migration if I have db version 10? 如果我有db版本10,我将有多少方法可以进行房间迁移?

I looked into the following example Google Persistence Migration Sample 我查看了以下示例Google Persistence Migration Sample

and I found Migration varargs based on probable scenarios for database version 4. 我发现了基于数据库版本4的可能场景的Migration varargs。

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;
    } 
} 

My question is, 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? 我的问题是,假设我正在使用db v1中的Room,到我的应用程序达到db v10时,我将需要编写多少种迁移方法?

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. 在sqlite中,我们在onUpgrade获得已安装app的当前db版本,我们只是通过switch case而没有break语句,因此它满足所有db升级。

However, I am not sure, but afaik, we cannot get current db version of installed app in room, we write all possible methods for migration. 但是,我不确定,但是afaik,我们无法在室内获得已安装app的当前db版本,我们编写了所有可能的迁移方法。

But it feels so inconvenient, inappropriate to write total 45 methods if I have db v10! 但是如果我有db v10的话,感觉很不方便,不适合写45种方法!

Is there any better solution? 有没有更好的解决方案?

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? 假设我正在使用db v1中的Room,并且当我的应用程序到达db v10时,我需要编写多少种迁移方法?

9. 9。

One approach is: 1->2, 2->3, 3->4, 4->5, 5->6, 6->7, 7->8, 8->9, and 9->10. 一种方法是:1-> 2,2-> 3,3-> 4,4-> 5,5-> 6,6-> 7,7-> 8,8-> 9和9-> 10。

Another approach is: 1->10, 2->10, 3->10, ..., 9->10. 另一种方法是:1-> 10,2-> 10,3-> 10,......,9-> 10。

My guess is that the first approach is more popular, as it is easier to develop. 我的猜测是第一种方法更受欢迎,因为它更容易开发。 For each new database release, you just create one additional object. 对于每个新的数据库版本,您只需创建一个附加对象。

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. 在sqlite中,我们在onUpgrade中获得已安装app的当前db版本,我们只是通过switch case而没有break语句,因此它满足所有db升级。

That will be about the same number of lines of code, give or take, as what you would do in Room. 这将与您在Room中所做的相同数量的代码行,给予或接受。 Each one of your case statements gets turned into a Migration object, handling an incremental upgrade (eg, 3->4). 每个case语句都会转换为Migration对象,处理增量升级(例如,3→4)。

But it feels so inconvenient, inappropriate to write total 45 methods if I have db v10! 但是如果我有db v10的话,感觉很不方便,不适合写45种方法!

Room knows how to "stitch together" individual migrations as needed to reach a target version. 房间知道如何根据需要“拼接”单个迁移以达到目标版本。 So, in the first approach that I outlined, if an app needs to be migrated from 3 to 10, Room can use 3->4, 4->5, 5->6, ..., 9->10 to get there. 因此,在我概述的第一种方法中,如果应用程序需要从3迁移到10,则Room可以使用3-> 4,4-> 5,5-> 6,...,9-> 10来获取那里。

Write migrations to v10 from versions 1 to 9. 将版本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;
} 
} 

Once you know your db schema you can migrate directly from v1 to v10, v2 to v10 and so on. 了解了数据库模式后,可以直接从v1迁移到v10,v2迁移到v10,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM