android.arch.persistence.room.migration.Migration Java Examples

The following examples show how to use android.arch.persistence.room.migration.Migration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocationDatabase.java    From background_location_updates with Apache License 2.0 5 votes vote down vote up
public static LocationDatabase getLocationDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(), LocationDatabase.class, "locations")
                        .addMigrations(new Migration(1, 2) {
                            @Override
                            public void migrate(@NonNull SupportSQLiteDatabase database) {
                                final String TABLE_TEMP = "_location_migration_1_2_temp";
                                final String TABLE_ORIG = "location";

                                final String CREATE_STMT_2 = "CREATE TABLE IF NOT EXISTS " +
                                        "`"+ TABLE_TEMP +"` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                        "`accuracy` REAL, " +
                                        "`longitude` REAL, " +
                                        "`latitude` REAL, " +
                                        "`altitude` REAL," +
                                        " `speed` REAL, " +
                                        "`time` INTEGER, " +
                                        "`vertical_accuracy` REAL, " +
                                        "`course` REAL, " +
                                        "`course_accuracy` REAL, " +
                                        "`speed_accuracy` REAL, `provider` TEXT, `read_count` INTEGER)";

                                database.execSQL(CREATE_STMT_2);

                                database.execSQL("INSERT INTO `" + TABLE_TEMP + "` (" +
                                            "id, accuracy, longitude, latitude, altitude, time" +
                                        ") SELECT id, accuracy, longitude, latitude, altitude, time FROM " + TABLE_ORIG);

                                database.execSQL("DROP TABLE " + TABLE_ORIG);
                                database.execSQL("ALTER TABLE " + TABLE_TEMP + " RENAME TO " + TABLE_ORIG);
                            }
                        })
                        .build();
    }
    return INSTANCE;
}