Skip to content

Commit

Permalink
Fix error in db migration from version 1.5
Browse files Browse the repository at this point in the history
Adds a missing column name in table creation during migration, an
invalid column name in a select and also a race when migration was just
about to be started but a different accessor already tried to select
from the db.
  • Loading branch information
saemy committed Jun 2, 2018
1 parent 38b7eb1 commit 7a46f89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface DbCallback<T> {
* unlock it 2x to release it)
*/
private final Lock mWriteLock;
private boolean hasInitialWriteLock = true;

{
final ReadWriteLock lock = new ReentrantReadWriteLock(true);
Expand All @@ -71,6 +72,9 @@ public LockableDatabase(@NonNull final Context context,
@NonNull final SchemaDefinition schemaDefinition) {
mContext = context;
mSchemaDefinition = schemaDefinition;

// Initial write lock s.t. no access happens prior to opening the database.
lockWrite();
}

/**
Expand Down Expand Up @@ -159,7 +163,11 @@ private <T> T execute(final boolean transactional, @NonNull final DbCallback<T>
}

public void open(String dbName) {
lockWrite();
if (!hasInitialWriteLock) {
lockWrite();
}
hasInitialWriteLock = false;

try {
mDb = mContext.openOrCreateDatabase(dbName, Context.MODE_PRIVATE, null);

Expand All @@ -174,7 +182,7 @@ public void open(String dbName) {
}

public boolean isOpen() {
return mDb != null;
return mDb != null && mDb.isOpen();
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void moveHostsToUsers(@NonNull SQLiteDatabase db) {
// To avoid circular dependencies, we store the favorite userIds in a static field which is
// later processed by the account migration helper.
List<Integer> favoriteUserIds = new ArrayList<>();
try (Cursor cursor = db.query("hosts", new String[]{ "id" }, null, null, null, null, null)) {
try (Cursor cursor = db.query("hosts", new String[]{ "_id" }, null, null, null, null, null)) {
if (cursor.moveToFirst()) {
do {
final int userId = cursor.getInt(0);
Expand All @@ -86,8 +86,9 @@ void createFeedbackTable(@NonNull SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS feedback");
db.execSQL("CREATE TABLE feedback (" +
"id INTEGER NOT NULL, " +
"sender_id INTEGER NOT NULL, " +
"recipient_id INTEGER NOT NULL, " +
"sender_id INTEGER NOT NULL, " +
"sender_fullname TEXT, " +
"relation INTEGER, " +
"rating INTEGER, " +
"meeting_date INTEGER, " +
Expand Down

0 comments on commit 7a46f89

Please sign in to comment.