mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 04:33:54 -04:00
#1449 migrate from 2.5.3 -> 3.0
This commit is contained in:
parent
3a449b149f
commit
085bd8820b
@ -188,9 +188,9 @@ def branchName = System.getenv('TRAVIS_PULL_REQUEST') ?: "false" == "false"
|
||||
def buildNumber = System.getenv('TRAVIS_BUILD_NUMBER') ?: "dev"
|
||||
|
||||
ext {
|
||||
versionMajor = 2
|
||||
versionMinor = 5
|
||||
versionPatch = 1
|
||||
versionMajor = 3
|
||||
versionMinor = 0
|
||||
versionPatch = 0
|
||||
}
|
||||
|
||||
private String generateVersionName() {
|
||||
|
@ -51,7 +51,7 @@ public class KiwixDatabaseTest {
|
||||
@Test
|
||||
@SuppressWarnings("CharsetObjectCanBeUsed") // Standard charset throws exception on < API19
|
||||
public void testMigrateDatabase() throws IOException {
|
||||
KiwixDatabase kiwixDatabase = new KiwixDatabase(context, null, null);
|
||||
KiwixDatabase kiwixDatabase = new KiwixDatabase(context, null, null, null, null);
|
||||
kiwixDatabase.recreate();
|
||||
String testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c";
|
||||
String[] testBookmarks = new String[] { "Test1", "Test2", "Test3" };
|
||||
|
@ -36,13 +36,16 @@ import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
||||
import org.kiwix.kiwixmobile.data.local.dao.BookDao;
|
||||
import org.kiwix.kiwixmobile.data.local.dao.BookmarksDao;
|
||||
import org.kiwix.kiwixmobile.data.local.dao.NetworkLanguageDao;
|
||||
import org.kiwix.kiwixmobile.data.local.dao.RecentSearchDao;
|
||||
import org.kiwix.kiwixmobile.data.local.entity.BookDatabaseEntity;
|
||||
import org.kiwix.kiwixmobile.data.local.entity.Bookmark;
|
||||
import org.kiwix.kiwixmobile.data.local.entity.LibraryDatabaseEntity;
|
||||
import org.kiwix.kiwixmobile.data.local.entity.NetworkLanguageDatabaseEntity;
|
||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookmarksDao;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewRecentSearchDao;
|
||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
|
||||
import org.kiwix.kiwixmobile.utils.UpdateUtils;
|
||||
|
||||
@ -51,17 +54,24 @@ import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
||||
@Singleton
|
||||
public class KiwixDatabase extends SquidDatabase {
|
||||
|
||||
private static final int VERSION = 17;
|
||||
private static final int FINAL = 18;//3.0.0
|
||||
private static final int VERSION = FINAL;
|
||||
private final Context context;
|
||||
private final NewBookDao bookDao;
|
||||
private final NewLanguagesDao languagesDao;
|
||||
private final NewBookmarksDao bookmarksDao;
|
||||
private final NewRecentSearchDao recentSearchDao;
|
||||
|
||||
@Inject
|
||||
public KiwixDatabase(Context context, NewBookDao bookDao, NewLanguagesDao languagesDao) {
|
||||
public KiwixDatabase(Context context, NewBookDao bookDao, NewLanguagesDao languagesDao,
|
||||
NewBookmarksDao bookmarksDao,
|
||||
NewRecentSearchDao recentSearchDao) {
|
||||
super(context);
|
||||
this.context = context;
|
||||
this.bookDao = bookDao;
|
||||
this.languagesDao = languagesDao;
|
||||
this.bookmarksDao = bookmarksDao;
|
||||
this.recentSearchDao = recentSearchDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -72,25 +82,11 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
@Override
|
||||
protected Table[] getTables() {
|
||||
return new Table[] {
|
||||
RecentSearch.TABLE,
|
||||
Bookmark.TABLE,
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onUpgrade(SQLiteDatabaseWrapper db, int oldVersion, int newVersion) {
|
||||
if (newVersion >= 16) { //2.5 attempt reading values from old db before they get dropped
|
||||
try {
|
||||
bookDao.migrationInsert(new BookDao(this).getBooks());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
languagesDao.insert(new NetworkLanguageDao(this).getFilteredLanguages());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
switch (oldVersion) {
|
||||
case 1:
|
||||
case 2:
|
||||
@ -135,12 +131,35 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
tryAddColumn(Bookmark.ZIM_FILE_PATH);
|
||||
tryAddColumn(Bookmark.FAVICON);
|
||||
migrateBookmarksVersion16();
|
||||
case 16:
|
||||
try {
|
||||
bookDao.migrationInsert(new BookDao(this).getBooks());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
languagesDao.insert(new NetworkLanguageDao(this).getFilteredLanguages());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
tryDropTable(BookDatabaseEntity.TABLE);
|
||||
tryDropTable(NetworkLanguageDatabaseEntity.TABLE);
|
||||
tryDropTable(LibraryDatabaseEntity.TABLE);
|
||||
case 16:
|
||||
new BookmarksDao(this).processBookmark(UpdateUtils::reformatProviderUrl);
|
||||
//TODO MIGRATIONS BEFORE 3.0
|
||||
case 17:
|
||||
try {
|
||||
final BookmarksDao oldBookmarksDao = new BookmarksDao(this);
|
||||
oldBookmarksDao.processBookmark(UpdateUtils::reformatProviderUrl);
|
||||
this.bookmarksDao.migrationInsert(oldBookmarksDao.getBookmarks(false));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
tryDropTable(Bookmark.TABLE);
|
||||
try {
|
||||
recentSearchDao.migrationInsert(new RecentSearchDao(this).getRecentSearches());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
tryDropTable(RecentSearch.TABLE);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -40,23 +40,6 @@ public class BookmarksDao {
|
||||
this.kiwixDatabase = kiwixDatabase;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Url of the bookmarks from the current Zim file.
|
||||
*/
|
||||
public List<String> getCurrentZimBookmarksUrl() {
|
||||
ArrayList<String> bookmarksUrl = new ArrayList<>();
|
||||
try (SquidCursor<Bookmark> bookmarkCursor = kiwixDatabase.query(Bookmark.class,
|
||||
Query.selectDistinct(Bookmark.BOOKMARK_URL)
|
||||
.where(Bookmark.ZIM_ID.eq(ZimContentProvider.getId())
|
||||
.or(Bookmark.ZIM_NAME.eq(ZimContentProvider.getName())))
|
||||
.orderBy(Bookmark.BOOKMARK_TITLE.asc()))) {
|
||||
while (bookmarkCursor.moveToNext()) {
|
||||
bookmarksUrl.add(bookmarkCursor.get(Bookmark.BOOKMARK_URL));
|
||||
}
|
||||
}
|
||||
return bookmarksUrl;
|
||||
}
|
||||
|
||||
public void saveBookmark(Bookmark bookmark) {
|
||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
||||
@ -94,18 +77,6 @@ public class BookmarksDao {
|
||||
return bookmarks;
|
||||
}
|
||||
|
||||
public void deleteBookmarks(List<Bookmark> bookmarks) {
|
||||
for (Bookmark bookmark : bookmarks) {
|
||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteBookmark(Bookmark bookmark) {
|
||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
||||
}
|
||||
|
||||
public void processBookmark(StringOperation operation) {
|
||||
try (SquidCursor<Bookmark> bookmarkCursor = kiwixDatabase.query(Bookmark.class,
|
||||
Query.select(Bookmark.ID, Bookmark.BOOKMARK_URL))) {
|
||||
|
@ -22,7 +22,6 @@ import com.yahoo.squidb.sql.Query;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
||||
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
||||
|
||||
@ -43,43 +42,14 @@ public class RecentSearchDao {
|
||||
/**
|
||||
* Returns a distinct enumeration of the {@code NUM_RECENT_RESULTS} most recent searches.
|
||||
*/
|
||||
public List<String> getRecentSearches() {
|
||||
SquidCursor<RecentSearch> searchCursor = mDb.query(
|
||||
RecentSearch.class,
|
||||
Query.selectDistinct(RecentSearch.SEARCH_STRING)
|
||||
.where(RecentSearch.ZIM_I_D.eq(ZimContentProvider.getId()))
|
||||
.orderBy(RecentSearch.ID.desc())
|
||||
.limit(NUM_RECENT_RESULTS));
|
||||
List<String> result = new ArrayList<>();
|
||||
try {
|
||||
public List<RecentSearch> getRecentSearches() {
|
||||
List<RecentSearch> result = new ArrayList<>();
|
||||
try (SquidCursor<RecentSearch> searchCursor = mDb.query(
|
||||
RecentSearch.class, Query.select())) {
|
||||
while (searchCursor.moveToNext()) {
|
||||
result.add(searchCursor.get(RecentSearch.SEARCH_STRING));
|
||||
result.add(new RecentSearch(searchCursor));
|
||||
}
|
||||
} finally {
|
||||
searchCursor.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save {@code searchString} as the most recent search.
|
||||
*/
|
||||
public void saveSearch(String searchString) {
|
||||
mDb.persist(
|
||||
new RecentSearch().setSearchString(searchString).setZimID(ZimContentProvider.getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all entries that exactly matches {@code searchString}
|
||||
*/
|
||||
public void deleteSearchString(String searchString) {
|
||||
mDb.deleteWhere(RecentSearch.class, RecentSearch.SEARCH_STRING.eq(searchString));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all entries.
|
||||
*/
|
||||
public void deleteSearchHistory() {
|
||||
mDb.deleteWhere(RecentSearch.class, RecentSearch.ID.isNotNull());
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import io.objectbox.Box
|
||||
import io.objectbox.kotlin.query
|
||||
import org.kiwix.kiwixmobile.bookmark.BookmarkItem
|
||||
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
||||
import org.kiwix.kiwixmobile.data.local.entity.Bookmark
|
||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookmarkEntity
|
||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookmarkEntity_
|
||||
import javax.inject.Inject
|
||||
@ -58,4 +59,8 @@ class NewBookmarksDao @Inject constructor(val box: Box<BookmarkEntity>) {
|
||||
equal(BookmarkEntity_.bookmarkUrl, bookmarkUrl)
|
||||
}.remove()
|
||||
}
|
||||
|
||||
fun migrationInsert(bookmarks: MutableList<Bookmark>) {
|
||||
box.put(bookmarks.map(::BookmarkEntity))
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.dao
|
||||
import io.objectbox.Box
|
||||
import io.objectbox.kotlin.query
|
||||
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch
|
||||
import org.kiwix.kiwixmobile.database.newdb.entities.RecentSearchEntity
|
||||
import org.kiwix.kiwixmobile.database.newdb.entities.RecentSearchEntity_
|
||||
import javax.inject.Inject
|
||||
@ -51,6 +52,10 @@ class NewRecentSearchDao @Inject constructor(val box: Box<RecentSearchEntity>) {
|
||||
box.removeAll()
|
||||
}
|
||||
|
||||
fun migrationInsert(recentSearches: MutableList<RecentSearch>) {
|
||||
box.put(recentSearches.map(::RecentSearchEntity))
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val NUM_RECENT_RESULTS = 5
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.entities
|
||||
import io.objectbox.annotation.Entity
|
||||
import io.objectbox.annotation.Id
|
||||
import org.kiwix.kiwixmobile.bookmark.BookmarkItem
|
||||
import org.kiwix.kiwixmobile.data.local.entity.Bookmark
|
||||
|
||||
@Entity
|
||||
data class BookmarkEntity(
|
||||
@ -40,4 +41,14 @@ data class BookmarkEntity(
|
||||
item.bookmarkTitle,
|
||||
item.favicon
|
||||
)
|
||||
|
||||
constructor(bookmark: Bookmark) : this(
|
||||
0,
|
||||
bookmark.zimId,
|
||||
bookmark.zimName,
|
||||
bookmark.zimFilePath,
|
||||
bookmark.bookmarkUrl,
|
||||
bookmark.bookmarkTitle,
|
||||
bookmark.favicon
|
||||
)
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.entities
|
||||
import io.objectbox.annotation.Entity
|
||||
import io.objectbox.annotation.Id
|
||||
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch
|
||||
|
||||
@Entity
|
||||
data class RecentSearchEntity(
|
||||
@ -31,4 +32,10 @@ data class RecentSearchEntity(
|
||||
searchTerm = searchTerm,
|
||||
zimId = ZimContentProvider.getId()
|
||||
)
|
||||
|
||||
constructor(recentSearch: RecentSearch) : this(
|
||||
0,
|
||||
recentSearch.searchString,
|
||||
recentSearch.zimID
|
||||
)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user