Neo4j和Spring的数据库迁移

Dav*_*ent 1 java spring flyway spring-data-neo4j-4

这里有一个样例项目

在样本数据,我尝试使用迁飞来实现与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
    }
    */
}
}
Run Code Online (Sandbox Code Playgroud)

我读这关于飞路,可与Neo4j的管理数据库迁移,而我看起来有些页面,大约有春节和Neo4j的迁飞整合解释。

我要问的是,如何保存初始化数据并使用Flyway对其进行管理,并将其与Neo4j和Spring集成在一起?

Rol*_*olf 6

编辑

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

您所需要做的就是使用启动程序 liquigraph-spring-boot-starterspring-boot-starter-jdbc,启用自动配置(通过@EnableAutoConfiguration),并至少liquigraph.url在中声明一个Provide application.properties

初始答案

Liquigraph设计用于管理Neo4j的迁移。根据要支持的Neo4j的版本,您可以选择Liquigraph 2.x(对于Neo4j 2.x)或Liquigraph 3.x(对于Neo4j 3.x)。

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

文档在此处对此进行描述(不特定于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();
    }
}
Run Code Online (Sandbox Code Playgroud)

完整的示例在这里:https : //github.com/fbiville/liquigraph-spring-boot-example