如何在 Android Room Persistence Library 中创建 table 而无需迁移版本?
How to create table without migrate version in Android Room Persistence Library?
只创建一个table,不改变其他table和数据,是否需要迁移数据库?
作为 google tutorial 和谷歌搜索,大多数说我们必须迁移数据库,如果我每次升级创建 table,我必须增加数据库版本和迁移,这很混乱
是的,是的。 Room 一开始就比较数据库架构,如果架构不同,它将 运行 迁移。如果架构不同并且不会添加迁移,则可能会抛出 IllegalStateException。
此外,当您提高了数据库版本并且架构没有变化时,您应该将迁移对象添加到构建器中。如果不是,Room 将清除您的数据库并重新创建它们。您可以在 class RoomDatabase.java
:
中阅读更多文档
/**
* Adds a migration to the builder.
* <p>
* Each Migration has a start and end versions and Room runs these migrations to bring the
* database to the latest version.
* <p>
* If a migration item is missing between current version and the latest version, Room
* will clear the database and recreate so even if you have no changes between 2 versions,
* you should still provide a Migration object to the builder.
* <p>
* A migration can handle more than 1 version (e.g. if you have a faster path to choose when
* going version 3 to 5 without going to version 4). If Room opens a database at version
* 3 and latest version is >= 5, Room will use the migration object that can migrate from
* 3 to 5 instead of 3 to 4 and 4 to 5.
*
* @param migrations The migration object that can modify the database and to the necessary
* changes.
* @return this
*/
@NonNull
public Builder<T> addMigrations(Migration... migrations) {
mMigrationContainer.addMigrations(migrations);
return this;
}
/**
* Allows Room to destructively recreate database tables if {@link Migration}s that would
* migrate old database schemas to the latest schema version are not found.
* <p>
* When the database version on the device does not match the latest schema version, Room
* runs necessary {@link Migration}s on the database.
* <p>
* If it cannot find the set of {@link Migration}s that will bring the database to the
* current version, it will throw an {@link IllegalStateException}.
* <p>
* You can call this method to change this behavior to re-create the database instead of
* crashing.
* <p>
* Note that this will delete all of the data in the database tables managed by Room.
*
* @return this
*/
@NonNull
public Builder<T> fallbackToDestructiveMigration() {
mRequireMigration = false;
return this;
}
只创建一个table,不改变其他table和数据,是否需要迁移数据库?
作为 google tutorial 和谷歌搜索,大多数说我们必须迁移数据库,如果我每次升级创建 table,我必须增加数据库版本和迁移,这很混乱
是的,是的。 Room 一开始就比较数据库架构,如果架构不同,它将 运行 迁移。如果架构不同并且不会添加迁移,则可能会抛出 IllegalStateException。
此外,当您提高了数据库版本并且架构没有变化时,您应该将迁移对象添加到构建器中。如果不是,Room 将清除您的数据库并重新创建它们。您可以在 class RoomDatabase.java
:
/**
* Adds a migration to the builder.
* <p>
* Each Migration has a start and end versions and Room runs these migrations to bring the
* database to the latest version.
* <p>
* If a migration item is missing between current version and the latest version, Room
* will clear the database and recreate so even if you have no changes between 2 versions,
* you should still provide a Migration object to the builder.
* <p>
* A migration can handle more than 1 version (e.g. if you have a faster path to choose when
* going version 3 to 5 without going to version 4). If Room opens a database at version
* 3 and latest version is >= 5, Room will use the migration object that can migrate from
* 3 to 5 instead of 3 to 4 and 4 to 5.
*
* @param migrations The migration object that can modify the database and to the necessary
* changes.
* @return this
*/
@NonNull
public Builder<T> addMigrations(Migration... migrations) {
mMigrationContainer.addMigrations(migrations);
return this;
}
/**
* Allows Room to destructively recreate database tables if {@link Migration}s that would
* migrate old database schemas to the latest schema version are not found.
* <p>
* When the database version on the device does not match the latest schema version, Room
* runs necessary {@link Migration}s on the database.
* <p>
* If it cannot find the set of {@link Migration}s that will bring the database to the
* current version, it will throw an {@link IllegalStateException}.
* <p>
* You can call this method to change this behavior to re-create the database instead of
* crashing.
* <p>
* Note that this will delete all of the data in the database tables managed by Room.
*
* @return this
*/
@NonNull
public Builder<T> fallbackToDestructiveMigration() {
mRequireMigration = false;
return this;
}