如果僅添加新表,則會進行會議室數據庫遷移

[英]Room database migration if only new table is added


Let't assume, I have a simple Room database:

不要假設,我有一個簡單的房間數據庫:

@Database(entities = {User.class}, version = 1)
abstract class AppDatabase extends RoomDatabase {
    public abstract Dao getDao();
}

Now, I'm adding a new entity: Pet and bumping version to 2:

現在,我正在添加一個新實體:Pet和bumping version to 2:

@Database(entities = {User.class, Pet.class}, version = 2)
abstract class AppDatabase extends RoomDatabase {
    public abstract Dao getDao();
}

Of course, Room throws an exception: java.lang.IllegalStateException: A migration from 1 to 2 is necessary.

當然,Room會拋出異常:java.lang.IllegalStateException:需要從1遷移到2。

Assuming, I haven't changed User class (so all data is safe), I have to provide migration which just creates a new table. So, I'm looking into classes generated by Room, searching for generated query to create my new table, copying it and pasting into migration:

假設,我沒有更改User類(因此所有數據都是安全的),我必須提供只創建新表的遷移。所以,我正在調查Room生成的類,搜索生成的查詢以創建我的新表,復制它並粘貼到遷移中:

final Migration MIGRATION_1_2 =
        new Migration(1, 2) {
            @Override
            public void migrate(@NonNull final SupportSQLiteDatabase database) {
                database.execSQL("CREATE TABLE IF NOT EXISTS `Pet` (`name` TEXT NOT NULL, PRIMARY KEY(`name`))");
            }
        };

However I find it inconvenient to do it manually. Is there a way to tell Room: I'm not touching any of the existing table, so data is safe. Please create migration for me?

但是我覺得手動操作不方便。有沒有辦法告訴Room:我沒有觸及任何現有的表,所以數據是安全的。請為我創建遷移?

4 个解决方案

#1


0  

Maybe in this case(if you've only created new table without changing others) you can do this not creating any migrations at all?

也許在這種情況下(如果您只創建了新表而不更改其他表),您可以這樣做而不創建任何遷移嗎?

#2


0  

You can to create new SQlite table by Room migration without to change old tables. It is important to annotate PrimaryKey by @NonNull in @Entity. Without annotation Room does not create new table with primary key.

您可以通過Room遷移創建新的SQlite表,而無需更改舊表。在@Entity中通過@NonNull注釋PrimaryKey非常重要。如果沒有注釋,則Room不會使用主鍵創建新表。

@Entity
public class Pet {

  @PrimaryKey
  @android.support.annotation.NonNull
  @ColumnInfo(name = "name")
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

#3


0  

Sorry, Room doesn't support auto-creation of tables without data loss.

抱歉,Room不支持自動創建表而不會丟失數據。

It is mandatory to write the migration. Otherwise, it'll erase all the data and create the new table structure.

必須編寫遷移。否則,它將擦除所有數據並創建新的表結構。

#4


-1  

You can do this way-

你可以這樣做 -

@Database(entities = {User.class, Pet.class}, version = 2)

abstract class AppDatabase extends RoomDatabase {
public abstract Dao getDao();
public abstract Dao getPetDao();
}

Remaining will be same as you have mentioned above-

剩余將與您上面提到的相同 -

 db = Room.databaseBuilder(this, AppDatabase::class.java, "your_db")
        .addMigrations(MIGRATION_1_2).build()

Reference - For more

參考 - 更多


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/01/23/725eee289c87c70014368db14266baa8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com