Search code examples
neo4jcypherliquigraph

Neo4j Cypher LiquiGraph - make migration script idempotent


I have created the following LiquiGraph migration script:

<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns="http://www.liquigraph.org/schema/1.0/liquigraph.xsd">

    <changeset id="initial_indexes_and_constraints" author="alex">
        <query>CREATE INDEX ON :Action(entityId)</query>
        <query>CREATE CONSTRAINT ON ( action:Action ) ASSERT action.id IS UNIQUE</query>
    </changeset>
</changelog>

As you may see -I added 2 lines there - for INDEX and CONSTRAINT creation.

I would like to make it idempotent - this way I'd like to check that index or constraint doesn't exist and only then to create them.

Is it possible to add such kind of validation to my LiquiGraph migration script? If so, please show an example.


Solution

  • In general, there is no need to check by index or constraint existence because no action is performed when they already exist.

    For example: running the following command one time (using Neo4j Browser):

    CREATE CONSTRAINT ON ( action:Action ) ASSERT action.id IS UNIQUE
    

    will produce this output:

    Added 1 constraint, completed after 285 ms.
    

    Running the same command again will produce the following output:

    (no changes, no records)