Bug 1128523 - Don't alter reading list table if it was created with the new schema in an earlier migration. r=rnewman
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Wed, 04 Feb 2015 13:56:53 -0800
changeset 227873 2b2130aec3cd093fb5362216a597610594fa8e22
parent 227872 1de14614f44e49095562887018bdb6f220dfa98e
child 227874 3d5548f9d755920ab8fa1a32df903feb8c25fb11
push idunknown
push userunknown
push dateunknown
reviewersrnewman
bugs1128523
milestone38.0a1
Bug 1128523 - Don't alter reading list table if it was created with the new schema in an earlier migration. r=rnewman
mobile/android/base/db/BrowserDatabaseHelper.java
--- a/mobile/android/base/db/BrowserDatabaseHelper.java
+++ b/mobile/android/base/db/BrowserDatabaseHelper.java
@@ -19,16 +19,17 @@ import org.mozilla.gecko.db.BrowserContr
 import static org.mozilla.gecko.db.DBUtils.qualifyColumn;
 
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteException;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.net.Uri;
 import android.os.Build;
 import android.util.Log;
 
 
 final class BrowserDatabaseHelper extends SQLiteOpenHelper {
 
@@ -762,22 +763,28 @@ final class BrowserDatabaseHelper extend
 
     private void upgradeDatabaseFrom19to20(SQLiteDatabase db) {
         createSearchHistoryTable(db);
     }
 
     private void upgradeDatabaseFrom21to22(SQLiteDatabase db) {
         debug("Adding CONTENT_STATUS column to reading list table.");
 
-        db.execSQL("ALTER TABLE " + TABLE_READING_LIST +
-                   " ADD COLUMN " + ReadingListItems.CONTENT_STATUS +
-                   " TINYINT DEFAULT " + ReadingListItems.STATUS_UNFETCHED);
+        try {
+            db.execSQL("ALTER TABLE " + TABLE_READING_LIST +
+                       " ADD COLUMN " + ReadingListItems.CONTENT_STATUS +
+                       " TINYINT DEFAULT " + ReadingListItems.STATUS_UNFETCHED);
 
-        db.execSQL("CREATE INDEX reading_list_content_status ON " + TABLE_READING_LIST + "("
-                + ReadingListItems.CONTENT_STATUS + ")");
+            db.execSQL("CREATE INDEX reading_list_content_status ON " + TABLE_READING_LIST + "("
+                    + ReadingListItems.CONTENT_STATUS + ")");
+        } catch (SQLiteException e) {
+            // We're betting that an error here means that the table already has the column,
+            // so we're failing due to the duplicate column name.
+            Log.e(LOGTAG, "Error upgrading database from 21 to 22", e);
+        }
     }
 
     private void createV19CombinedView(SQLiteDatabase db) {
         db.execSQL("DROP VIEW IF EXISTS " + VIEW_COMBINED);
         db.execSQL("DROP VIEW IF EXISTS " + VIEW_COMBINED_WITH_FAVICONS);
 
         createCombinedViewOn19(db);
     }
@@ -835,17 +842,21 @@ final class BrowserDatabaseHelper extend
                     upgradeDatabaseFrom18to19(db);
                     break;
 
                 case 20:
                     upgradeDatabaseFrom19to20(db);
                     break;
 
                 case 22:
-                    upgradeDatabaseFrom21to22(db);
+                    if (oldVersion <= 17) {
+                         // We just created the right table in 17to18. Do nothing here.
+                    } else {
+                         upgradeDatabaseFrom21to22(db);
+                    }
                     break;
             }
         }
 
         for (Table table : BrowserProvider.sTables) {
             table.onUpgrade(db, oldVersion, newVersion);
         }