ClauSita
ClauSita

Reputation: 33

How to migrate a Room database adding a Date column?

I have a simple database, with one table (id, title and description), I want to add a column to storage a duedate. I use the info I found in Developer android migration and TypeConverters to migrate it, I can install to test, but it doesn't open the app. I'll apreciate any help!

My entity


@Entity(tableName = "todo_db")
public class todoEnt {
    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo(name = "title")
    public String todoTitle;

    @ColumnInfo(name = "description")
    public String todoDescription;

    @ColumnInfo(name = "dueDate")
    public Date dueDate;

}

I have a Typeconverter

public class Convertors {
    @TypeConverter
    public static Date fromTimeStamp(Long value){
        return value == null ? null : new Date(value);
    }

    @TypeConverter
    public static Long dateToTimestamp(Date date){
        if (date == null) {
            return null;
        } else {
            return date.getTime();
        }
    }
}

And I added this to my database file

@TypeConverters({Convertors.class})
    static final Migration M_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE todo_db ADD duedate INTEGER");
        }
    };
   INSTANCE = Room.databaseBuilder(
       context.getApplicationContext(),
       TDAHdb.class,
       "tdah_database")
       .addMigrations(M_1_2)
       .allowMainThreadQueries()
       .build();

Upvotes: 2

Views: 936

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38319

With Room migrations, table and column names are case-sensitive. Change duedate in your migration to match the case of the @ColumnInfo name specified in the entity definition, dueDate:

database.execSQL("ALTER TABLE todo_db ADD dueDate INTEGER");

Upvotes: 1

Related Questions