diff --git a/src/main/java/liquibase/ext/neo4j/generator/DeleteGenerator.java b/src/main/java/liquibase/ext/neo4j/generator/DeleteGenerator.java new file mode 100644 index 00000000..2f07003b --- /dev/null +++ b/src/main/java/liquibase/ext/neo4j/generator/DeleteGenerator.java @@ -0,0 +1,32 @@ +package liquibase.ext.neo4j.generator; + +import liquibase.change.ChangeMetaData; +import liquibase.database.Database; +import liquibase.exception.ValidationErrors; +import liquibase.sql.Sql; +import liquibase.sql.UnparsedSql; +import liquibase.sqlgenerator.SqlGeneratorChain; +import liquibase.sqlgenerator.core.AbstractSqlGenerator; +import liquibase.statement.core.DeleteStatement; + +public class DeleteGenerator extends AbstractSqlGenerator { + + @Override + public ValidationErrors validate(DeleteStatement deleteStatement, Database database, + SqlGeneratorChain sqlGeneratorChain) { + return null; + } + + @Override + public Sql[] generateSql(DeleteStatement deleteStatement, Database database, + SqlGeneratorChain sqlGeneratorChain) { + String label = deleteStatement.getTableName(); + String cypher = String.format("MATCH (n: %s)%nDELETE n", label); + return new Sql[]{new UnparsedSql(cypher)}; + } + + @Override + public int getPriority() { + return ChangeMetaData.PRIORITY_DEFAULT; + } +} diff --git a/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator b/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator new file mode 100644 index 00000000..e9bee1a7 --- /dev/null +++ b/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator @@ -0,0 +1 @@ +liquibase.ext.neo4j.generator.DeleteGenerator \ No newline at end of file diff --git a/src/test/groovy/liquibase/ext/neo4j/Neo4jPluginTest.groovy b/src/test/groovy/liquibase/ext/neo4j/Neo4jPluginTest.groovy index 9aa67080..7f398db2 100644 --- a/src/test/groovy/liquibase/ext/neo4j/Neo4jPluginTest.groovy +++ b/src/test/groovy/liquibase/ext/neo4j/Neo4jPluginTest.groovy @@ -148,6 +148,30 @@ CREATE (:SecretMovie {title: 'Neo4j 4.4 EE: A life story'}); } + def "run deletes"(){ + given: + queryRunner.run("MERGE (:Person {name: 'Charles', firstName: 'Clarence'})") + String[] arguments = [ + "--url", "jdbc:neo4j:${neo4jContainer.getBoltUrl()}", + "--username", "neo4j", + "--password", PASSWORD, + "--changeLogFile", "classpath:/changelog-delete.xml", + "update" + ].toArray() + + when: + Main.run(arguments) + + then: + def rows = queryRunner.getRows(""" + MATCH (p: Person) + RETURN Count(p) = 0 as result + """) + rows.size() == 1 + rows[0] == ["result": true] + } + + private static PrintStream mute() { new PrintStream(Files.createTempFile("liquibase", "neo4j").toFile()) } diff --git a/src/test/resources/changelog-delete.xml b/src/test/resources/changelog-delete.xml new file mode 100644 index 00000000..68471631 --- /dev/null +++ b/src/test/resources/changelog-delete.xml @@ -0,0 +1,10 @@ + + + + + Deleting a person + + + \ No newline at end of file