1

Main question - this may seem like a basic flyway question and I might have (somehow) missed this during my research but - is it possible to access an applications services (spring configured) when attempting to migrate data using flyway? Few details below -

Additional details -

  • I know we cannot inject spring data services etc. (learnt from this SO question). And I understand this from a data access point of view.
  • But can we not use (by injection) any other application services either while using flyway (I searched for examples - but without luck, and no details given on flyway documentation either)
  • Let us say we cannot use any Spring services (and I find some way to work around that), can we access properties declared in application.properties / .yml (this does not appear possible either).

Putting the above in context of our requirement - we have added a couple of new fields to few tables and as part of the release we want to populate those columns with data. This requires us (or flyway) to execute the following algorithm -

  • Get data from first table.
  • Using some of the data from each row, lookup more data with an API call.
  • The URL of the API is environment specific (hence the third point above).
  • Update the data returned from the API into the newly added columns.
  • Repeat steps above for next table.

P.S. - I know, adding columns that depend on other columns in the same table is not in accordance with 3rd normal etc. but for reasons outside this post, it is required.

Tech Stack -

  1. Spring boot 1.3.x
  2. Flyway 4.0.3
  3. Using Java migration

Few examples I tried as below -

My flyway migration class is as below.

public class R__MigrationYeah implements SpringJdbcMigration {

  @Value("${mypath.subpath}") // this does not work !
  private String someStringIwannaUse;


  @Inject // this does not work either (even with Autowired or Const. injection)!
  private MyService myService;

}

I have seen some posts / blogs that have complicated details on how to configure flyways MigrationResolver or ConfigurationAware etc. - and not sure if they solve this problem (even if they do - it is a LOT of work just to write a quick migration script - is this the only way?).

Finally - I know I'm missing something because if we have to write flyway Java code without being able to use ANY existing application classes thru Spring, then it is no different than writing an independent migration project (therefore no value added by flyway other than making a DB connection available) - I'm sure this cannot be the case.

Any help would be great on this !

1
  • UPDATE - we finally ended up using the workaround solution (as in the last section in question above, and also in the answer below from @DanielKafer). Since this is not available out-of-box from flyway. P.S. not clear why the question or answers below are down-voted? Am new to SO, am I missing something? Aug 30, 2016 at 22:27

3 Answers 3

0

It is not possible to use dependency injection in a flyway migration.

The next version from flyway will support dependency injection from spring beans. See the Github issue for more details. On Stack Overflow is a workaround for the current version available.

-2

I think you want to use flyway more dynamically than it's designed for. Basically it's just for DB-Schema, you can do anything sql can do, but since it does it job in a repeatable, reliable, step-by-step way you wouldn't want any real business data in it. Flyway uses static scripts you provide, you can't have them dynamically change over time (it would protest by checksum not matching) or with "API-Calls".

-2

For that kind of stuff you can create your own Spring Boot app and use Flyway through it's Java API. Something along this lines.

@SpringBootApplication
@Import(ServiceConfig.class)
public class FlyWayApp implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(FlyWayApp.class, args);
    }

    @Value("${mypath.subpath}")
    private String someStringIwannaUse;


    @Autowired
    private MyService myService;


    @Override
    public void run(String... args) throws Exception {
        // Create the Flyway instance
        Flyway flyway = new Flyway();


        // Point it to the database
        flyway.setDataSource("jdbc:h2:file:./target/foobar", "sa", null);

        //Fetch data and create migration scripts needed by Flyway
        myService.createMigrationScripts();

        // Start the migration
        flyway.migrate();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.