Last active
May 21, 2020 12:59
-
-
Save lomza/0f311a1b1e9c896bc58dff925d65eab2 to your computer and use it in GitHub Desktop.
Room db migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.ahammad.roomsample; | |
import android.arch.persistence.db.SupportSQLiteDatabase; | |
import android.arch.persistence.room.Database; | |
import android.arch.persistence.room.Room; | |
import android.arch.persistence.room.RoomDatabase; | |
import android.arch.persistence.room.migration.Migration; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
/** | |
* Created by alahammad on 10/2/17. | |
*/ | |
@Database(entities = {User.class}, version = 2) | |
public abstract class AppDatabase extends RoomDatabase { | |
private static AppDatabase INSTANCE; | |
private static final String DB_NAME = "database-name"; | |
static final Migration MIGRATION_1_2 = new Migration(1, 2) { | |
@Override | |
public void migrate(@NonNull SupportSQLiteDatabase database) { | |
database.execSQL("ALTER TABLE users " | |
+"ADD COLUMN address TEXT"); | |
} | |
}; | |
public static AppDatabase getDatabase(final Context context) { | |
if (INSTANCE == null) { | |
synchronized (AppDatabase.class) { | |
if (INSTANCE == null) { | |
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), | |
AppDatabase.class, DB_NAME) | |
.addMigrations(MIGRATION_1_2) | |
.build(); | |
} | |
} | |
} | |
return INSTANCE; | |
} | |
public abstract UserDao userDao(); | |
} | |
//// LocalCacheManager changes //// | |
private LocalCacheManager(Context context) { | |
db = AppDatabase.getDatabase(context); | |
} | |
public void findUserById(final DatabaseCallback databaseCallback, int id, final boolean delete) { | |
db.userDao().findById(id).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<User>() { | |
@Override | |
public void accept(@io.reactivex.annotations.NonNull User user) throws Exception { | |
if (user != null) { | |
if (!delete) | |
updateUser(databaseCallback, user); | |
else | |
deleteUser(databaseCallback, user); | |
} | |
} | |
}); | |
} | |
//// MainActivity changes ///// | |
private void getUser(int id, final boolean delete) { | |
LocalCacheManager.getInstance(this).findUserById(this, id, delete); | |
} | |
//// User.java ///// | |
package net.ahammad.roomsample; | |
import android.arch.persistence.room.ColumnInfo; | |
import android.arch.persistence.room.Entity; | |
import android.arch.persistence.room.PrimaryKey; | |
/** | |
* Created by alahammad on 10/2/17. | |
*/ | |
@Entity(tableName = "users") | |
public class User { | |
@PrimaryKey(autoGenerate = true) | |
private int uid; | |
@ColumnInfo(name = "first_name") | |
private String firstName; | |
@ColumnInfo(name = "last_name") | |
private String lastName; | |
@ColumnInfo(name = "address") | |
private String address; | |
public User(String firstName, String lastName, String address) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.address = address; | |
} | |
public int getUid() { | |
return uid; | |
} | |
public void setUid(int uid) { | |
this.uid = uid; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment