diff --git a/app/src/main/java/com/etesync/syncadapter/App.java b/app/src/main/java/com/etesync/syncadapter/App.java index b0433329..478de93d 100644 --- a/app/src/main/java/com/etesync/syncadapter/App.java +++ b/app/src/main/java/com/etesync/syncadapter/App.java @@ -40,7 +40,6 @@ import com.etesync.syncadapter.log.LogcatHandler; import com.etesync.syncadapter.log.PlainTextFormatter; import com.etesync.syncadapter.model.CollectionInfo; import com.etesync.syncadapter.model.JournalEntity; -import com.etesync.syncadapter.model.JournalModel; import com.etesync.syncadapter.model.Models; import com.etesync.syncadapter.model.ServiceDB; import com.etesync.syncadapter.model.ServiceEntity; @@ -70,9 +69,9 @@ import at.bitfire.ical4android.CalendarStorageException; import at.bitfire.vcard4android.ContactsStorageException; import io.requery.Persistable; import io.requery.android.sqlite.DatabaseSource; +import io.requery.meta.EntityModel; import io.requery.sql.Configuration; import io.requery.sql.EntityDataStore; -import io.requery.sql.TableCreationMode; import lombok.Cleanup; import lombok.Getter; import okhttp3.internal.tls.OkHostnameVerifier; @@ -229,13 +228,43 @@ public class App extends Application { public EntityDataStore getData() { if (dataStore == null) { // override onUpgrade to handle migrating to a new version - DatabaseSource source = new DatabaseSource(this, Models.DEFAULT, 3); + DatabaseSource source = new MyDatabaseSource(this, Models.DEFAULT, 3); Configuration configuration = source.getConfiguration(); dataStore = new EntityDataStore<>(configuration); } return dataStore; } + private static class MyDatabaseSource extends DatabaseSource { + MyDatabaseSource(Context context, EntityModel entityModel, int version) { + super(context, entityModel, version); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + super.onUpgrade(db, oldVersion, newVersion); + + if (oldVersion < 3) { + db.execSQL("PRAGMA foreign_keys=OFF;"); + + db.execSQL("CREATE TABLE new_Journal (id integer primary key autoincrement not null, deleted boolean not null, encryptedKey varbinary(255), info varchar(255), owner varchar(255), service integer, serviceModel integer, uid varchar(64) not null, foreign key (serviceModel) references Service (id) on delete cascade);"); + db.execSQL("CREATE TABLE new_Entry (id integer primary key autoincrement not null, content varchar(255), journal integer, uid varchar(64) not null, foreign key (journal) references Journal (id) on delete cascade);"); + + db.execSQL("INSERT INTO new_Journal SELECT id, deleted, encryptedKey, info, owner, service, serviceModel, uid from Journal;"); + db.execSQL("INSERT INTO new_Entry SELECT id, content, journal, uid from Entry;"); + + db.execSQL("DROP TABLE Journal;"); + db.execSQL("DROP TABLE Entry;"); + db.execSQL("ALTER TABLE new_Journal RENAME TO Journal;"); + db.execSQL("ALTER TABLE new_Entry RENAME TO Entry;"); + // Add back indexes + db.execSQL("CREATE UNIQUE INDEX journal_unique_together on Journal (serviceModel, uid);"); + db.execSQL("CREATE UNIQUE INDEX entry_unique_together on Entry (journal, uid);"); + db.execSQL("PRAGMA foreign_keys=ON;"); + } + } + } + // update from previous account settings private final static String PREF_VERSION = "version";