Skip to content

Commit

Permalink
Export Liquigraph ChangeLog to Liquibase main class #9
Browse files Browse the repository at this point in the history
  • Loading branch information
oussamabadr committed Mar 31, 2021
1 parent 0ad01e9 commit 342e97f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.enforcer.requireJavaVersion>${maven.compiler.source}</maven.enforcer.requireJavaVersion>
<liquibase.version>4.1.1</liquibase.version>
</properties>
Expand All @@ -34,13 +34,19 @@
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
<scope>provided</scope>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-bolt</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.liquigraph</groupId>
<artifactId>liquigraph-core</artifactId>
<version>4.0.2</version>
<optional>true</optional>
</dependency>


<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package liquibase.ext.neo4j.liquigraph;

import liquibase.change.core.RawSQLChange;
import liquibase.changelog.ChangeSet;
import liquibase.serializer.ChangeLogSerializer;
import liquibase.serializer.ChangeLogSerializerFactory;
import org.jetbrains.annotations.NotNull;
import org.liquigraph.core.io.xml.ChangelogParser;
import org.liquigraph.core.io.xml.ChangelogPreprocessor;
import org.liquigraph.core.io.xml.ClassLoaderChangelogLoader;
import org.liquigraph.core.io.xml.ImportResolver;
import org.liquigraph.core.io.xml.XmlSchemaValidator;
import org.liquigraph.core.model.Changeset;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* Poc for exporting Liquigraph changelog to Liquibase
* TODO ==> Post a condition + Check Liquigraph in classpath....
*/
public class ChangelogExporter {

public static void main(String[] args) throws IOException {
ChangeLogSerializerFactory changeLogSerializerFactory = ChangeLogSerializerFactory.getInstance();

ChangeLogSerializer changeLogSerializer = changeLogSerializerFactory.getSerializer("xml");

Collection<Changeset> liquigraphChangeSets = getLiquigraphChangeSets();

List<ChangeSet> changeSets = convertChangeSets(liquigraphChangeSets);
changeLogSerializer.write(changeSets, System.out);
}

@NotNull
private static Collection<Changeset> getLiquigraphChangeSets() {
ChangelogParser changelogParser = new ChangelogParser(new XmlSchemaValidator(), new ChangelogPreprocessor(new ImportResolver()));
return changelogParser.parse(ClassLoaderChangelogLoader.currentThreadContextClassLoader(), "liquigraph-changelog.xml");
}

@NotNull
private static List<ChangeSet> convertChangeSets(Collection<Changeset> liquigraphChangeSets) {
return liquigraphChangeSets.stream().map(ChangelogExporter::from).collect(Collectors.toList());
}

@NotNull
private static ChangeSet from(Changeset changeset) {
ChangeSet liquibaseChangeSet = new ChangeSet(changeset.getId(), changeset.getAuthor(), changeset.isRunAlways(),
changeset.isRunOnChange(), null, String.join(",",
changeset.getExecutionsContexts()),null, null);
changeset.getQueries().stream().map(RawSQLChange::new).forEach(liquibaseChangeSet::addChange);
return liquibaseChangeSet;
}

}
10 changes: 10 additions & 0 deletions src/main/resources/liquigraph-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.liquigraph.org/schema/1.0/liquigraph.xsd">
<changeset id="hello-world" author="you">
<query>CREATE (n:Sentence {text:'Hello monde!'}) RETURN n</query>
</changeset>
<changeset id="hello-world-fixed" author="you">
<query>MATCH (n:Sentence {text:'Hello monde!'}) SET n.text='Hello world!' RETURN n</query>
</changeset>
</changelog>

0 comments on commit 342e97f

Please sign in to comment.