Graph migrations and rollback
A graph migration describes how to change a graph definition: its nodes, connections, rules, or presentation data. Your application supplies a saved definition and a plan of actions; ExecKernel checks those actions and returns a new definition. This gives you a change that you can inspect before making it available to the application.
This page follows a small change from its original snapshot to a verified compensation. The example changes the prompt shown at the start of a support request. The graph keeps the same nodes and connections, so we can concentrate on the relationship between the source, the change, and the record of what was applied.
Keep the source that the plan was reviewed against
Section titled “Keep the source that the plan was reviewed against”A snapshot is the complete array describing the definition at a particular point. For migration, it includes the graph data, version declarations, and applied_migrations, the ordered list of migration identifiers already applied. Retain that source alongside the plan: the original values are needed both to review the change and to prepare compensation.
Our support-request graph starts at node 10. Its title is “Contact support” in English and “Contactar con soporte” in Spanish. Choosing edge 100 reaches terminal node 20, where your application can present the request form. We will change the initial title to “Describe your request” and “Describe tu solicitud”. No node identifier or connection changes.
The complete source snapshot declares graph version 1.0.0, engine_version: "1.0.0", and DSL/schema version 1.0. These values are inputs to the compatibility checks; engine_version selects the declared engine compatibility policy and is not a reading of the installed Composer package version. Version compatibility and session upgrades explains that distinction.
This isolated example has no active sessions, so its active_nodes and active_edges lists are empty. In an application with active sessions, your application must supply accurate lists. The strict migration validator uses them to reject protected structural or rule changes; it does not discover your sessions or move them to the new definition.
Bind the plan to that snapshot
Section titled “Bind the plan to that snapshot”A migration plan contains an identifier and an ordered list of actions. This is the complete plan for the example:
{ "version": "1.0.0-mig.001", "description": "Clarify the request prompt in both supported languages", "source_snapshot_hash": "aca9a760e8d895ef1e62bcc283e2f169e9bbf4fdfe1681b2843549714d1a543e", "actions": [ { "type": "RENAME_NODE", "nodeId": 10, "lang": "en", "title": "Describe your request" }, { "type": "RENAME_NODE", "nodeId": 10, "lang": "es", "title": "Describe tu solicitud" } ]}Here, RENAME_NODE changes the localized title. It leaves the numeric node ID intact. The two actions update the two languages that your application can display.
The identifier follows X.Y.Z-mig.N. It must advance the migration ledger; changing the padding of an existing counter does not create another migration. This identifier tracks an applied change. It does not itself assign a new graph_version; publishing a later graph revision is a separate step.
Compute source_snapshot_hash with MigrationSnapshot::hash($source) when preparing the plan. It represents the full typed snapshot, including list order and the migration ledger. If someone changes that source after review, the same plan is rejected instead of being silently applied to different data. This binding is more specific than the graph checksum described in Graph identity and versioning.
Validate and apply the change
Section titled “Validate and apply the change”The following fragments use the downloaded snapshot and plan in a PHP application with ExecKernel installed. Load the snapshot as an array and parse the plan with MigrationPlanParser:
use ExecKernel\Infrastructure\Migration\MigrationPlanParser;
$source = json_decode( file_get_contents(__DIR__ . '/migration-source.json'), true, 512, JSON_THROW_ON_ERROR,);$plan = (new MigrationPlanParser())->parseFile( __DIR__ . '/migration-plan.json',);Validation checks the plan against that source, including its identity binding, action inputs, referenced nodes and edges, migration order, and resulting graph validity. Use a validation call when you want to review the result before applying the plan:
use ExecKernel\Domain\Versioning\CompatibilityMode;use ExecKernel\Infrastructure\Migration\MigrationApplier;use ExecKernel\Infrastructure\Migration\MigrationValidator;
$mode = CompatibilityMode::STRICT;(new MigrationValidator())->validate($plan, $source, $mode);
$applier = new MigrationApplier();$candidate = $applier->apply($plan, $source, $mode);apply() repeats validation and returns a separate array. It preserves $source. Your application can compare the candidate with the original, display a review, and retain both artifacts without replacing a published definition.
For this change, the observable difference is:
| Part of the definition | Source | Candidate |
|---|---|---|
Node 10, English title |
Contact support | Describe your request |
Node 10, Spanish title |
Contactar con soporte | Describe tu solicitud |
Nodes and edge 100 |
10 → 20 |
10 → 20 |
graph_version |
1.0.0 |
1.0.0 |
applied_migrations |
Empty | 1.0.0-mig.001 |
If validation rejects the change, retain the source and correct or reconsider the plan. A rejected action has not published a definition or advanced any session.
Prepare a compensation from the original values
Section titled “Prepare a compensation from the original values”A rollback plan is another migration. It restores the values affected by the forward change and receives a later identifier. RollbackPlanBuilder uses the original snapshot to recover those values, applies its proposed compensation internally, and checks that it faithfully restores the source content before returning the plan.
use ExecKernel\Infrastructure\Migration\RollbackPlanBuilder;
$rollback = (new RollbackPlanBuilder())->build( plan: $plan, graphSnapshot: $source, rollbackVersion: '1.0.0-mig.002', mode: $mode,);$restored = $applier->apply($rollback, $candidate, $mode);The returned plan is bound to the exact forward result. Applying it to a candidate that has changed again is rejected. This prevents a compensation prepared for one change from overwriting subsequent work.
The restored title matches the original, but the ledger now contains both 1.0.0-mig.001 and 1.0.0-mig.002. That history explains why the current content returned to its earlier form.
Use the migration content comparison to distinguish restored content from an identical entire snapshot:
use ExecKernel\Domain\Migration\MigrationSnapshot;
$contentRestored = MigrationSnapshot::content($restored) === MigrationSnapshot::content($source); // true
$identicalSnapshot = $restored === $source; // false: the ledger grewcontent() excludes the migration ledger and normalizes a few optional envelope defaults for this comparison. It preserves meaningful list order and scalar types. Keep the actual snapshots and ledger; the comparison does not erase their history.
Not every change admits a faithful compensation. For example, a removal may discard data that the available inverse actions cannot restore. The builder then rejects the rollback instead of promising a partial restoration. Keep the original revision available and review how to proceed. A graph compensation also leaves external business effects, such as a sent email, untouched.
Complete example and publication
Section titled “Complete example and publication”Download migration-source.json, migration-plan.json, and migration-roundtrip.php. Place all three in your project root, alongside vendor/. The PHP file loads vendor/autoload.php from that root and contains the complete sequence shown above.
php migration-roundtrip.phpIts result includes the original, changed, and restored titles; content_restored: true; snapshot_identical: false; both ledger entries; and changed_source_rejected: true for a deliberately modified source. No published graph or stored session is changed.
The candidate is now ready for Graph upgrade and publication pipeline, which validates a target revision, checks execution, and confirms that your application’s graph repository can retrieve the exact published artifact. Whether an existing session can use that revision remains a separate session compatibility decision.