mercredi 15 septembre 2021

Best way to handle temporary code implementation in java

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??

Aucun commentaire:

Enregistrer un commentaire