Skip to content

Commit

Permalink
fix: synchronise the DBPersistentManager.checkForMigrations method
Browse files Browse the repository at this point in the history
  • Loading branch information
1abhishekpandey committed Feb 5, 2024
1 parent 00dd64e commit 771d410
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
* Helper class for SQLite operations
Expand Down Expand Up @@ -101,6 +103,7 @@ class DBPersistentManager/* extends SQLiteOpenHelper*/ {
final Queue<Message> queue = new LinkedList<>();
DBInsertionHandlerThread dbInsertionHandlerThread;
private Persistence persistence;
private final Lock lock = new ReentrantLock();

private DBPersistentManager(Application application,
PersistenceProvider.Factory persistenceProviderFactory) {
Expand Down Expand Up @@ -315,7 +318,12 @@ void getEventsFromDB(Map<Integer, Integer> messageIdStatusMap,//(id (row_id), st
}
Cursor cursor;
synchronized (DB_LOCK) {
cursor = persistence.rawQuery(selectSQL, null);
lock.lock();
try {
cursor = persistence.rawQuery(selectSQL, null);
} finally {
lock.unlock();
}
}
if (!cursor.moveToFirst()) {
RudderLogger.logInfo("DBPersistentManager: fetchEventsFromDB: DB is empty");
Expand Down Expand Up @@ -506,26 +514,27 @@ private boolean checkIfColumnExists(String newColumn) {

void checkForMigrations() {
Runnable runnable = () -> {
lock.lock();
try {
synchronized (DB_LOCK) {
boolean isNewColumnAdded = false;
if (!checkIfColumnExists(STATUS_COL)) {
RudderLogger.logDebug("DBPersistentManager: checkForMigrations: Status column doesn't exist in the events table, hence performing the migration now");
performMigration(STATUS_COL);
isNewColumnAdded = true;
}
if (!checkIfColumnExists(DM_PROCESSED_COL)) {
RudderLogger.logDebug("DBPersistentManager: checkForMigrations: dm_processed column doesn't exist in the events table, hence performing the migration now");
performMigration(DM_PROCESSED_COL);
isNewColumnAdded = true;
}
if (!isNewColumnAdded) {
RudderLogger.logDebug("DBPersistentManager: checkForMigrations: Status and dm_processed column exists in the table already, hence no migration required");
}
boolean isNewColumnAdded = false;
if (!checkIfColumnExists(STATUS_COL)) {
RudderLogger.logDebug("DBPersistentManager: checkForMigrations: Status column doesn't exist in the events table, hence performing the migration now");
performMigration(STATUS_COL);
isNewColumnAdded = true;
}
if (!checkIfColumnExists(DM_PROCESSED_COL)) {
RudderLogger.logDebug("DBPersistentManager: checkForMigrations: dm_processed column doesn't exist in the events table, hence performing the migration now");
performMigration(DM_PROCESSED_COL);
isNewColumnAdded = true;
}
if (!isNewColumnAdded) {
RudderLogger.logDebug("DBPersistentManager: checkForMigrations: Status and dm_processed column exists in the table already, hence no migration required");
}
} catch (SQLiteDatabaseCorruptException | ConcurrentModificationException |
NullPointerException ex) {
RudderLogger.logError(DBPERSISTENT_MANAGER_CHECK_FOR_MIGRATIONS_TAG + ex.getLocalizedMessage());
} finally {
lock.unlock();
}
};
// Need to perform db operations on a separate thread to support strict mode.
Expand Down

0 comments on commit 771d410

Please sign in to comment.