3

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?

fbiville
  • 8,407
  • 7
  • 51
  • 79
David Vincent
  • 634
  • 1
  • 8
  • 33
  • 1
    I'm not sure about flyway migration but have you looked at https://github.com/fbiville/liquigraph? It's inspired by Liquibase and is specific to Neo4j migrations. – digx1 Nov 15 '16 at 00:41
  • Yes, i am. But the liquigraph lack of documentation I think, and I need Java configuration + java sample code. Its hard to understand that documentation. At first, I want to use it, but after check the documentation and try to look deeper, I'm not sure how to use it. – David Vincent Nov 15 '16 at 03:54
  • I thought it was reasonably documented: http://fbiville.github.io/liquigraph/latest/index.html. Is there something more detailed you need? – digx1 Nov 15 '16 at 03:56
  • Yes, is there any documentation for Java Annotation? Because I have a lot of data to be inserted, and to do that, Java Annotation is more readable and more easy to custom. – David Vincent Nov 15 '16 at 04:24
  • @DavidVincent feel free to open issues in Liquigraph to point out the areas that are unclear in the documentation. We'll do our best to improve it! – fbiville Nov 15 '16 at 13:42
  • I edited the initial answer. From now, Liquigraph 3.x fully supports Spring Boot via its own Spring Boot starter, the configuration becomes trivial. – fbiville Dec 30 '16 at 00:39

1 Answers1

9

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.

fbiville
  • 8,407
  • 7
  • 51
  • 79
  • Woah, this is so Cool!! Is it support JPA QueryBuilder too? It will be awesome if JPA queryBuilder also support it. Is changelog only support XML? Is there any example about Java class changelog? – David Vincent Nov 16 '16 at 06:11
  • The preferred way is to use XML at the moment. You could define Java instances of Liquigraph model directly (see https://github.com/fbiville/liquigraph/blob/master/liquigraph-core/src/main/java/org/liquigraph/core/model/Changelog.java) but that's not recommended. JPA QueryBuilder support is not planned. We could maybe see if we could integrate with JCypher in the future (https://github.com/Wolfgang-Schuetzelhofer/jcypher/wiki/Query-DSL-Expressions). – fbiville Nov 16 '16 at 12:13
  • Ahh, okay. I will try to write all data on XML. Thank you a lot @Rolf – David Vincent Nov 17 '16 at 03:38