Upgrade telephony db (carriers table) only if needed.

Before adding user_visible column check if it already exists.

Bug: 25193324
Change-Id: I2e9efcec30d366320e1d721c1518c4d018532b0c
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index 75cddee..bf7dc74 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -533,6 +533,12 @@
                 // copyPreservedApnsToNewTable()
                 // The only exception if upgrading from version 14 is that EDITED field is already
                 // present (but is called USER_EDITED)
+                /*********************************************************************************
+                 * IMPORTANT NOTE: SINCE CARRIERS TABLE IS RECREATED HERE, IT WILL BE THE LATEST
+                 * VERSION AFTER THIS. AS A RESULT ANY SUBSEQUENT UPDATES TO THE TABLE WILL FAIL
+                 * (DUE TO COLUMN-ALREADY-EXISTS KIND OF EXCEPTION). ALL SUBSEQUENT UPDATES SHOULD
+                 * HANDLE THAT GRACEFULLY.
+                 *********************************************************************************/
                 Cursor c;
                 String[] proj = {"_id"};
                 if (VDBG) {
@@ -617,8 +623,24 @@
                 oldVersion = 16 << 16 | 6;
             }
             if (oldVersion < (17 << 16 | 6)) {
-                db.execSQL("ALTER TABLE " + CARRIERS_TABLE + " ADD COLUMN " +
-                        Telephony.Carriers.USER_VISIBLE + " BOOLEAN DEFAULT 1;");
+                Cursor c = null;
+                try {
+                    c = db.query(CARRIERS_TABLE, null, null, null, null, null, null,
+                            String.valueOf(1));
+                    if (c == null || c.getColumnIndex(Telephony.Carriers.USER_VISIBLE) == -1) {
+                        db.execSQL("ALTER TABLE " + CARRIERS_TABLE + " ADD COLUMN " +
+                                Telephony.Carriers.USER_VISIBLE + " BOOLEAN DEFAULT 1;");
+                    } else {
+                        if (DBG) {
+                            log("onUpgrade skipping " + CARRIERS_TABLE + " upgrade.  Column " +
+                                    Telephony.Carriers.USER_VISIBLE + " already exists.");
+                        }
+                    }
+                } finally {
+                    if (c != null) {
+                        c.close();
+                    }
+                }
                 oldVersion = 17 << 16 | 6;
             }
             if (DBG) {