Requery: Fix database to have the correct constraints (on upgrade).

Requery doesn't automatically update column constraints, and there was
an issue with it applying indexes before adding the new columns which
was also causing troubles. This commit, while ugly, just manually
updates the database using raw SQL to what we expect it to be.
pull/14/head
Tom Hacohen 7 years ago
parent e2f206e02e
commit ae08510729

@ -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<Persistable> 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";

Loading…
Cancel
Save