Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run flyway migrations inside Java code during runtime

Tags:

java

jdbc

flyway

I want to be able to run Flyway migrations inside my Java code during runtime, is there a way of achieving this? I can't seem to be able to find it in the docs. I'm using a SQLite database (if this matters at all).

like image 334
Peter Zhu Avatar asked Jan 01 '23 18:01

Peter Zhu


1 Answers

Flyway::migrate()

Call Flyway::migrate.

To quote the documentation:

package foobar;

import org.flywaydb.core.Flyway;

public class App {
    public static void main(String[] args) {

        // Create the Flyway instance and point it to the database
        Flyway flyway = 
                Flyway.configure()
                      .dataSource( "jdbc:h2:file:./target/foobar" , "scott" , "tiger" )  // (url, user, password)
                      .load()                                                            // Returns a `Flyway` object.
        ;

        // Start the migration
        flyway.migrate();

    }
}
like image 60
Basil Bourque Avatar answered Jan 14 '23 06:01

Basil Bourque