Skip to content

Instantly share code, notes, and snippets.

@JaydipZala
Last active July 30, 2018 11:52
Show Gist options
  • Save JaydipZala/7276eb6cdde525a53008613b4aac1bcc to your computer and use it in GitHub Desktop.
Save JaydipZala/7276eb6cdde525a53008613b4aac1bcc to your computer and use it in GitHub Desktop.
Room database migration for all
public class DBConstants {
// word table
public static final String TABLE_WORD = "word_table";
public static final String KEY_WORD = "word";
public static final String KEY_ID = "word_id";
}
public class DBUtils {
public static ArrayList<String> getColumns(SupportSQLiteDatabase db, String tableName) {
ArrayList<String> al = null;
Cursor c = null;
try {
c = db.query("SELECT * FROM " + tableName + " LIMIT 1", null);
if (c != null) {
al = new ArrayList<>(Arrays.asList(c.getColumnNames()));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null)
c.close();
}
return al;
}
}
@Entity(tableName = DBConstants.TABLE_WORD)
public class Word {
public static void upgradeWordTable(SupportSQLiteDatabase database) {
ArrayList<String> columns = DBUtils.getColumns(database, DBConstants.TABLE_WORD);
// 1. Create new table
final String TABLE_NAME_TEMP = "_temp" + DBConstants.TABLE_WORD;
database.execSQL("CREATE TABLE IF NOT EXISTS `" + TABLE_NAME_TEMP + "` (`" + DBConstants.KEY_WORD + "` TEXT NOT NULL, `" + DBConstants.KEY_ID + "` TEXT, PRIMARY KEY(`" + DBConstants.KEY_WORD + "`))");
// 2. find common columns.
columns.retainAll(DBUtils.getColumns(database, TABLE_NAME_TEMP));
final String COLS = TextUtils.join(",", columns);
// 3. Copy the data
database.execSQL("INSERT INTO " + TABLE_NAME_TEMP + " (" + COLS + ") "
+ "SELECT " + COLS + " "
+ "FROM " + DBConstants.TABLE_WORD);
// 4. Remove the old table
database.execSQL("DROP TABLE " + DBConstants.TABLE_WORD);
// 5. Change the table name to the correct one
database.execSQL("ALTER TABLE " + TABLE_NAME_TEMP + " RENAME TO " + DBConstants.TABLE_WORD);
}
@PrimaryKey
@NonNull
@ColumnInfo(name = DBConstants.KEY_WORD)
private String mWord;
@Nullable
@ColumnInfo(name = DBConstants.KEY_ID)
private String mId;
public Word(@NonNull String word, @Nullable String mId) {
this.mWord = word;
this.mId = mId;
}
public String getWord() {
return this.mWord;
}
@NonNull
public String getId() {
return mId == null ? "" : mId;
}
}
@Database(entities = {Word.class}, version = 2)
public abstract class WordRoomDatabase extends RoomDatabase {
public abstract WordDao wordDao();
private static WordRoomDatabase INSTANCE;
static WordRoomDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (WordRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
WordRoomDatabase.class, "word_database")
.addMigrations(WordRoomDatabase.MIGRATION_1_2)
.build();
}
}
}
return INSTANCE;
}
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
Word.upgradeWordTable(database);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment