java.lang.RuntimeException: Unable to resume activity {br.com.alura.agenda/br.com.alura.agenda.ui.activity.ListaAlunosActivity}: java.lang.IllegalStateException: A migration from 5 to 4 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3400)
Arquivo AgendaMigrations.java:
package br.com.alura.agenda.database;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.room.migration.Migration;
import androidx.annotation.NonNull;
class AgendaMigrations {
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE Aluno ADD COLUMN sobrenome TEXT");
}
};
private static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// Criar nova tabela com as informações desejadas
database.execSQL("CREATE TABLE IF NOT EXISTS `Aluno_novo` " +
"(`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"`nome` TEXT, " +
"`telefone` TEXT, " +
"`email` TEXT)");
// Copiar dados da tabela antiga para a nova
database.execSQL("INSERT INTO Aluno_novo (id, nome, telefone, email) " +
"SELECT id, nome, telefone, email FROM Aluno");
// Remove tabela antiga
database.execSQL("DROP TABLE Aluno");
// Renomear a tabela nova com o nome da tabela antiga
database.execSQL("ALTER TABLE Aluno_novo RENAME TO Aluno");
}
};
private static final Migration MIGRATION_3_4 = new Migration(3, 4) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE Aluno ADD COLUMN momentoDeCadastro INTEGER");
}
};
static final Migration[] TODAS_MIGRATIONS = {MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4};
}
Arquivo Aluno.java:
package br.com.alura.agenda.model;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Calendar;
@Entity
public class Aluno implements Serializable {
@PrimaryKey(autoGenerate = true)
private int id = 0;
private String nome;
private String telefone;
private String email;
private Calendar momentoDeCadastro = Calendar.getInstance();
public Calendar getMomentoDeCadastro() {
return momentoDeCadastro;
}
public void setMomentoDeCadastro(Calendar momentoDeCadastro) {
this.momentoDeCadastro = momentoDeCadastro;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public void setEmail(String email) {
this.email = email;
}
public String getNome() {
return nome;
}
public String getTelefone() {
return telefone;
}
public String getEmail() {
return email;
}
@NonNull
@Override
public String toString() {
return nome + " - " + telefone;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public boolean temIdValido() {
return id > 0;
}
}
O que será que pode ser ?