Skip to content

Neo4j schema DDL on the SQL stores

Postgres, Oracle and SQLite run Neo4j’s schema DDL, given as Cypher (a CypherStatement, or a statement of no language on a datasource that does not default to SQL) to a PersistenceManager, so that a schema file written for Neo4j, such as an APOC export’s, replays as it is. Drivine reads each statement and applies it through the schema machinery, as the schema API applies a spec: the item it makes has the name, the expressions and the inventory entry the API gives the same spec, so a later enforce of an application that declares it finds it matching, not drifted or orphaned. The statement gives no rows. Neo4j, Memgraph and FalkorDB are sent the statement as it is, as before.

DDL Applied as
CREATE [RANGE] INDEX [name] [IF NOT EXISTS] FOR (n:L) ON (n.a, n.b), or Neo4j 4’s ON :L(a, b) RangeIndexSpec(L, [a, b])
CREATE CONSTRAINT [name] [IF NOT EXISTS] FOR (n:L) REQUIRE n.p IS [NODE] UNIQUE, REQUIRE (n.a, n.b) IS UNIQUE, or Neo4j 4’s ON (n:L) ASSERT n.p IS UNIQUE UniquenessConstraintSpec
CREATE FULLTEXT INDEX … FOR (n:L) ON EACH [n.a, …] [OPTIONS {indexConfig: {fulltext.analyzer: …}}] FullTextIndexSpec, with the analyzer; fulltext.eventually_consistent is ignored
CREATE VECTOR INDEX … FOR (n:L) ON n.p OPTIONS {indexConfig: {…}} VectorIndexSpec: vector.dimensions (required), vector.similarity_function (cosine or euclidean, in any case), vector.hnsw.m and vector.hnsw.ef_construction, and vector.quantization.enabled as Neo4jVectorOptions; indexProvider is ignored
CREATE TEXT INDEX … TextIndexSpec, which the SQL stores refuse (see the tables below), so the statement fails
DROP INDEX name [IF EXISTS], DROP CONSTRAINT name [IF EXISTS] SchemaManager.remove of the item that goes by the name
CALL db.awaitIndexes(…), CALL db.awaitIndex(…) Nothing: an index on a SQL store is whole once it is made

A statement is read as DDL only where it opens with one of these keyword sequences, so Cypher that merely uses the words, such as CREATE index = (a)-[:R]->(b), is Cypher. Names, labels and properties may be in backticks, with spaces, and a doubled backtick for one. Keywords are read in any case, and a statement may end with ;.

Refused by name. POINT and LOOKUP indexes; an index or constraint on relationships; one over several labels ((n:A|B)); existence (IS NOT NULL), key (IS NODE KEY, IS KEY) and type (IS :: TYPE) constraints; a vector index over several properties, with WITH [...] properties, or without vector.dimensions; Neo4j 4’s DROP … ON by label and properties; a name, or OPTIONS, given as a parameter. The message says what is not made and why, and the Cypher compiler refuses the statement with the same message. One a SQL store honours is refused by the compiler too, as a schema command to run through a PersistenceManager. drivine-cypher-spec’s schema.yaml pins how each statement reads, in all three libraries.

The ddl owner. What DDL creates is ensured with SchemaManager.ensure under an owner of its own, ddl, never an application’s: an application’s own owner records what its catalogs declare, and an ensure of DDL under it would take the place of that. So the items are recorded in the _DrivineSchema marker of scope ddl, as ensured at runtime, and an application that declares the same item co-owns it: each owner’s marker records it under the same inventory key. A DROP removes the item from the ddl owner and drops it, as Neo4j does, unless another owner records it: where an application (or any owner other than ddl) has the item in its _DrivineSchema marker, declared or ensured at runtime, the DROP forgets it in the ddl owner alone, leaves it in place, takes the dropped name off its aliases, and logs a warning, so the application’s next enforce finds it as it was. That removal is SchemaManager.remove, above, under the ddl owner. The marker’s and the store stamp’s own constraints (_DrivineSchema(scope), DrivineStore(key)), which an export of a Drivine graph carries, are ensured as Drivine ensures them and recorded in no inventory; any other schema for those labels is refused.

Names and aliases. The item is made under the name the schema API gives it (Label_property_unique, …). The DDL’s name, where the item goes by another (as an item made over the same properties in another order does, or one the API named), is kept as an alias in the item’s record in <graph>_schema, a list aliases among its details, so that a DROP by it finds the item, in this process or a later one: an APOC export’s DROP CONSTRAINT UNIQUE_IMPORT_NAME, its last statement, drops the constraint its schema file named so. A DROP looks the name up as the API’s name first, then as an alias. Readers of the catalog that do not know of aliases read the record as before. A recreate (drift, a version change, recreateAll) carries an item’s aliases over. Drivine for TypeScript and drivine-python do not carry aliases through a recreate yet: until their ports land, an item they recreate loses its aliases, as does, on Oracle, a range index whose record they write again (for its labelled index, or as served by a uniqueness constraint’s), and a DROP by one then fails, or does nothing with IF EXISTS. Nothing else of the item is lost: it reads as before, and is not drift.

CREATE follows Neo4j: over an item of the same kind, label and properties that is there already, IF NOT EXISTS does nothing but add the alias (and leaves an item of another shape as it is, with a warning), and without it the statement fails. A name another item goes by, by the API’s name or an alias, is refused, with IF NOT EXISTS or without. A DROP of a name no item goes by fails, and does nothing with IF EXISTS; DROP INDEX of a constraint’s name fails, as does DROP CONSTRAINT of an index’s.

Transactions. DDL is refused inside an open transaction (the transactional manager, or the delegating one within @Transactional), save db.awaitIndexes, which does nothing there: Drivine applies schema on connections of its own, committed as they go, which the transaction’s rollback would not undo. Run it outside the transaction. Each command borrows one such connection, which its catalog reads, its DDL and its marker’s writes all run on, and gives it back as it ends. A batch (executeBatch) of one schema command runs it; a batch of more that holds one is refused, since a batch is one transaction.