Search code examples
androidsqlitesqliteopenhelperreactive

Reactive android database migration if only new table is added


I Am added the new coulmn for database ,I am getting this error while running program with ReActive android Database.

Unable to create application com.reactiveandroid.sample.App: java.lang.IllegalArgumentException: SQL file assets/from_2_to_3.sql note found.

@Database(name = "AppDatabase", version = 3)
public class AppDatabase {
    static final Migration MIGRATION_1_2=new Migration(1,2) {
        @Override
        public void migrate(SQLiteDatabase database) {
            AssetsSqlMigration.executeSqlScript(database,"assets/from_1_to_2.sql");
        }
    };

    static final Migration MIGRATION_2_3=new Migration(2,3) {
        @Override
        public void migrate(SQLiteDatabase database) {
            AssetsSqlMigration.executeSqlScript(database,"assets/from_2_to_3.sql");
        }
    };
}

public class App extends Application{

@Override
public void onCreate() {
    super.onCreate();

    DatabaseConfig appDatabaseConfig = new DatabaseConfig.Builder(AppDatabase.class)
            .addModelClasses(Note.class, Folder.class, NoteFolderRelation.class)
            .addMigrations(AppDatabase.MIGRATION_1_2 ,MIGRATION_2_3)
            .disableMigrationsChecking()
            .build();

    ReActiveAndroid.init(new ReActiveConfig.Builder(this)
            .addDatabaseConfigs(appDatabaseConfig)
            .build());


}

}


ALTER TABLE Note ADD COLUMN nameid INTEGER;


Solution

  • You need to write DDL Queries in migrate() as below.

    static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            // if you need to create new table
            database.execSQL("ALTER TABLE your_table "
                + " ADD COLUMN column_name DATATYPE");
        }
    };