Skip to main content

Migrations

Changing a schema only applies its additive steps right away, everything else waits for a migration. A migration applies the schema to all databases:

  • Neo4j: the indices which the properties stored in Neo4j ask for with index, and raw Neo4j indices, see Neo4j.
  • MongoDB: a $jsonSchema validator and the indices of the properties stored in MongoDB, see MongoDB.
  • Elasticsearch: the index of the type with the mapping of the schema, see Elasticsearch.

Every step belongs to a target (neo4j, mongodb or elasticsearch).

Additive steps are applied right away

The rule: a step is applied right away if it only adds structure, does not build an index over existing data, and would otherwise be decided wrongly by the next write, e.g. by the dynamic mapping of Elasticsearch. Every other step is part of a migration.

Creating an Elasticsearch index, adding fields and dynamic templates to its mapping, creating a MongoDB collection and setting its validator do not wait for a migration: they are applied when a schema is saved (PUT, PATCH, DELETE of /schema/<type>) and after backup:load has loaded the schemas. The steps are named create-elasticsearch-index, update-elasticsearch-mapping, create-collection and update-validator. Index builds (every Neo4j index, MongoDB indices), removing or dropping something, and incompatible fields are never applied this way. A database with a blocking step is not touched at all. Errors are logged and never fail the request which saved the schema, the next plan contains the steps again and a migration reports their errors.

Versions of the databases

Some indices need a minimum version of the database, e.g. full-text indices need Neo4j 5.0 and bit vectors need Elasticsearch 8.15. The plan is made for the version which the database reports. An index which is asked for with index but which the version can not provide is part of the plan as a step which fails. Such steps, and the steps for incompatible Elasticsearch fields, are marked as blocking: they run before the other steps of the same database, so a database whose blocking step fails has not been changed. A blocking step does not hold back the databases before it: an incompatible Elasticsearch field fails the migration only after the Neo4j and MongoDB steps ran. Update the database or choose another intent. If a newer index replaces an older one in a later version of the API, databases which support it get the newer index, and the others keep the older one.

Running a migration

  1. GET /schema/<type>/plan shows the steps without changing anything, in the order Neo4j, MongoDB, Elasticsearch, the blocking steps of each database first.
  2. POST /migration stores them in a pending migration. The migration remembers the version of the schema which it was planned for, and cancels the older pending, queued and failed migrations of the same schema. A migration which is running right now is left alone.
  3. PATCH /migration/<uuid> with {"status": "queued"} queues it and announces it in RabbitMQ. The background job migration:listen waits for these announcements and runs the migration right away, so requests never wait for the database; migration:run sweeps every 15 minutes for queued migrations which were not announced, e.g. while RabbitMQ was down. This is refused with 409 Conflict if the schema was changed since the migration was generated (its schemaVersion is not the current version of the schema): generate a new migration.

The statements are generated when the plan is made, so GET /schema/<type>/plan shows exactly what a migration generated now would execute. The plan is a migration which is not stored, _MigrationPlan, with the same names as a migration (schemaName, kind, schemaVersion, steps): every step has its statement (a Cypher string for Neo4j, a JSON object for MongoDB and Elasticsearch) and blocking (true or false). POST /migration stores these steps in the property _steps, where every step also has the status and error of the run, and running or retrying a migration executes the stored statements. A step without a statement fails with a clear error and is never executed.

The result of every step and, if a step failed, the error are readable on the migration, see GET /migration/<uuid>. A failing step stops the migration, earlier steps are not rolled back. The result of each step is saved right after the step, so a migration which is interrupted is retried after the steps which already succeeded. A Neo4j index step only succeeds once the index is online, a failed population fails the step. While a migration runs, it holds a lock which is extended after every step, so long steps like building an index do not let it expire. A migration which is running without a lock was interrupted, e.g. by a crash, and is marked as failed. A run whose lock expired during a long step leaves the status to whoever marked it, only its progress is saved. Status changes with PATCH /migration/<uuid> take the same lock and answer with 409 Conflict while the migration runs.

While a migration runs, writes of elements of its type answer with 423 Locked, and so does saving the schema of the type. A migration whose type is locked, e.g. because its schema is saved right now, stays queued and runs with the next sweep of migration:run. Migrations which were queued for an older version of the schema are marked as failed by migration:run as well. The version is checked again before every further step: if the schema is changed while a migration runs, the migration stops before its next step and is marked as failed, the steps which already ran are kept. Generate a new migration, it plans what the changed schema needs. The status is the only property which clients set, with PATCH /migration/<uuid>:

StatusAllowed fromEffect
queuedpending (run), failed (retry)migration:run runs it; a retry skips the steps which already succeeded and continues with the failed one
cancelledpending, queued, failedthe migration is never run, the databases are not changed

Every other transition is answered with 409 Conflict, the API moves a queued migration on to running, succeeded or failed itself. The commands migration:generate and migration:run offer the same on the command line. Migrations are connected to their schema with HAS_MIGRATION.

Backups

backup:create removes the statement of every step of a migration and keeps the rest of the steps (target, action, name, description, status, error): a statement which was planned for another state of the databases, maybe years ago, must never run. backup:load plans the steps again for the schemas and databases as they are after the load. Migrations which were queued or running are pending again, nobody is running them anymore, and pending and failed migrations get new steps, which also picks up improvements of the planner. If nothing is left to do, or the schema of the migration does not exist anymore, the migration is cancelled and its error says why. succeeded and cancelled migrations are history, they keep their steps without statements.

An incompatible Elasticsearch mapping

Fields of an Elasticsearch mapping can’t change their type. If a property was mapped with another type before, e.g. by the dynamic mapping of values which were written before the schema existed, or by an earlier version of the schema, the step for it is incompatible and fails every migration of the type. The steps of Neo4j and MongoDB still run. Elasticsearch only holds a copy, so the index can be rebuilt from the primary databases: create a backup with backup:create, reset the databases and load it again with backup:load, which creates the indices for the current schemas.