Skip to content

Commit 64eae8b

Browse files
ashishkumar468misaochan
authored andcommitted
Bug fix issue #2476 (#2526)
* Added wikidataEntityID in all db versions, handled db.execSql via method runQuery
1 parent 6365815 commit 64eae8b

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java

+31-17
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
import android.content.ContentValues;
55
import android.database.Cursor;
66
import android.database.sqlite.SQLiteDatabase;
7+
import android.database.sqlite.SQLiteException;
78
import android.net.Uri;
89
import android.os.RemoteException;
910
import android.support.annotation.Nullable;
1011
import android.text.TextUtils;
11-
12+
import fr.free.nrw.commons.settings.Prefs;
13+
import fr.free.nrw.commons.utils.StringUtils;
1214
import java.util.Date;
13-
1415
import javax.inject.Inject;
1516
import javax.inject.Named;
1617
import javax.inject.Provider;
17-
18-
import fr.free.nrw.commons.settings.Prefs;
19-
import fr.free.nrw.commons.utils.StringUtils;
18+
import timber.log.Timber;
2019

2120
import static fr.free.nrw.commons.contributions.ContributionDao.Table.ALL_FIELDS;
2221
import static fr.free.nrw.commons.contributions.ContributionDao.Table.COLUMN_WIKI_DATA_ENTITY_ID;
@@ -262,16 +261,20 @@ public static void onUpdate(SQLiteDatabase db, int from, int to) {
262261
if (from == to) {
263262
return;
264263
}
264+
265+
//Considering the crashes we have been facing recently, lets blindly add this column to any table which has ever existed
266+
runQuery(db,ADD_WIKI_DATA_ENTITY_ID_FIELD);
267+
265268
if (from == 1) {
266-
db.execSQL(ADD_DESCRIPTION_FIELD);
267-
db.execSQL(ADD_CREATOR_FIELD);
269+
runQuery(db,ADD_DESCRIPTION_FIELD);
270+
runQuery(db,ADD_CREATOR_FIELD);
268271
from++;
269272
onUpdate(db, from, to);
270273
return;
271274
}
272275
if (from == 2) {
273-
db.execSQL(ADD_MULTIPLE_FIELD);
274-
db.execSQL(SET_DEFAULT_MULTIPLE);
276+
runQuery(db, ADD_MULTIPLE_FIELD);
277+
runQuery(db, SET_DEFAULT_MULTIPLE);
275278
from++;
276279
onUpdate(db, from, to);
277280
return;
@@ -290,23 +293,34 @@ public static void onUpdate(SQLiteDatabase db, int from, int to) {
290293
}
291294
if (from == 5) {
292295
// Added width and height fields
293-
db.execSQL(ADD_WIDTH_FIELD);
294-
db.execSQL(SET_DEFAULT_WIDTH);
295-
db.execSQL(ADD_HEIGHT_FIELD);
296-
db.execSQL(SET_DEFAULT_HEIGHT);
297-
db.execSQL(ADD_LICENSE_FIELD);
298-
db.execSQL(SET_DEFAULT_LICENSE);
296+
runQuery(db, ADD_WIDTH_FIELD);
297+
runQuery(db, SET_DEFAULT_WIDTH);
298+
runQuery(db, ADD_HEIGHT_FIELD);
299+
runQuery(db, SET_DEFAULT_HEIGHT);
300+
runQuery(db, ADD_LICENSE_FIELD);
301+
runQuery(db, SET_DEFAULT_LICENSE);
299302
from++;
300303
onUpdate(db, from, to);
301304
return;
302305
}
303306
if (from > 5) {
304307
// Added place field
305-
db.execSQL(ADD_WIKI_DATA_ENTITY_ID_FIELD);
306-
from++;
308+
from=to;
307309
onUpdate(db, from, to);
308310
return;
309311
}
310312
}
313+
314+
/**
315+
* perform the db.execSQl with handled exceptions
316+
*/
317+
private static void runQuery(SQLiteDatabase db, String query) {
318+
try {
319+
db.execSQL(query);
320+
} catch (SQLiteException e) {
321+
Timber.e("Exception performing query: " + query + " message: " + e.getMessage());
322+
}
323+
}
324+
311325
}
312326
}

app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionDaoTest.kt

-6
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,11 @@ class ContributionDaoTest {
7878
@Test
7979
fun upgradeDatabase_v3_to_v4() {
8080
Table.onUpdate(database, 3, 4)
81-
82-
// No changes
83-
verifyZeroInteractions(database)
8481
}
8582

8683
@Test
8784
fun upgradeDatabase_v4_to_v5() {
8885
Table.onUpdate(database, 4, 5)
89-
90-
// No changes
91-
verifyZeroInteractions(database)
9286
}
9387

9488
@Test

0 commit comments

Comments
 (0)