2

I have an application running on production and I want to do a database level schema change for my application without a downtime (i.e need to migrate old data to the new application also refer this for more information).

To handle the data operations in my application I have a java class like below.

public class MongoDBDataLayerImpl implements <some_class> {
   @Override
   public void addElement(){
       //code to do inserting elements
   }

   @Override
   public void deleteElement(){
       //code to perform deletion
   }
   
   @Override
   public void updateElement(){
       //code to perform update
   }

   @Override
   public void readElement(){
       //code to perform read
   }

   /* Note: I have a many more methods like this in my actual implementation,
            I just added this code for the clarity and simplicity.
   */
}

So in order to do the schema change I'm planning to do plug a temporary change(which should be easily unpluggable) to the above implementation, so that it's supporting both schema models for temporarily.

For this I'm thought of having a status of data migration in the DB side and use it inside my implementation to check, whether I should use new schema model or old model for that particular data.

So there I may need to have similar set of if conditions or switch statements inside the above methods to perform this for example see the below.

@Override
public void addElement(){
    If (migrationStatus == 'migrated'){
        // use new schema
    }
    else if (migrationStatus == 'migrating'){
        //wait for migration completed and recall this function
    }
    else:
        // use old schema
}

So my question is, is there a proper way (kind of design pattern) to do this implementation rather than using If conditions like above?? (so this should be easily revertible)

What are the best practices for this??

Vajira Prabuddhaka
  • 852
  • 4
  • 13
  • 34
  • 1
    In general, database have version (for android see https://stackoverflow.com/questions/8133597/android-upgrading-db-version-and-adding-new-table ) and it is checked each time the database is initialized or updated. You shall also follow this design pattern to avoid any data loss. It is also my advice that you either use the updated database or the old one. Don't let the user be in "migrating" state as it will cause issues like data loss or column not found errors. – Abishek Stephen Sep 16 '21 at 05:30
  • @AbishekStephen for one particular user, the migration only takes few seconds, so that's why I decided to have it migrated and wait for completion.. – Vajira Prabuddhaka Sep 16 '21 at 05:34
  • I see. My advice was in general database migration scope and it is adaptable to future migrations and/or modifications as well. If you are confident the migration will be quick in all client machines, then you can do it like this. In the end, it all comes to the question, "Well, does it work without an issue?" – Abishek Stephen Sep 16 '21 at 05:42

0 Answers0