javaspringmongodbspring-data-jpamongock

What to do when name of class and collection used in Mongock migration file has changed?


I have an app in Spring Boot and use Mogock with MongoDB. Recently, I made a big refactoring and changed many class names and collection names. What I used to have before refactoring is an entity FooDb:

@Document("foo")
public class FooDb {
  @Id
  private String id;
  ...
}

and some interface:

@Repository
public interface FooRepository extends MongoRepository<FooDb, String> { ... }

and migration file:

@ChangeUnit(order="001, ...)
public class CreateFooChangelog {
  ...
  @Execution
  public void migration(FooRepository repository) {
     repository.save(...);
  }
}

During refactoring I only changed names of collections and classes. Their structure is the same:

@Document("bar")
public class BarDb {
  @Id
  private String id;
  ...
}

@Repository
public interface BarRepository extends MongoRepository<BarDb, String> { ... }

However, now I am not sure what to do with existing migration file. Is it good idea to modify existing migration files? I will definitely need to modify it because Foo* classes no longer exist in code.

I can think of solution like this: if there exists a foo collection it needs to be renamed to bar and if not, I need to initialize bar with some default data. Then I could try to modify this migration file like this:

@ChangeUnit(order="001", ...)
public class CreateBarChangelog {
  ...
  @Execution
  public void migration(BarRepository repository) {
     // if foo exists, rename it to bar
     // else: repository.save(...);
  }
}

Is this the right approach in this scenario or is there a better solution?


Solution

  • There are a couple of aspects to discuss.

    In conclusion, we suggest.

    1. No change the existing changeUnits
    2. Perform the renaming in a changeUnit as part of the migration steps
    3. Move the deprecated classes to an independent library and bring it as a maven/gradle dependency