使用Neo4j和Spring进行数据库迁移

时间:2016-11-14 07:49:29

标签: java spring flyway spring-data-neo4j-4

我有这样的示例项目Graeme Pyle's answer's jsfiddle above

在该示例数据中,我尝试使用HERE来实现与neo4j数据库的数据库迁移。我可以使用H2数据库创建和插入普通的SQL(我在我的示例项目中使用了H2数据库),但我不知道如何使用Neo4j graphdatabase实现它。

我需要在应用程序启动时初始化数据。这就是我尝试设置迁移代码的方法:

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
    }
    */
}
}

我读了这篇Flyway关于可以使用neo4j管理数据库迁移的flyway,我看了一些解释Flyway与Spring和Neo4j集成的页面。

我问的是,如何保存我的初始化数据并使用Flyway来管理它并将其与Neo4j和Spring集成?

1 个答案:

答案 0 :(得分:4)

修改

引入了一个新的Spring Boot启动程序(仅适用于Liquigraph 3.x )以完全使用Spring Boot,最后的链接仍然是现在的参考。

您所要做的就是使用初学者liquigraph-spring-boot-starterspring-boot-starter-jdbc,启用自动配置(通过@EnableAutoConfiguration)并在{{liquigraph.url中声明至少提供application.properties 1}})。

INITIAL ANSWER

Liquigraph旨在通过Neo4j管理迁移。 根据您想要支持的Neo4j版本,您可以选择Liquigraph 2.x(适用于Neo4j 2.x)或Liquigraph 3.x(适用于Neo4j 3.x)。

与Spring的集成非常简单,您可以将DataSourceJDBC URI(和用户/密码)传递给配置构建器,并以编程方式触发迁移运行。

文档描述了这个here(不是特定于Spring,这是以编程方式运行迁移的不可知方式)。

配置可能如下所示(假设您在此配置类可以访问的位置定义DataSource):

@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();
    }
}

完整示例如下:https://github.com/fbiville/liquigraph-spring-boot-example