Migrate excerpt

This commit is contained in:
Stefan Niedermann 2020-05-23 09:49:25 +02:00
parent 9c8148ebbd
commit b2d07fd520

View file

@ -373,10 +373,29 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
} }
if (oldVersion < 15) { if (oldVersion < 15) {
// Rename a tmp_NOTES table. // Rename a tmp_NOTES table.
String tmpTableNotes = String.format("tmp_%s", table_notes); String tmpTableNotes = String.format("tmp_%s", "NOTES");
db.execSQL("ALTER TABLE " + table_notes + " RENAME TO " + tmpTableNotes); db.execSQL("ALTER TABLE NOTES RENAME TO " + tmpTableNotes);
createNotesTable(db); db.execSQL("CREATE TABLE " + table_notes + " ( " +
createCategoryTable(db); "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"REMOTEID INTEGER, " +
"ACCOUNT_ID INTEGER, " +
"STATUS VARCHAR(50), " +
"TITLE TEXT, " +
"MODIFIED INTEGER DEFAULT 0, " +
"CONTENT TEXT, " +
"FAVORITE INTEGER DEFAULT 0, " +
"CATEGORY INTEGER, " +
"ETAG TEXT," +
"EXCERPT TEXT NOT NULL DEFAULT '', " +
"FOREIGN KEY(CATEGORY) REFERENCES CATEGORIES(CATEGORY_ID), " +
"FOREIGN KEY(ACCOUNT_ID) REFERENCES ACCOUNTS(ID))");
DatabaseIndexUtil.createIndex(db, "NOTES", "REMOTEID", "ACCOUNT_ID", "STATUS", "FAVORITE", "CATEGORY", "MODIFIED");
db.execSQL("CREATE TABLE CATEGORIES(" +
"CATEGORY_ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"CATEGORY_ACCOUNT_ID INTEGER, " +
"CATEGORY_TITLE TEXT, " +
" UNIQUE( CATEGORY_ACCOUNT_ID , CATEGORY_TITLE))");
DatabaseIndexUtil.createIndex(db, "CATEGORIES", "CATEGORY_ID", "CATEGORY_ACCOUNT_ID", "CATEGORY_TITLE");
// A hashtable storing categoryTitle - categoryId Mapping // A hashtable storing categoryTitle - categoryId Mapping
// This is used to prevent too many searches in database // This is used to prevent too many searches in database
Hashtable<String, Integer> categoryTitleIdMap = new Hashtable<>(); Hashtable<String, Integer> categoryTitleIdMap = new Hashtable<>();
@ -385,33 +404,33 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
while (tmpNotesCursor.moveToNext()) { while (tmpNotesCursor.moveToNext()) {
String categoryTitle = tmpNotesCursor.getString(8); String categoryTitle = tmpNotesCursor.getString(8);
int accountId = tmpNotesCursor.getInt(2); int accountId = tmpNotesCursor.getInt(2);
int categoryId = 0; Integer categoryId;
if (categoryTitleIdMap.containsKey(categoryTitle) && categoryTitleIdMap.get(categoryTitle) != null) { if (categoryTitleIdMap.containsKey(categoryTitle) && categoryTitleIdMap.get(categoryTitle) != null) {
categoryId = categoryTitleIdMap.get(categoryTitle); categoryId = categoryTitleIdMap.get(categoryTitle);
} else { } else {
// The category does not exists in the database, create it. // The category does not exists in the database, create it.
categoryId = id++; categoryId = id++;
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(key_category_id, categoryId); values.put("CATEGORY_ID", categoryId);
values.put(key_category_account_id, accountId); values.put("CATEGORY_ACCOUNT_ID", accountId);
values.put(key_category_title, categoryTitle); values.put("CATEGORY_TITLE", categoryTitle);
db.insert(table_category, null, values); db.insert("CATEGORIES", null, values);
categoryTitleIdMap.put(categoryTitle, categoryId); categoryTitleIdMap.put(categoryTitle, categoryId);
} }
// Move the data in tmp_NOTES to NOTES // Move the data in tmp_NOTES to NOTES
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(key_id, tmpNotesCursor.getInt(0)); values.put("ID", tmpNotesCursor.getInt(0));
values.put(key_remote_id, tmpNotesCursor.getInt(1)); values.put("REMOTEID", tmpNotesCursor.getInt(1));
values.put(key_account_id, tmpNotesCursor.getInt(2)); values.put("ACCOUNT_ID", tmpNotesCursor.getInt(2));
values.put(key_status, tmpNotesCursor.getString(3)); values.put("STATUS", tmpNotesCursor.getString(3));
values.put(key_title, tmpNotesCursor.getString(4)); values.put("TITLE", tmpNotesCursor.getString(4));
values.put(key_modified, tmpNotesCursor.getLong(5)); values.put("MODIFIED", tmpNotesCursor.getLong(5));
values.put(key_content, tmpNotesCursor.getString(6)); values.put("CONTENT", tmpNotesCursor.getString(6));
values.put(key_favorite, tmpNotesCursor.getInt(7)); values.put("FAVORITE", tmpNotesCursor.getInt(7));
values.put(key_category, categoryId); values.put("CATEGORY", categoryId);
values.put(key_etag, tmpNotesCursor.getString(9)); values.put("ETAG", tmpNotesCursor.getString(9));
values.put(key_etag, tmpNotesCursor.getString(10)); values.put("EXCERPT", tmpNotesCursor.getString(10));
db.insert(table_notes, null, values); db.insert("NOTES", null, values);
} }
tmpNotesCursor.close(); tmpNotesCursor.close();
db.execSQL("DROP TABLE IF EXISTS " + tmpTableNotes); db.execSQL("DROP TABLE IF EXISTS " + tmpTableNotes);