---
title: 'Configuration Sync'
url: 'http://phramecms.org/docs/configuration-sync'
markdown_url: 'http://phramecms.org/docs/configuration-sync.md'
bundle: guide
section: operations
summary: 'Manage active database configuration alongside Git-tracked canonical JSON in config/sync/ with dry-run previews, atomic imports, and immutable rollback snapshots.'
source: 'https://github.com/PhrameCMS/docs/blob/main/guides/configuration-sync.md'
updated_at: '2026-12-04T21:49:38+00:00'
---

# Configuration Sync

PhrameCMS separates **active runtime configuration** from **Git-tracked canonical configuration** so content models, entity types, bundles, field definitions, and site settings can be code-reviewed, diffed cleanly, and deployed safely across environments.

## Active Storage vs. Sync Storage

`PhrameCms\Config\ConfigManager` coordinates three pluggable stores (wired in `phramecms/skeleton` by `App\AppBootstrap`):

1. **Active storage (`DatabaseConfigStorage`)**: Persists live configuration objects in the `phrame_config` database table (`name`, `data`, SHA-256 `hash`, `updated_at`). All runtime reads resolve from here.
2. **Sync storage (`FileConfigStorage`)**: Reads and writes `<name>.json` files inside `config/sync/` (configurable via `PHRAME_CONFIG_SYNC_DIR`).
3. **Snapshot store (`DatabaseSnapshotStore`)**: Appends immutable, timestamped full-configuration snapshots (`snap_<hex>`) to the `phrame_config_snapshots` table before every live mutation (`config.set`, `config.delete`, `config.import`, and `config.rollback`).

### Canonical JSON & Naming Rules

- Every configuration name must match `^[a-z0-9_]+(\.[a-z0-9_]+)+$` (at least two dot-separated lowercase alphanumeric/underscore segments, such as `admin.settings` or `entity.bundle.content.article`).
- `PhrameCms\Config\Canonicalizer` recursively sorts associative keys (`SORT_STRING`) while preserving list order, formats JSON with `JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE` and a trailing LF newline, and computes deterministic SHA-256 hashes. This eliminates noisy diffs in Git and enables reliable optimistic concurrency checks.
- Before any write or import, `ConfigManager::validate()` checks each configuration object against the most specific matching `ConfigSchemaProviderInterface` registered in the container. On `config.import`, **all** incoming objects are validated before any snapshot is taken or any storage row is modified.

## Core Configuration Operations

All configuration operations execute through `OperationBus` and are available via CLI (`./bin/phrame op:run`), MCP tools (`config_*`), and the Admin UI (`/admin/config`). The HTTP API (`/api/v1/config*`) exposes `config.list`, `config.diff`, `config.get`, `config.set`, and `config.delete`, while bulk sync and snapshot operations (`config.export`, `config.import`, `config.snapshots`, `config.rollback`) are executed through CLI, MCP, or `/admin/config`.

| Operation | Required Permission | Dry-Run | Key Input Parameters |
| :--- | :--- | :--- | :--- |
| `config.list` | `config.read` | No | `prefix` *(optional string)* |
| `config.get` | `config.read` or `config.<name>.read` | No | `name` *(required)* |
| `config.diff` | `config.read` | No | `prefix`, plus at most one of `directory`, `configs`, or `snapshot_id` *(defaults to configured sync storage)* |
| `config.set` | `config.write` or `config.<name>.write` | Yes | `name` *(required)*, `data` *(required object)*, `reason` *(optional)* |
| `config.delete` | `config.write` or `config.<name>.write` | Yes | `name` *(required)*, `reason` *(optional)* |
| `config.export` | `config.export` | Yes | `prefix`, `directory` *(optional override)*, `delete_extra` *(boolean, default `true`)* |
| `config.import` | `config.import` | Yes | `prefix`, `delete_missing` *(boolean; defaults to `true` for sync/directory imports and `false` for inline `configs`)*, `reason`, plus at most one of `configs` or `directory` *(defaults to configured sync storage)* |
| `config.snapshots` | `config.read` | No | `limit` *(integer `1`–`500`, default `50`)* |
| `config.rollback` | `config.import` | Yes | `snapshot_id` *(required)*, `reason` *(optional)* |

> **Granular per-object permissions:** `config.get`, `config.set`, and `config.delete` enforce `ConfigPermissionGuard`, accepting either the coarse permission (`config.read` / `config.write`) or the object-specific permission (`config.<name>.read` / `config.<name>.write`, e.g., `config.admin.settings.write`).

## Day-to-Day Git Sync Workflow

### 1. Export Active Configuration to `config/sync/`

After making schema or settings changes locally (in `/admin/schema`, `/admin/appearance`, or via operations), export them to canonical JSON files in `config/sync/`:

```bash
# Preview what would change in config/sync/ without writing files
./bin/phrame op:run config.export --dry-run --scopes=config.export --json

# Write canonical JSON files into config/sync/
./bin/phrame op:run config.export --scopes=config.export
```

Inspect `git diff config/sync/` and commit the canonical JSON files to your repository.

### 2. Inspect Differences (`config.diff`)

Compare active database configuration against `config/sync/` (or pass `snapshot_id` to diff against a snapshot):

```bash
./bin/phrame op:run config.diff --scopes=config.read --json

# Limit diff to a prefix (for example, admin.* settings only)
./bin/phrame op:run config.diff --scopes=config.read --input='{"prefix":"admin."}' --json
```

The response includes `empty` (`bool`) and a structured `diff` object with `created`, `updated`, and `deleted` arrays (each entry holding `name`, `before`, and `after`).

### 3. Preview and Apply Imports (`config.import`)

In staging or production deployments, always preview with `--dry-run` first, then run the atomic import:

```bash
# Dry-run: validate all files in config/sync/ and return the diff with zero writes
./bin/phrame op:run config.import --dry-run --scopes=config.import --json

# Live import: creates an automatic pre-import snapshot, then applies all changes atomically
./bin/phrame op:run config.import --scopes=config.import --input='{"reason":"Release 2026.11"}' --json
```

If any write fails mid-import, `ConfigManager::applyDiffAtomically()` rolls back previously applied creates, updates, and deletes in-memory/storage and rethrows the exception. On success, `data.snapshot_id` contains the ID of the snapshot taken right before the import, and a `ConfigChangedEvent` is dispatched over PSR-14.

### 4. Edit or Delete a Single Config Object (`config.set` / `config.delete`)

```bash
# Preview updating admin.settings
./bin/phrame op:run config.set --dry-run --scopes=config.admin.settings.write \
  --input='{"name":"admin.settings","data":{"brand_name":"Acme CMS","theme":"default"},"reason":"Update brand"}' \
  --json

# Apply live
./bin/phrame op:run config.set --scopes=config.admin.settings.write \
  --input='{"name":"admin.settings","data":{"brand_name":"Acme CMS","theme":"default"},"reason":"Update brand"}'

# Delete a configuration object (also captures a snapshot first)
./bin/phrame op:run config.delete --scopes=config.write \
  --input='{"name":"site.feature_flags","reason":"Remove deprecated flags"}'
```

## Snapshots and Reversible Rollback

Every live `config.set`, `config.delete`, and `config.import` records an immutable snapshot of the **entire** active configuration map in `phrame_config_snapshots` via `DatabaseSnapshotStore`.

List recent snapshots (newest first):

```bash
./bin/phrame op:run config.snapshots --scopes=config.read --input='{"limit":10}' --json
```

Preview and execute a rollback to any prior snapshot:

```bash
# Dry-run rollback to inspect exact changes
./bin/phrame op:run config.rollback --dry-run --scopes=config.import \
  --input='{"snapshot_id":"snap_<id>"}' --json

# Execute rollback (automatically creates a pre-rollback snapshot so the rollback itself is reversible)
./bin/phrame op:run config.rollback --scopes=config.import \
  --input='{"snapshot_id":"snap_<id>","reason":"Revert unintended import"}' --json
```

Because `config.rollback` saves a `pre_rollback_snapshot_id` before restoring the target snapshot, even a rollback can be undone cleanly.

## Related Guides

- [Architecture Overview](architecture.md) — How `ConfigModule` interacts with `EntityModule`, `AdminModule`, and `OperationBus`.
- [AI Change Proposals & Human Approval Queue](proposals.md) — Stage AI-generated `config.set` or `config.import` changes for human approval instead of granting live write access.
- [Security & Production Deployment](security-production.md) — Keep secrets in `.env`, never in `config/sync/`.
