Add destroyed column to BookmarkLocation table (##3729) (#3774)

* Add destroyed column to BookmarkLocation table (##3729)

* switch with fall through for migrations
This commit is contained in:
Vaibhav 2020-05-27 14:40:00 +05:30 committed by GitHub
parent 7a49ca12a4
commit 82d662c8ef
3 changed files with 38 additions and 38 deletions

View file

@ -164,7 +164,7 @@ public class BookmarkLocationsDao {
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)),
builder.build(), builder.build(),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)),
null cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESTROYED))
); );
} }
@ -181,6 +181,7 @@ public class BookmarkLocationsDao {
cv.put(BookmarkLocationsDao.Table.COLUMN_LAT, bookmarkLocation.location.getLatitude()); cv.put(BookmarkLocationsDao.Table.COLUMN_LAT, bookmarkLocation.location.getLatitude());
cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude()); cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude());
cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic); cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic);
cv.put(BookmarkLocationsDao.Table.COLUMN_DESTROYED, bookmarkLocation.destroyed);
return cv; return cv;
} }
@ -199,6 +200,7 @@ public class BookmarkLocationsDao {
static final String COLUMN_WIKIDATA_LINK = "location_wikidata_link"; static final String COLUMN_WIKIDATA_LINK = "location_wikidata_link";
static final String COLUMN_COMMONS_LINK = "location_commons_link"; static final String COLUMN_COMMONS_LINK = "location_commons_link";
static final String COLUMN_PIC = "location_pic"; static final String COLUMN_PIC = "location_pic";
static final String COLUMN_DESTROYED = "location_destroyed";
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES. // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
public static final String[] ALL_FIELDS = { public static final String[] ALL_FIELDS = {
@ -213,7 +215,8 @@ public class BookmarkLocationsDao {
COLUMN_WIKIPEDIA_LINK, COLUMN_WIKIPEDIA_LINK,
COLUMN_WIKIDATA_LINK, COLUMN_WIKIDATA_LINK,
COLUMN_COMMONS_LINK, COLUMN_COMMONS_LINK,
COLUMN_PIC COLUMN_PIC,
COLUMN_DESTROYED
}; };
static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME; static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
@ -230,7 +233,8 @@ public class BookmarkLocationsDao {
+ COLUMN_WIKIPEDIA_LINK + " STRING," + COLUMN_WIKIPEDIA_LINK + " STRING,"
+ COLUMN_WIKIDATA_LINK + " STRING," + COLUMN_WIKIDATA_LINK + " STRING,"
+ COLUMN_COMMONS_LINK + " STRING," + COLUMN_COMMONS_LINK + " STRING,"
+ COLUMN_PIC + " STRING" + COLUMN_PIC + " STRING,"
+ COLUMN_DESTROYED + " STRING"
+ ");"; + ");";
public static void onCreate(SQLiteDatabase db) { public static void onCreate(SQLiteDatabase db) {
@ -244,37 +248,24 @@ public class BookmarkLocationsDao {
public static void onUpdate(SQLiteDatabase db, int from, int to) { public static void onUpdate(SQLiteDatabase db, int from, int to) {
Timber.d("bookmarksLocations db is updated from:"+from+", to:"+to); Timber.d("bookmarksLocations db is updated from:"+from+", to:"+to);
if (from == to) { switch (from) {
return; case 7: onCreate(db);
} case 8: // No change
if (from < 7) { case 9: // No change
// doesn't exist yet case 10:
from++; try {
onUpdate(db, from, to); db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_pic STRING;");
return; } catch (SQLiteException exception){
} Timber.e(exception);
if (from == 7) { }
// table added in version 8 case 11: // No change
onCreate(db); case 12:
from++; try {
onUpdate(db, from, to); db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_destroyed STRING;");
return; }catch (SQLiteException exception){
} Timber.e(exception);
if (from == 8) { }
from++; break;
onUpdate(db, from, to);
return;
}
if (from == 10 && to == 11) {
from++;
//This is safe, and can be called clean, as we/I do not remember the appropriate version for this
//We are anyways switching to room, these things won't be nescessary then
try {
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_pic STRING;");
}catch (SQLiteException exception){
Timber.e(exception);//
}
return;
} }
} }
} }

View file

@ -13,7 +13,7 @@ import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao;
public class DBOpenHelper extends SQLiteOpenHelper { public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "commons.db"; private static final String DATABASE_NAME = "commons.db";
private static final int DATABASE_VERSION = 12; private static final int DATABASE_VERSION = 13;
public static final String CONTRIBUTIONS_TABLE = "contributions"; public static final String CONTRIBUTIONS_TABLE = "contributions";
private final String DROP_TABLE_STATEMENT="DROP TABLE IF EXISTS %s"; private final String DROP_TABLE_STATEMENT="DROP TABLE IF EXISTS %s";

View file

@ -36,7 +36,8 @@ class BookMarkLocationDaoTest {
COLUMN_COMMONS_LINK, COLUMN_COMMONS_LINK,
COLUMN_LAT, COLUMN_LAT,
COLUMN_LONG, COLUMN_LONG,
COLUMN_PIC) COLUMN_PIC,
COLUMN_DESTROYED)
private val client: ContentProviderClient = mock() private val client: ContentProviderClient = mock()
private val database: SQLiteDatabase = mock() private val database: SQLiteDatabase = mock()
private val captor = argumentCaptor<ContentValues>() private val captor = argumentCaptor<ContentValues>()
@ -95,6 +96,7 @@ class BookMarkLocationDaoTest {
assertEquals(builder.build().wikidataLink, it.siteLinks.wikidataLink) assertEquals(builder.build().wikidataLink, it.siteLinks.wikidataLink)
assertEquals(builder.build().commonsLink, it.siteLinks.commonsLink) assertEquals(builder.build().commonsLink, it.siteLinks.commonsLink)
assertEquals("picName",it.pic) assertEquals("picName",it.pic)
assertEquals("placeDestroyed", it.destroyed)
} }
} }
} }
@ -147,7 +149,7 @@ class BookMarkLocationDaoTest {
assertTrue(testObject.updateBookmarkLocation(examplePlaceBookmark)) assertTrue(testObject.updateBookmarkLocation(examplePlaceBookmark))
verify(client).insert(eq(BASE_URI), captor.capture()) verify(client).insert(eq(BASE_URI), captor.capture())
captor.firstValue.let { cv -> captor.firstValue.let { cv ->
assertEquals(11, cv.size()) assertEquals(12, cv.size())
assertEquals(examplePlaceBookmark.name, cv.getAsString(COLUMN_NAME)) assertEquals(examplePlaceBookmark.name, cv.getAsString(COLUMN_NAME))
assertEquals(examplePlaceBookmark.longDescription, cv.getAsString(COLUMN_DESCRIPTION)) assertEquals(examplePlaceBookmark.longDescription, cv.getAsString(COLUMN_DESCRIPTION))
assertEquals(examplePlaceBookmark.label.text, cv.getAsString(COLUMN_LABEL_TEXT)) assertEquals(examplePlaceBookmark.label.text, cv.getAsString(COLUMN_LABEL_TEXT))
@ -158,6 +160,7 @@ class BookMarkLocationDaoTest {
assertEquals(examplePlaceBookmark.siteLinks.wikidataLink.toString(), cv.getAsString(COLUMN_WIKIDATA_LINK)) assertEquals(examplePlaceBookmark.siteLinks.wikidataLink.toString(), cv.getAsString(COLUMN_WIKIDATA_LINK))
assertEquals(examplePlaceBookmark.siteLinks.commonsLink.toString(), cv.getAsString(COLUMN_COMMONS_LINK)) assertEquals(examplePlaceBookmark.siteLinks.commonsLink.toString(), cv.getAsString(COLUMN_COMMONS_LINK))
assertEquals(examplePlaceBookmark.pic.toString(), cv.getAsString(COLUMN_PIC)) assertEquals(examplePlaceBookmark.pic.toString(), cv.getAsString(COLUMN_PIC))
assertEquals(examplePlaceBookmark.destroyed.toString(), cv.getAsString(COLUMN_DESTROYED))
} }
} }
@ -253,12 +256,18 @@ class BookMarkLocationDaoTest {
verify(database).execSQL(CREATE_TABLE_STATEMENT) verify(database).execSQL(CREATE_TABLE_STATEMENT)
} }
@Test
fun migrateTableVersionFrom_v12_to_v13() {
onUpdate(database, 12, 13)
verify(database).execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_destroyed STRING;")
}
private fun createCursor(rowCount: Int) = MatrixCursor(columns, rowCount).apply { private fun createCursor(rowCount: Int) = MatrixCursor(columns, rowCount).apply {
for (i in 0 until rowCount) { for (i in 0 until rowCount) {
addRow(listOf("placeName", "placeDescription","placeCategory", exampleLabel.text, exampleLabel.icon, addRow(listOf("placeName", "placeDescription","placeCategory", exampleLabel.text, exampleLabel.icon,
exampleUri, builder.build().wikipediaLink, builder.build().wikidataLink, builder.build().commonsLink, exampleUri, builder.build().wikipediaLink, builder.build().wikidataLink, builder.build().commonsLink,
exampleLocation.latitude, exampleLocation.longitude, "picName")) exampleLocation.latitude, exampleLocation.longitude, "picName", "placeDestroyed"))
} }
} }
} }