16.1 Reverting the database to an earlier point
The revert API puts the whole database back to an earlier point by permanently deleting every transaction after it. Use it for experiments, tests and seeding, by hand or from a coding agent: try something on real data, then keep it or throw it away.
The revert window is the cheap form, for when you know in advance:
long ts = db.BeginRevertWindow(); // the point to return to
// ... insert, update, delete, query ...
db.RollbackRevertWindow(); // back to exactly the state at Begin
// ... or ...
db.CommitRevertWindow(); // keep everythingWhile a window is open, the store saves no state past its start, so a rollback is only a log
truncation and a reload. The changes are still durable: a crash inside the window keeps them. Keep
windows short, as the indexes are not made durable meanwhile. One window can be open at a time,
db.RevertWindow returns it (or null), and closing the store commits it.
DeleteTransactionsAfter works with any timestamp you kept, without a window, even across
restarts:
long ts = db.Timestamp; // take it BEFORE the changes
// ... changes, possibly across a restart ...
var preview = db.DeleteTransactionsAfter(ts, dryRun: true); // counts only, changes nothing
var result = db.DeleteTransactionsAfter(ts); // truncates the log and reloadsBoth forms return a DeleteTransactionsResult: what was deleted and what had to be rebuilt. Without a
window, state and indexes saved after the target are rebuilt from the log, which takes a while on a
large database. While a window is open, DeleteTransactionsAfter runs only as a dry run.
- It is destructive and global. The deleted transactions are gone, including writes that other parts of the application made in the meantime. There is no undo.
- The target must be in the log. A timestamp before its first transaction is refused.
- Files are not reverted. File properties get back the values they had, but files uploaded since stay behind in the file store, unreferenced.
- History goes too. The versions of chapter 16 are in the same log.
- From the command line,
relatude timestampandrelatude revert --after <timestamp>do the same (chapter 31).