# Dumps and Stale Dates (database.fast-forward)

A database dump keeps the dates of the day it was taken. Restore it into a
sandbox months later and every trial, token and scheduled item in it has
already expired, so the app behaves like a dead system. The built-in
operation `database.fast-forward` moves a restored Postgres database's
dates forward to the moment the snapshot was built, so the data looks
freshly captured. It changes data only; the sandbox's clock is already
correct (see "What is not faked" below).

## Declare the database

A top-level `databases` block names each database and the date its dump
was captured. Like every top-level field, it works in GitHub-delivered
and pushed definitions alike.

```yaml
databases:
  app:
    engine: postgres
    url: ${env.DATABASE_URL}
    as_of: 2026-09-30T00:00:00Z
```

- `engine`: `postgres` is the only engine today.
- `url`: Exactly one `${env.NAME}` reference and nothing else around
  it (no `postgres://${env.HOST}/db`). It is resolved inside the
  sandbox, against the env an action sees: the definition's
  `env` (with `${params.*}` filled in), the environment's `env`,
  the emulators' env and repository secrets. So a `DATABASE_URL` in `env`, or a
  secret of that name, works, and no credential is committed.
- `as_of`: When the dump was captured, in UTC with a trailing `Z`. A date
  without a time, or a local offset, is rejected by `validate`.

## Add the step after the restore

A seed step runs the operation with `operation:` instead of `action:`
(see `seeds-and-actions`). It must come after the step that restores
the dump:

```yaml
environments:
  default:
    description: A customer database restored from a 2026-09-30 dump,
      then moved forward to the snapshot's build time.
    seeds:
      - { id: restore, action: restore-dump }
      - { id: fast_forward, operation: database.fast-forward, input: { database: app } }
```

`input.database` is a key of `databases`; `validate` reports an unknown
one with its path. A restore that recreates the database also clears
the record of earlier fast-forwards (below), which is right: the data is
old again. A restore that only replaces tables must also run
`drop schema if exists cofactor cascade`, or a later fast-forward
starts from the old record and moves nothing (the worked example's
dump does this).

## When it runs is where you put it

There is no mode flag. Placement is the "when":

- As a seed step it runs once, while the version's snapshot is built,
  so every claim of that snapshot starts with shifted data.
- On a live sandbox, run the same operation by hand:

  ```bash
  cofactor sandbox run <id> database.fast-forward --input database=app
  ```

  It shifts from the date the database recorded last time (below), so
  a sandbox kept for days can be brought current again.

A Workflow can also run it as a regular sandbox action node. Once the
definition declares `databases`, action discovery lists
`database.fast-forward`; set its `database` input to a key from that block.
The node validates the same built-in input contract as seed and sandbox runs,
and registers the declared outputs (`database`, `offset_days`, `tables`,
`rows`, `from`, `to`) as `$var.<name>` values. A later node can read a value
such as `$var.offset_days`. The seed-chain example in "Output" below uses
`${fast_forward.offset_days}` because that is seed interpolation syntax; it
is not Workflow variable syntax. No emulator plugin named `database` is
required.

## Reproducible Workflow proof

To verify the operation without shifting the application's own data, use a
Run-owned sandbox and a separate fixture database. Declare a database key
whose URL points to that fixture database, create it and a test-owned table
with an old timestamp from a Workflow SQL action, then run
`database.fast-forward`. A SQL assertion can raise an error unless the row
reaches the current UTC date and `$var.offset_days` is positive. Run the
operation again the same day and assert the offset is zero and the row is
unchanged. Finally reset the Run-owned sandbox and assert the fixture database
is gone. Keep the declared date scoped to this fixture database; never point
the proof at the application's own database.

## The shift rule

- `as_of` is the anchor. The data moves by the whole days between its
  current date and now, rounded down, so it ends at most a day behind
  now and never in the future. Times of day are kept: a row at 14:30
  still reads 14:30.
- The first run starts from `as_of`. When it finishes it records the
  data's new date in the database itself, in the table
  `cofactor.fast_forward` (its own schema, so your migration tool does
  not see it). Later runs start from that date instead of `as_of`, and
  the record travels with the data into every snapshot and copy.
- If less than a day has passed the run changes no table (`offset_days: 0`). An `as_of` in the future fails.

## What is shifted

Every `timestamp`, `timestamptz` and `date` column of every base table
in every schema except the system ones and `cofactor`. `infinity`
values stay `infinity`.

- A generated column is recomputed from its shifted inputs.
- A partitioned table is updated through its parent, so rows move
  between partitions as their dates move.
- User triggers are disabled while a table is shifted, so a trigger
  such as `updated_at = now()` leaves `updated_at` with its shifted
  value. Afterwards every user trigger on a touched table is enabled
  again, including one your dump had deliberately disabled.

## What is not shifted

Integer epoch columns, dates inside JSON, dates stored as text, columns
of a domain over a date type, array columns, and materialized views.
Refresh materialized views in a step after the fast-forward:

```yaml
- { id: refresh, action: refresh-views }
```

## Requirements and failures

- The role in `url` must own the tables (it is normally the role that
  restored the dump). A role that does not fails with Postgres's own
  "must be owner of table" error.
- The whole shift is one transaction. Any error rolls everything back,
  so nothing is half-shifted, and the error names the table (and the
  constraint, when there is one):

  ```text
  table public.sessions, constraint sessions_day_key: duplicate key ...
  ```

- A unique constraint over a date column can collide while its rows are
  moving; that fails the run. A foreign key whose columns are shifted
  dates is checked as each table moves and can fail the run with
  Postgres's own error.
- On a live sandbox the shift locks every table that has a date column
  until it commits. Run it when the app is idle.
- After a large dump, follow the step with an action that runs
  `VACUUM (ANALYZE)`: the update leaves dead rows and stale statistics
  behind.

## What is not faked

The sandbox's system clock is not changed, and does not need to be: it
is correct after every claim. Only data moves. So state minted in memory
while the snapshot was built (sessions, tokens an app created during a
seed) is as old as the snapshot. A claim's data is as old as its
snapshot plus under a day of rounding, and a snapshot is rebuilt on
every push, so keep pushing (a nightly push is plenty). A preview moved
in place to a new commit keeps its data and is not shifted again.

## Output

The step (and a `sandbox run`) returns:

- `database`: the key in `databases`
- `offset_days`: whole days the data moved this run (0: nothing to do)
- `tables`, `rows`: how many tables and rows were updated
- `from`, `to`: the data's date before and after (ISO-8601, UTC)

A later step can use them, e.g. `${fast_forward.offset_days}`.
`cofactor sandbox show` lists each seed step with its time and prints
these under the seed outputs.

## Worked example

A complete Postgres app definition combines a dated dump, a restore
action, the `databases` block and the two-step seed above, plus an `aged`
environment that rewinds the data and fast-forwards it a second time.
