0

I need to assure data migration using mongock. The @ChangeUnit class holds the logic for migration. It has a field annotated with @Value which is always null, even though I properly initialized in application.properties:

mongock.migration-scan-package=my.package
login-secret=test

Then the MigrationConfiguration looks as follows:

@ChangeUnit(id = "test", order = "001", author = "test")
@RequiredArgsConstructor
@Configuration
public class InitUsersChangeLog {

  private final MyService service;
  private final MongoTemplate template;

  @Value("${login-secret}")
  private String LOGIN;

  @Execution
  public void initUser() {
    service.create(User.builder().login(LOGIN).build());
  }
}

Main class:

@EnableMongock
@SpringBootApplication
public class MailServiceApplication {...}

My assumption is that this value is not injected properly into the MongockConfiguration bean. I tried to configure the bean manually (without using mongock.migration-scan-package=my.package) in the properties, but with no success.

obolenskaya00
  • 29
  • 1
  • 6

3 Answers3

1

As Mongock currently doesn't support @Value annotation you can try to use getProperty method from Environment bean. Environment bean can be injected same as other beans using constructor or Lombok annotations.

You want to change this:

@Value("your.key.property")

to that:

private final Environment env;
  
public void method(){
  env.getProperty("your.key.property")
}
MichalJ
  • 36
  • 4
0

Mongock currently no supports @value injection via field o method parameter. We will provide that in a future minor release within version 5, but we can't give you dates, yet.

Mongock team
  • 1,188
  • 5
  • 9
0

Extending MichalJ's answer, which is absolutely valid. I would like to add that the changeUnits are not retrieved by Mongock via Springboot, they are processed by Mongock independently. So the annotation @Configuration, @Component, etc. won't be taken into account and they could even be damaging.

Related to that, this code won't work, at least not in a near future:

  @Value("${login-secret}")
  private String LOGIN;

First, as said, Mongock doesn't support value currently, but the first approach will require the constructor parameter to have that @Value("${login-secret}"), not at the field level.

Mongock team
  • 1,188
  • 5
  • 9