Skip to content

Commit 9567533

Browse files
gvaibhav1734ashishkumar468
authored andcommitted
Add destroyed column to BookmarkLocation table (#commons-app#3729) (commons-app#3774)
* Add destroyed column to BookmarkLocation table (#commons-app#3729) * switch with fall through for migrations
1 parent 1cda35e commit 9567533

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.java

+25-34
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Place fromCursor(Cursor cursor) {
164164
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)),
165165
builder.build(),
166166
cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)),
167-
null
167+
cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESTROYED))
168168
);
169169
}
170170

@@ -181,6 +181,7 @@ private ContentValues toContentValues(Place bookmarkLocation) {
181181
cv.put(BookmarkLocationsDao.Table.COLUMN_LAT, bookmarkLocation.location.getLatitude());
182182
cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude());
183183
cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic);
184+
cv.put(BookmarkLocationsDao.Table.COLUMN_DESTROYED, bookmarkLocation.destroyed);
184185
return cv;
185186
}
186187

@@ -199,6 +200,7 @@ public static class Table {
199200
static final String COLUMN_WIKIDATA_LINK = "location_wikidata_link";
200201
static final String COLUMN_COMMONS_LINK = "location_commons_link";
201202
static final String COLUMN_PIC = "location_pic";
203+
static final String COLUMN_DESTROYED = "location_destroyed";
202204

203205
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
204206
public static final String[] ALL_FIELDS = {
@@ -213,7 +215,8 @@ public static class Table {
213215
COLUMN_WIKIPEDIA_LINK,
214216
COLUMN_WIKIDATA_LINK,
215217
COLUMN_COMMONS_LINK,
216-
COLUMN_PIC
218+
COLUMN_PIC,
219+
COLUMN_DESTROYED
217220
};
218221

219222
static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
@@ -230,7 +233,8 @@ public static class Table {
230233
+ COLUMN_WIKIPEDIA_LINK + " STRING,"
231234
+ COLUMN_WIKIDATA_LINK + " STRING,"
232235
+ COLUMN_COMMONS_LINK + " STRING,"
233-
+ COLUMN_PIC + " STRING"
236+
+ COLUMN_PIC + " STRING,"
237+
+ COLUMN_DESTROYED + " STRING"
234238
+ ");";
235239

236240
public static void onCreate(SQLiteDatabase db) {
@@ -244,37 +248,24 @@ public static void onDelete(SQLiteDatabase db) {
244248

245249
public static void onUpdate(SQLiteDatabase db, int from, int to) {
246250
Timber.d("bookmarksLocations db is updated from:"+from+", to:"+to);
247-
if (from == to) {
248-
return;
249-
}
250-
if (from < 7) {
251-
// doesn't exist yet
252-
from++;
253-
onUpdate(db, from, to);
254-
return;
255-
}
256-
if (from == 7) {
257-
// table added in version 8
258-
onCreate(db);
259-
from++;
260-
onUpdate(db, from, to);
261-
return;
262-
}
263-
if (from == 8) {
264-
from++;
265-
onUpdate(db, from, to);
266-
return;
267-
}
268-
if (from == 10 && to == 11) {
269-
from++;
270-
//This is safe, and can be called clean, as we/I do not remember the appropriate version for this
271-
//We are anyways switching to room, these things won't be nescessary then
272-
try {
273-
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_pic STRING;");
274-
}catch (SQLiteException exception){
275-
Timber.e(exception);//
276-
}
277-
return;
251+
switch (from) {
252+
case 7: onCreate(db);
253+
case 8: // No change
254+
case 9: // No change
255+
case 10:
256+
try {
257+
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_pic STRING;");
258+
} catch (SQLiteException exception){
259+
Timber.e(exception);
260+
}
261+
case 11: // No change
262+
case 12:
263+
try {
264+
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_destroyed STRING;");
265+
}catch (SQLiteException exception){
266+
Timber.e(exception);
267+
}
268+
break;
278269
}
279270
}
280271
}

app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookMarkLocationDaoTest.kt

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class BookMarkLocationDaoTest {
3636
COLUMN_COMMONS_LINK,
3737
COLUMN_LAT,
3838
COLUMN_LONG,
39-
COLUMN_PIC)
39+
COLUMN_PIC,
40+
COLUMN_DESTROYED)
4041
private val client: ContentProviderClient = mock()
4142
private val database: SQLiteDatabase = mock()
4243
private val captor = argumentCaptor<ContentValues>()
@@ -95,6 +96,7 @@ class BookMarkLocationDaoTest {
9596
assertEquals(builder.build().wikidataLink, it.siteLinks.wikidataLink)
9697
assertEquals(builder.build().commonsLink, it.siteLinks.commonsLink)
9798
assertEquals("picName",it.pic)
99+
assertEquals("placeDestroyed", it.destroyed)
98100
}
99101
}
100102
}
@@ -147,7 +149,7 @@ class BookMarkLocationDaoTest {
147149
assertTrue(testObject.updateBookmarkLocation(examplePlaceBookmark))
148150
verify(client).insert(eq(BASE_URI), captor.capture())
149151
captor.firstValue.let { cv ->
150-
assertEquals(11, cv.size())
152+
assertEquals(12, cv.size())
151153
assertEquals(examplePlaceBookmark.name, cv.getAsString(COLUMN_NAME))
152154
assertEquals(examplePlaceBookmark.longDescription, cv.getAsString(COLUMN_DESCRIPTION))
153155
assertEquals(examplePlaceBookmark.label.text, cv.getAsString(COLUMN_LABEL_TEXT))
@@ -158,6 +160,7 @@ class BookMarkLocationDaoTest {
158160
assertEquals(examplePlaceBookmark.siteLinks.wikidataLink.toString(), cv.getAsString(COLUMN_WIKIDATA_LINK))
159161
assertEquals(examplePlaceBookmark.siteLinks.commonsLink.toString(), cv.getAsString(COLUMN_COMMONS_LINK))
160162
assertEquals(examplePlaceBookmark.pic.toString(), cv.getAsString(COLUMN_PIC))
163+
assertEquals(examplePlaceBookmark.destroyed.toString(), cv.getAsString(COLUMN_DESTROYED))
161164
}
162165
}
163166

@@ -253,12 +256,18 @@ class BookMarkLocationDaoTest {
253256
verify(database).execSQL(CREATE_TABLE_STATEMENT)
254257
}
255258

259+
@Test
260+
fun migrateTableVersionFrom_v12_to_v13() {
261+
onUpdate(database, 12, 13)
262+
verify(database).execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_destroyed STRING;")
263+
}
264+
256265
private fun createCursor(rowCount: Int) = MatrixCursor(columns, rowCount).apply {
257266

258267
for (i in 0 until rowCount) {
259268
addRow(listOf("placeName", "placeDescription","placeCategory", exampleLabel.text, exampleLabel.icon,
260269
exampleUri, builder.build().wikipediaLink, builder.build().wikidataLink, builder.build().commonsLink,
261-
exampleLocation.latitude, exampleLocation.longitude, "picName"))
270+
exampleLocation.latitude, exampleLocation.longitude, "picName", "placeDestroyed"))
262271
}
263272
}
264273
}

0 commit comments

Comments
 (0)