Catch no such column errors in TasksXmlExporter and try to recover

pull/14/head
Sam Bosley 12 years ago
parent a496e2e94c
commit da6dda9181

@ -14,12 +14,14 @@ import org.xmlpull.v1.XmlSerializer;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.util.Xml; import android.util.Xml;
import android.widget.Toast; import android.widget.Toast;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.data.AbstractDatabase;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.PropertyVisitor; import com.todoroo.andlib.data.Property.PropertyVisitor;
@ -31,6 +33,7 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
@ -69,6 +72,7 @@ public class TasksXmlExporter {
private final Context context; private final Context context;
private int exportCount = 0; private int exportCount = 0;
private XmlSerializer xml; private XmlSerializer xml;
private final Database database = PluginServices.getDatabase();
private final TaskService taskService = PluginServices.getTaskService(); private final TaskService taskService = PluginServices.getTaskService();
private final MetadataService metadataService = PluginServices.getMetadataService(); private final MetadataService metadataService = PluginServices.getMetadataService();
private final ExceptionService exceptionService = PluginServices.getExceptionService(); private final ExceptionService exceptionService = PluginServices.getExceptionService();
@ -175,9 +179,27 @@ public class TasksXmlExporter {
fos.close(); fos.close();
} }
@SuppressWarnings("nls")
private void serializeTasks() throws IOException { private void serializeTasks() throws IOException {
TodorooCursor<Task> cursor = taskService.query(Query.select( TodorooCursor<Task> cursor;
try {
cursor = taskService.query(Query.select(
Task.PROPERTIES).orderBy(Order.asc(Task.ID))); Task.PROPERTIES).orderBy(Order.asc(Task.ID)));
} catch (SQLiteException e) {
String message = e.getMessage().toLowerCase();
if (message.contains("no such column") && message.contains("tasks.commentCount")) {
try {
database.getDatabase().execSQL("ALTER TABLE " + Task.TABLE.name + " ADD " +
Task.COMMENT_COUNT.accept(new AbstractDatabase.SqlConstructorVisitor(), null) + " DEFAULT 0");
} catch (SQLiteException e2) {
// Suppress, column creation failed. Just try the query again
}
cursor = taskService.query(Query.select(
Task.PROPERTIES).orderBy(Order.asc(Task.ID)));
} else {
throw e;
}
}
try { try {
Task task = new Task(); Task task = new Task();
int length = cursor.getCount(); int length = cursor.getCount();

@ -69,6 +69,11 @@ public final class PluginServices {
return instance; return instance;
} }
public static Database getDatabase() {
getInstance().database.openForWriting();
return getInstance().database;
}
public static TaskService getTaskService() { public static TaskService getTaskService() {
getInstance().database.openForWriting(); getInstance().database.openForWriting();
return getInstance().taskService; return getInstance().taskService;

Loading…
Cancel
Save