Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Make it possible to use pre-instantiated Java-based migrations #1062

Closed
guylabs opened this issue Jul 22, 2015 · 23 comments
Closed

API: Make it possible to use pre-instantiated Java-based migrations #1062

guylabs opened this issue Jul 22, 2015 · 23 comments

Comments

@guylabs
Copy link

@guylabs guylabs commented Jul 22, 2015

Hi,

as we are using Spring with Flyway we want to use other beans inside the SpringJdbcMigrations. Therefore we created a new resolver which retrieves the SpringJdbcMigrations from the Spring application context. This way we are able to define the migrations inside the application context and reference other beans we would need for this migration inside the SpringJdbcMigraiton with the dependency injection mechanisms of Spring (@Autowired). Please have a look at the linked pull request and the tests how this is done.

If this pull request is accepted and integrated, then I would also add some documentation to the website project of Flyway on how to use this new resolver.

Thanks and regards,

Guy

@kapil-malik
Copy link

@kapil-malik kapil-malik commented Nov 7, 2015

Hi,
This is very useful for us as well. The PR looks pretty cool. Any updates on how soon can it be part of flyway builds?
Till then we will override SpringJbdcMigrationResolver (like your code) as a workaround.

@guylabs
Copy link
Author

@guylabs guylabs commented Nov 13, 2015

@kapil-malik Thanks and yes this would be really useful.

@axelfontaine Thanks a lot for taking it up as a feature!

@axelfontaine axelfontaine added this to the Flyway 4.1 milestone Jan 19, 2016
@wimdeblauwe
Copy link

@wimdeblauwe wimdeblauwe commented Apr 7, 2016

+1 for this!

For those that cannot wait until this gets done: I have added a workaround on stackoverflow: http://stackoverflow.com/a/36474970/40064

BTW: If this is released, will there be an easy way to still have the SQL resolver as well to avoid the hacky workaround I had to do now?

@crstalli
Copy link

@crstalli crstalli commented May 8, 2016

@wimdeblauwe Thank you very much! That was extremely helpful!

@marbot
Copy link

@marbot marbot commented May 31, 2016

+1, that would be a very useful feature.

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Feb 7, 2017

See also #869

@axelfontaine axelfontaine modified the milestones: Flyway 5.0, Flyway 4.1 Feb 7, 2017
@geirgp
Copy link

@geirgp geirgp commented May 5, 2017

+1

3 similar comments
@tpischke
Copy link

@tpischke tpischke commented May 15, 2017

+1

@paolodenti
Copy link

@paolodenti paolodenti commented May 27, 2017

+1

@k-yosuke
Copy link

@k-yosuke k-yosuke commented Aug 7, 2017

+1

@zinefer
Copy link

@zinefer zinefer commented Jan 12, 2018

When I use Spring JPA flyway migrations seem to run before the application context has been instantiated ... Does anyone know of a way around this, or how I can instantiate my repositories inside a flyway migration?

@OldIMP
Copy link

@OldIMP OldIMP commented Jan 12, 2018

We use a workaround from https://github.com/avehlies/spring-beans-flyway2 But I really don't recommend injecting data repo in migrations since the domain could have been changed.

@zinefer
Copy link

@zinefer zinefer commented Jan 12, 2018

Yes, I've tried that technique... But in my project, flyway migrations are being run before the application context - I get Null exceptions.

@zinefer
Copy link

@zinefer zinefer commented Jan 12, 2018

Infact, if you take that project (spring-beans-flyway2) and add compile("org.springframework.boot:spring-boot-starter-data-jpa") to the dependencies in build.gradle it will fail with a null pointer exception

@OldIMP
Copy link

@OldIMP OldIMP commented Jan 12, 2018

Sry I forgot to mention our configuration:

@Configuration
class FlywayConfiguration {
    @Primary
    @Bean(name = "flywayInitializer")
    @DependsOn("springUtility") //or whatever your ApplicationContextAware is named
    FlywayMigrationInitializer flywayMigrationInitializer(){
      //your flyway init configuration...
    }
}

The trick here is to force FlywayMigrationInitializer to get instantiated after the ApplicationContextAware workaround

@zinefer
Copy link

@zinefer zinefer commented Jan 12, 2018

I will try that when I get home. Thanks so much.

EDIT: I did eventually get this working. See my SO answer here. Thanks everyone.

@purocean
Copy link

@purocean purocean commented Jan 20, 2018

+1

1 similar comment
@fladulima
Copy link

@fladulima fladulima commented Apr 17, 2018

+1

@idallas456
Copy link

@idallas456 idallas456 commented Dec 10, 2018

+1, this should really get fixed at some point.

@purocean
Copy link

@purocean purocean commented Dec 11, 2018

Now I use my simple Migration for app, very simple. I leave Flayway for long time.

https://github.com/purocean/kotlin-db-migration

  1. simple & light
  2. name based version
  3. support dry run
  4. support code migration and sql migration

Only support MySQL :(

@axelfontaine axelfontaine changed the title Use Spring ApplicationContext to resolve SpringJdbcMigrations Make it possible to use pre-instantiated Java-based migrations Dec 11, 2018
@axelfontaine axelfontaine changed the title Make it possible to use pre-instantiated Java-based migrations API: Make it possible to use pre-instantiated Java-based migrations Dec 11, 2018
axelfontaine pushed a commit to flyway/flywaydb.org that referenced this issue Dec 11, 2018
@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Dec 11, 2018

This has now been implemented generically using a new javaMigrations property in the API.

Spring users can use this to automatically use all JavaMigration Spring beans with Flyway:

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.springframework.context.ApplicationContext;

...
ApplicationContext applicationContext = ...; // obtain a reference to Spring's ApplicationContext.

Flyway flyway = Flyway.configure()
    .dataSource(url, user, password)
    // Add all Spring-instantiated JavaMigration beans
    .javaMigrations(applicationContext.getBeansOfType(JavaMigration.class).values().toArray(new JavaMigration[0]))
    .load();
flyway.migrate();
@guylabs
Copy link
Author

@guylabs guylabs commented Dec 11, 2018

@axelfontaine Thanks for incorporating the feedback! I will have a look at it when I'll work again with a Spring project. Maybe someones else in the thread is able to use it the way you implemented it. Always nice to see that feedback gets integrated!

@simonzhong1985
Copy link

@simonzhong1985 simonzhong1985 commented Jan 15, 2019

Sry I forgot to mention our configuration:

@Configuration
class FlywayConfiguration {
    @Primary
    @Bean(name = "flywayInitializer")
    @DependsOn("springUtility") //or whatever your ApplicationContextAware is named
    FlywayMigrationInitializer flywayMigrationInitializer(){
      //your flyway init configuration...
    }
}

The trick here is to force FlywayMigrationInitializer to get instantiated after the ApplicationContextAware workaround

This has now been implemented generically using a new javaMigrations property in the API.

Spring users can use this to automatically use all JavaMigration Spring beans with Flyway:

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.springframework.context.ApplicationContext;

...
ApplicationContext applicationContext = ...; // obtain a reference to Spring's ApplicationContext.

Flyway flyway = Flyway.configure()
    .dataSource(url, user, password)
    // Add all Spring-instantiated JavaMigration beans
    .javaMigrations(applicationContext.getBeansOfType(JavaMigration.class).values().toArray(new JavaMigration[0]))
    .load();
flyway.migrate();

@axelfontaine
Could you help to provide one example to show how to inject spring bean into class implementation of JavaMigration interface?

Thanks,
Simon

dohrayme pushed a commit to dohrayme/flyway that referenced this issue Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet