# Plan the Migration

Source: https://docs.formance.com/v4.0/modules/ledger/migration/overview

Stack: v4.0

Ledger does not upgrade a Ledger v2 database in place. Run the source and target side by side, keep Ledger v2 authoritative, and move production traffic only after you have validated the target.

```mermaid
flowchart LR
    Prepare["Prepare Ledger v2"] --> Clients["Update and test clients"]
    Clients --> Mirror["Create a read-only mirror"]
    Mirror --> Validate["Validate continuously"]
    Validate --> Freeze["Freeze Ledger v2 writes"]
    Freeze --> Final["Ingest and validate the final tail"]
    Final --> Promote["Promote once"]
    Promote --> Open["Open target traffic"]
```

## Choose the source transport

The mirror reads the Ledger v2 log through one of two transports.

| Transport  | Choose it when                                                                                                      | Access required                                                                        | Main tradeoff                                                                                      |
| ---------- | ------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| HTTP       | The target can reach the Ledger v2 API and the API can sustain the replay workload. This is the preferred boundary. | `GET /v2/{ledger}/logs`, with OAuth2 client credentials when the source requires them. | Preserves the Ledger v2 service as the database boundary, but adds API and authorization overhead. |
| PostgreSQL | The API is unavailable for this workload or direct database access is an approved operational choice.               | Read access to `_system.ledgers` and the source bucket's `logs` table.                 | Removes API overhead, but grants the target direct access to the Ledger v2 database.               |

Do not choose PostgreSQL only for convenience. It expands the network, credential, and database-permission scope of the migration.

## Understand the authority and rollback boundary

Before promotion:

- applications continue writing to Ledger v2;
- the target remains read-only to application writes;
- you can delete the target, correct its source or rewrite configuration, and replay from the beginning;
- validation can run repeatedly without changing the source.

Promotion stops source replication and changes the target to normal, writable mode. Ledger does not support demoting that ledger back into mirror mode.

Promotion is irreversible for the mirror. After the first target-only production write, routing traffic back to Ledger v2 would also lose that new state because Ledger v2 never received it.

## Assign responsibilities

Record a named owner for each responsibility before creating the production mirror.

| Responsibility       | Required decision                                                    |
| -------------------- | -------------------------------------------------------------------- |
| Migration lead       | Owns the runbook, go/no-go decision, and migration record.           |
| Ledger v2 owner      | Grants read access, monitors source load, and freezes source writes. |
| Target administrator | Creates, monitors, recreates, and promotes the mirror.               |
| Application owner    | Updates API clients, authentication, retries, and event consumers.   |
| Validation owner     | Defines material invariants and signs the final comparison.          |
| Incident owner       | Decides when to stop the cutover and coordinates recovery.           |

The person who can promote the mirror should not be the only person who approves the validation evidence.

## Define the phases and exit conditions

| Phase    | Work                                                                        | Exit condition                                                                      |
| -------- | --------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| Assess   | Inventory ledgers, clients, data patterns, dependencies, and source access. | Every ledger and workflow has an owner and disposition.                             |
| Rehearse | Create a disposable mirror with production-like data and configuration.     | The complete procedure has run without unexplained differences.                     |
| Follow   | Run the production mirror while Ledger v2 remains active.                   | The cursor advances and repeatedly reaches the observed source head.                |
| Validate | Compare technical state, business invariants, clients, and consumers.       | Every material check passes or has an approved explanation.                         |
| Cut over | Freeze Ledger v2, ingest the final tail, and rerun critical checks.         | The target cursor equals the recorded final source log ID and approval is recorded. |
| Promote  | Stop replication, switch clients, and send one controlled write.            | The write, read, and downstream event all match the expected result.                |

## Produce a migration record

Keep one record per source ledger. At minimum, include:

- source and target ledger names;
- source transport and credential owner;
- initial and final Ledger v2 log IDs;
- target cursor and observed source head;
- exact rewrite rules, including their order;
- expected transformed or dropped entries;
- client and event-consumer versions;
- validation results and evidence locations;
- rehearsal duration and production interruption budget;
- go/no-go approvers and the promotion timestamp.

## Stop before creating the mirror when

- the target environment cannot read the full source log;
- the client contract or authentication path has not been tested;
- a required data difference has no deterministic transformation rule;
- no one owns the balance and business-invariant comparison;
- the team has no way to freeze all Ledger v2 writers;
- the cutover plan assumes that promotion can be undone.

Continue with [Prepare the Source](https://docs.formance.com/v4.0/modules/ledger/migration/prepare-source).
