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 · 22 comments

Comments

Projects
None yet
@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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

crstalli commented May 8, 2016

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

@marbot

This comment has been minimized.

marbot commented May 31, 2016

+1, that would be a very useful feature.

@axelfontaine

This comment has been minimized.

Contributor

axelfontaine commented Feb 7, 2017

See also #869

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

@geirgp

This comment has been minimized.

geirgp commented May 5, 2017

+1

3 similar comments
@tpischke

This comment has been minimized.

tpischke commented May 15, 2017

+1

@paolodenti

This comment has been minimized.

paolodenti commented May 27, 2017

+1

@K-Yosuke

This comment has been minimized.

K-Yosuke commented Aug 7, 2017

+1

@zinefer

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

purocean commented Jan 20, 2018

+1

1 similar comment
@fladulima

This comment has been minimized.

fladulima commented Apr 17, 2018

+1

@idallas456

This comment has been minimized.

idallas456 commented Dec 10, 2018

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

@purocean

This comment has been minimized.

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 from Use Spring ApplicationContext to resolve SpringJdbcMigrations to Make it possible to use pre-instantiated Java-based migrations Dec 11, 2018

@axelfontaine axelfontaine changed the title from Make it possible to use pre-instantiated Java-based migrations to 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

This comment has been minimized.

Contributor

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

This comment has been minimized.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment