Skip to content
Permalink
Browse files
Merge pull request #6181 from hackbar/database-downgrade
Android: Handle a database downgrade.
  • Loading branch information
Helios747 committed Nov 10, 2017
2 parents 4dc425b + 74ae252 commit 126b7ea
Showing 1 changed file with 22 additions and 9 deletions.
@@ -78,6 +78,7 @@ public final class GameDatabase extends SQLiteOpenHelper
+ KEY_DB_ID + TYPE_PRIMARY + SEPARATOR
+ KEY_FOLDER_PATH + TYPE_STRING + CONSTRAINT_UNIQUE + ")";

private static final String SQL_DELETE_FOLDERS = "DROP TABLE IF EXISTS " + TABLE_NAME_FOLDERS;
private static final String SQL_DELETE_GAMES = "DROP TABLE IF EXISTS " + TABLE_NAME_GAMES;

public GameDatabase(Context context)
@@ -91,23 +92,29 @@ public void onCreate(SQLiteDatabase database)
{
Log.debug("[GameDatabase] GameDatabase - Creating database...");

Log.verbose("[GameDatabase] Executing SQL: " + SQL_CREATE_GAMES);
database.execSQL(SQL_CREATE_GAMES);
execSqlAndLog(database, SQL_CREATE_GAMES);
execSqlAndLog(database, SQL_CREATE_FOLDERS);
}

@Override
public void onDowngrade(SQLiteDatabase database, int oldVersion, int newVersion)
{
Log.verbose("[GameDatabase] Downgrades not supporting, clearing databases..");
execSqlAndLog(database, SQL_DELETE_FOLDERS);
execSqlAndLog(database, SQL_CREATE_FOLDERS);

Log.verbose("[GameDatabase] Executing SQL: " + SQL_CREATE_FOLDERS);
database.execSQL(SQL_CREATE_FOLDERS);
execSqlAndLog(database, SQL_DELETE_GAMES);
execSqlAndLog(database, SQL_CREATE_GAMES);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
{
Log.info("[GameDatabase] Upgrading database from schema version " + oldVersion + " to " + newVersion);

Log.verbose("[GameDatabase] Executing SQL: " + SQL_DELETE_GAMES);
database.execSQL(SQL_DELETE_GAMES);

Log.verbose("[GameDatabase] Executing SQL: " + SQL_CREATE_GAMES);
database.execSQL(SQL_CREATE_GAMES);
// Delete all the games
execSqlAndLog(database, SQL_DELETE_GAMES);
execSqlAndLog(database, SQL_CREATE_GAMES);

Log.verbose("[GameDatabase] Re-scanning library with new schema.");
scanLibrary(database);
@@ -283,4 +290,10 @@ public void call(Subscriber<? super Cursor> subscriber)
}
});
}

private void execSqlAndLog(SQLiteDatabase database, String sql)
{
Log.verbose("[GameDatabase] Executing SQL: " + sql);
database.execSQL(sql);
}
}

0 comments on commit 126b7ea

Please sign in to comment.