From 342e97f4c85e04940946f31954ec91268913c3cf Mon Sep 17 00:00:00 2001 From: "oussama.badr" Date: Wed, 31 Mar 2021 02:05:49 +0200 Subject: [PATCH] Export Liquigraph ChangeLog to Liquibase main class #9 --- pom.xml | 12 +++- .../neo4j/liquigraph/ChangelogExporter.java | 57 +++++++++++++++++++ src/main/resources/liquigraph-changelog.xml | 10 ++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/main/java/liquibase/ext/neo4j/liquigraph/ChangelogExporter.java create mode 100644 src/main/resources/liquigraph-changelog.xml diff --git a/pom.xml b/pom.xml index 8a32ffb3..9889f2ba 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,8 @@ UTF-8 - 1.8 - 1.8 + 11 + 11 ${maven.compiler.source} 4.1.1 @@ -34,13 +34,19 @@ org.liquibase liquibase-core ${liquibase.version} - provided + org.neo4j neo4j-jdbc-bolt 4.0.1 + + org.liquigraph + liquigraph-core + 4.0.2 + true + diff --git a/src/main/java/liquibase/ext/neo4j/liquigraph/ChangelogExporter.java b/src/main/java/liquibase/ext/neo4j/liquigraph/ChangelogExporter.java new file mode 100644 index 00000000..f0b0abcd --- /dev/null +++ b/src/main/java/liquibase/ext/neo4j/liquigraph/ChangelogExporter.java @@ -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 liquigraphChangeSets = getLiquigraphChangeSets(); + + List changeSets = convertChangeSets(liquigraphChangeSets); + changeLogSerializer.write(changeSets, System.out); + } + + @NotNull + private static Collection getLiquigraphChangeSets() { + ChangelogParser changelogParser = new ChangelogParser(new XmlSchemaValidator(), new ChangelogPreprocessor(new ImportResolver())); + return changelogParser.parse(ClassLoaderChangelogLoader.currentThreadContextClassLoader(), "liquigraph-changelog.xml"); + } + + @NotNull + private static List convertChangeSets(Collection 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; + } + +} diff --git a/src/main/resources/liquigraph-changelog.xml b/src/main/resources/liquigraph-changelog.xml new file mode 100644 index 00000000..bf2c3d4b --- /dev/null +++ b/src/main/resources/liquigraph-changelog.xml @@ -0,0 +1,10 @@ + + + + CREATE (n:Sentence {text:'Hello monde!'}) RETURN n + + + MATCH (n:Sentence {text:'Hello monde!'}) SET n.text='Hello world!' RETURN n + + \ No newline at end of file