Merge pull request #1342 from nextcloud/errorDuringDbUpgrade

Check if columns exists prior adding them
This commit is contained in:
Tobias Kaminsky 2017-08-18 09:02:17 +02:00 committed by GitHub
commit 09de435ebc
2 changed files with 75 additions and 13 deletions

View file

@ -32,7 +32,7 @@ import com.owncloud.android.MainApp;
public class ProviderMeta {
public static final String DB_NAME = "filelist";
public static final int DB_VERSION = 23;
public static final int DB_VERSION = 24;
private ProviderMeta() {
}

View file

@ -43,7 +43,6 @@ import android.text.TextUtils;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.db.ProviderMeta;
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
import com.owncloud.android.lib.common.accounts.AccountUtils;
@ -1067,19 +1066,34 @@ public class FileContentProvider extends ContentProvider {
db.beginTransaction();
try {
// add type column default being CUSTOM (0)
Log_OC.i(SQL, "Add type column and default value 0 (CUSTOM) to synced_folders table");
db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_TYPE +
" INTEGER " + " DEFAULT 0");
if (!checkIfColumnExists(db, ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME,
ProviderTableMeta.SYNCED_FOLDER_TYPE)) {
Log_OC.i(SQL, "Add type column and default value 0 (CUSTOM) to synced_folders table");
db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_TYPE +
" INTEGER " + " DEFAULT 0");
} else {
Log_OC.i(SQL, "Type column of synced_folders table already exists");
}
Log_OC.i(SQL, "Add charging and wifi columns to uploads");
db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WIFI_ONLY +
" INTEGER " + " DEFAULT 0");
if (!checkIfColumnExists(db, ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME,
ProviderTableMeta.UPLOADS_IS_WIFI_ONLY)) {
Log_OC.i(SQL, "Add charging and wifi columns to uploads");
db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WIFI_ONLY +
" INTEGER " + " DEFAULT 0");
} else {
Log_OC.i(SQL, "Wifi column of synced_folders table already exists");
}
db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY +
" INTEGER " + " DEFAULT 0");
if (!checkIfColumnExists(db, ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME,
ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY)) {
db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY +
" INTEGER " + " DEFAULT 0");
} else {
Log_OC.i(SQL, "Charging column of synced_folders table already exists");
}
// create Filesystem table
Log_OC.i(SQL, "Create filesystem table");
@ -1097,10 +1111,58 @@ public class FileContentProvider extends ContentProvider {
if (!upgraded) {
Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
}
if (oldVersion < 24 && newVersion >= 24) {
Log_OC.i(SQL, "Entering in the #24 Re-adding user theming to capabilities table");
db.beginTransaction();
try {
if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
ProviderTableMeta.CAPABILITIES_SERVER_NAME)) {
db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_NAME + " TEXT ");
}
if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
ProviderTableMeta.CAPABILITIES_SERVER_COLOR)) {
db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + " TEXT ");
}
if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL)) {
db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT ");
}
if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN)) {
db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + " TEXT ");
}
upgraded = true;
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
if (!upgraded) {
Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
}
}
}
}
private boolean checkIfColumnExists(SQLiteDatabase database, String table, String column) {
Cursor cursor = database.rawQuery("SELECT * FROM " + table + " LIMIT 0", null);
boolean exists = cursor.getColumnIndex(column) != -1;
cursor.close();
return exists;
}
private void createFilesTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("