Search code examples
javaspringflywayspring-data-neo4j-4

Database migrations with Neo4j and Spring


I have this kind sample project HERE

In that sample data, I try to use Flyway to implement database migration with neo4j database. I can create and insert normal SQL with H2 database (I used H2 database in my sample project), but I dunno how to implement it with Neo4j graphdatabase.

I need to init data when application start. This is how I try to set my migration code :

public class V1_1__InitMaster implements SpringJdbcMigration  {
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    /*
    //Example using h2 database
    jdbcTemplate.execute("CREATE TABLE Unit ("
            + "code VARCHAR(30),"
            + "name VARCHAR(30),"
            + "value DOUBLE"
            + ");");

    jdbcTemplate.execute("INSERT INTO Unit ('ft','Feet',1);");

    //How I can save my init data to neo4j database in here?
    for(String[] unitString : initDataMaster.unitList){
        //I got list unitList here
    }
    */
}
}

I read this Page about flyway that can manage database migration with neo4j, and I looks some pages that explain about Flyway integration with Spring and Neo4j.

What I asked is, How to save my initialization Data and use Flyway to manage it and integrate it with Neo4j and Spring?


Solution

  • EDIT

    A new Spring Boot starter (for Liquigraph 3.x only) has been introduced to fully work with Spring Boot, the link at the end remains the reference to follow for now.

    All you have to do is use the starters liquigraph-spring-boot-starter and spring-boot-starter-jdbc, enable auto-configuration (via @EnableAutoConfiguration) and declare a provide at least liquigraph.url in application.properties).

    INITIAL ANSWER

    Liquigraph is designed for managing migrations with Neo4j. Depending on the version of Neo4j you want to support, you will either pick Liquigraph 2.x (for Neo4j 2.x) or Liquigraph 3.x (for Neo4j 3.x).

    The integration with Spring is straightforward, you can either pass a DataSource or a JDBC URI (and user/password) to the configuration builder and programmatically trigger the migration run.

    The documentation describes this here (not specific to Spring, that's the agnostic way to programmatically run migrations).

    The configuration could look like this (provided you define the DataSource somewhere accessible by this configuration class):

    @Configuration
    class MigrationConfiguration {
    
        @Bean
        public MethodInvokingFactoryBean liquigraph(org.liquigraph.core.configuration.Configuration liquigraphConfig) {
            MethodInvokingFactoryBean method = new MethodInvokingFactoryBean();
            method.setTargetObject(new Liquigraph());
            method.setTargetMethod("runMigrations");
            method.setArguments(new Object[] {liquigraphConfig});
            return method;
        }
    
        @Bean
        public org.liquigraph.core.configuration.Configuration configuration(DataSource dataSource) {
            return new ConfigurationBuilder()
                .withDataSource(dataSource)
                .withMasterChangelogLocation("changelog.xml")
                .withRunMode()
                .build();
        }
    }
    

    The full example is here: https://github.com/fbiville/liquigraph-spring-boot-example.