Fix "duplicate column: maxage" (issue #445)

The bug is explained in detail in the issue tracker.
This change added guard condition to check for existence of the field
before adding.

While I was at it, I also guarded a bunch of other ALTER statements
with the if (!columnExists()) check. It turns out that many of them
break, but we only saw the first one because it threw an exception
before getting to the others.
This commit is contained in:
Peter Serwylo 2014-02-20 15:29:50 +11:00
parent 8f506c0b0b
commit 3240faf7f2

View File

@ -120,7 +120,7 @@ public class DBHelper extends SQLiteOpenHelper {
}
private void renameRepoId(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 36) {
if (oldVersion < 36 && !columnExists(db, TABLE_REPO, "_id")) {
Log.d("FDroid", "Renaming " + TABLE_REPO + ".id to _id");
db.beginTransaction();
@ -273,10 +273,12 @@ public class DBHelper extends SQLiteOpenHelper {
* default repos with values from strings.xml.
*/
private void addNameAndDescriptionToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 21) {
if (!columnExists(db, TABLE_REPO, "name"))
boolean nameExists = columnExists(db, TABLE_REPO, "name");
boolean descriptionExists = columnExists(db, TABLE_REPO, "description");
if (oldVersion < 21 && !(nameExists && descriptionExists)) {
if (!nameExists)
db.execSQL("alter table " + TABLE_REPO + " add column name text");
if (!columnExists(db, TABLE_REPO, "description"))
if (!descriptionExists)
db.execSQL("alter table " + TABLE_REPO + " add column description text");
ContentValues values = new ContentValues();
values.put("name", context.getString(R.string.default_repo_name));
@ -322,7 +324,7 @@ public class DBHelper extends SQLiteOpenHelper {
}
private void addMaxAgeToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 30) {
if (oldVersion < 30 && !columnExists(db, TABLE_REPO, "maxage")) {
db.execSQL("alter table " + TABLE_REPO + " add column maxage integer not null default 0");
}
}