数据库迁移 数据迁移
Note: This article is part of the advanced Room series which covers all the details about the Room persistence library. You can read all the articles here:
注意:本文是高级Room系列的一部分,该系列涵盖有关Room持久性库的所有详细信息。 您可以在此处阅读所有文章:
Room Database Migrations [You are here]
会议室数据库迁移[您在这里]
会议室数据库迁移 (Room Database Migrations)
Database migration is a very important concept in any application development. As we add and change features in your app, we have to update schema of our database. Whenever there is a change in the schema of any of our tables, we need to write a migration for the existing application if we don’t want our users to lose all of their existing data.
在任何应用程序开发中,数据库迁移都是非常重要的概念。 当我们在您的应用程序中添加和更改功能时,我们必须更新数据库的架构。 每当我们的任何表的架构发生变化时,如果我们不希望用户丢失所有现有数据,就需要为现有应用程序编写迁移。
For example, we can consider a table named users
which contains information of users and it has 3 columns named uid
, first_name
and last_name
. Now if we add a new column for age
, we need to write a migration for altering the current table schema — i.e., adding a new column named age
.
例如,我们可以考虑一个名为users
的表,其中包含users
信息,并且它具有3列,分别为uid
, first_name
和last_name
。 现在,如果我们为age
添加一个新列,我们需要编写一个迁移来更改当前的表模式,即添加一个名为age
的新列。
The Room persistence library allows us to write Migration
classes to preserve user data in this manner. Each Migration
class specifies a startVersion
and endVersion
. At runtime, Room runs each Migration
class's migrate()
method, using the correct order to migrate the database to a later version.
Room持久性库允许我们编写Migration
类,以这种方式保留用户数据。 每个Migration
类都指定一个startVersion
和endVersion
。 在运行时,Room使用正确的顺序运行每个Migration
类的migrate()
方法,以将数据库迁移到更高版本。
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE users ADD COLUMN age INTEGER")
}
}
Now we have created the migration object MIGRATION_1_2
, we need to add it in the database configuration using database builder.
现在,我们已经创建了迁移对象MIGRATION_1_2
,我们需要使用数据库构建器将其添加到数据库配置中。
Room.databaseBuilder(
applicationContext,
UserDatabase::class.java,
"users-db"
).addMigrations(MIGRATION_1_2)
.build()
优雅地处理丢失的迁移路径 (Gracefully handle missing migration paths)
After updating our database’s schemas, it’s possible that some on-device databases could still use an older schema version. If Room cannot find a migration rule for upgrading that device’s database from the older version to the current version, it throws an IllegalStateException
which makes our app to crash.
更新数据库的架构后,某些设备上数据库仍可能使用较旧的架构版本。 如果Room无法找到将设备数据库从旧版本升级到当前版本的迁移规则,则会引发IllegalStateException
,这会使我们的应用崩溃。
To prevent the app from crashing when this situation occurs, call the fallbackToDestructiveMigration()
builder method when creating the database.
为防止这种情况发生时应用程序崩溃,在创建数据库时调用fallbackToDestructiveMigration()
构建器方法。
Room.databaseBuilder(
applicationContext,
UserDatabase::class.java,
"users-db"
).fallbackToDestructiveMigration()
.build()
The destructive recreation fallback logic includes several additional options:
破坏性的娱乐性后备逻辑包括几个其他选项:
If errors occur in specific versions of your schema history that you cannot solve with migration paths, use
fallbackToDestructiveMigrationFrom()
. This method indicates that you'd like Room to use the fallback logic only in cases where the database attempts to migrate from one of those problematic versions.如果在架构历史记录的特定版本中发生了无法使用迁移路径解决的错误,请使用
fallbackToDestructiveMigrationFrom()
。 此方法表明,仅在数据库尝试从这些有问题的版本之一迁移时,您才希望Room使用回退逻辑。To perform a destructive recreation only when attempting a schema downgrade, use
fallbackToDestructiveMigrationOnDowngrade()
instead.要仅在尝试将架构降级时执行破坏性重新创建,请改用
fallbackToDestructiveMigrationOnDowngrade()
。
Thank You!!!
谢谢!!!
翻译自: https://medium.com/mindorks/room-database-migrations-35a57bdd0ce6
数据库迁移 数据迁移