跳到内容
光环标志
光环基地

Roll back and repair migrations

Write the SQL that undoes a migration, run it so that the schema and the ledger change together, and put the ledger back in line with the schema when they disagree.

操作指南8分钟 读取级别 中间

此页面尚不支持您的语言。它以英文显示。

Aurabase never guesses how to undo a migration: you write the reverse SQL. This page runs it so that the schema and the ledger of the project change together, and shows how to edit the ledger alone when it no longer matches the schema.

Prerequisites#

  • A linked project: aura link <project-id>.

  • The example schema, installed with the service role key of the project:

    export AURABASE_SERVICE_ROLE_KEY=aura_sk_…
    aura db seed --example docs

    It creates profiles, orgs, org_members, posts, todos, the view open_todos and the function todo_count(p_user uuid). See Example schema.

  • How migrations are applied: SQL migrations.

The examples create two migrations, wf_rb_1_tags and wf_rb_2_labels, each with a down file, and the tables wf_rb_tags and wf_rb_labels; the Clean up block removes them. As on the migrations page, sed replaces the timestamp of the file names with YYYYMMDDHHMMSS.

aura db migration new wf_rb_1_tags > /dev/null
aura db migration new wf_rb_2_labels > /dev/null
one=$(ls aurabase/migrations/*_wf_rb_1_tags.sql)
two=$(ls aurabase/migrations/*_wf_rb_2_labels.sql)
echo "create table wf_rb_tags (id bigint generated always as identity primary key, name text not null);" > "$one"
echo "drop table wf_rb_tags;" > "${one%.sql}.down.sql"
echo "create table wf_rb_labels (id bigint generated always as identity primary key, name text not null);" > "$two"
echo "drop table wf_rb_labels;" > "${two%.sql}.down.sql"
aura db push 2>&1 | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/'
» Applying migrations...
  • Applying to linked project 11111111-2222-4333-8444-555555555555 via API...
    ✔ YYYYMMDDHHMMSS_wf_rb_1_tags.sql applied
    ✔ YYYYMMDDHHMMSS_wf_rb_2_labels.sql applied
✔ 2 migration(s) applied successfully!

Three tools#

ToolRuns SQLChanges the ledgerUse it to
aura db migration down [N] with <version>.down.sql filesThe down file, if it existsRemoves the versionUndo migrations from the CLI. Read the defect below first.
POST /v1/db/{project_id}/migrations/rollback with {"version", "sql"}sql, if givenRemoves the versionUndo one named migration.
aura db migration repair <file> --applied or --reverted, POST …/migrations/repairNeverAdds or removes the versionPut the ledger in line with a schema that is already right.

When SQL runs, it runs in one transaction with the removal from the ledger: either both happen or neither does. The same isolation rules as for migrations apply to it (What a migration may contain). aura migration … is the same command as aura db migration ….

Write a down file#

A down file has the name of its migration with .down.sql instead of .sql, in the same folder: 20261123100000_wf_rb_1_tags.down.sql undoes 20261123100000_wf_rb_1_tags.sql. aura db push, aura db status and aura db diff ignore down files. Write the down file when you write the migration, and undo the statements in reverse order: drop what the migration created, add back what it dropped.

aura db migration down picks the oldest migration#

aura db migration down says it reverts the last N applied migrations (1 by default). On a linked project it reverts the oldest N of the ledger instead (measured with CLI 0.3.4). With the two migrations above, it undoes wf_rb_1_tags, applied first, and leaves wf_rb_2_labels:

aura db migration down 2>&1 | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/g'
aura db status | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/'
» Reverting last 1 migration(s)...
  • Reverting on linked project 11111111-2222-4333-8444-555555555555 via API...
  • Reverting YYYYMMDDHHMMSS_wf_rb_1_tags.sql via aurabase/migrations/YYYYMMDDHHMMSS_wf_rb_1_tags.down.sql...

✔ 1 migration(s) reverted.

» Migration Status - Project 11111111-2222-4333-8444-555555555555
  Migration                                Status       Date
  ------------------------------------------------------------------------
  YYYYMMDDHHMMSS_wf_rb_1_tags.sql          PENDING      -
  YYYYMMDDHHMMSS_wf_rb_2_labels.sql        APPLIED      2026-11-23 10:00:00.000000+00

Until this is fixed, use aura db migration down only when the ledger holds a single migration, and revert a named migration with the route of the next section. When it already ran on the wrong migration, as here, the down file did its work (the table wf_rb_tags is gone) and the version is pending again: aura db push applies it again.

aura db push 2>&1 | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/' | grep wf_rb
    ✔ YYYYMMDDHHMMSS_wf_rb_1_tags.sql applied
    ℹ YYYYMMDDHHMMSS_wf_rb_2_labels.sql (already applied)

Without a down file, down prints has no .down.sql — removing from migration history only.: the schema stays, the ledger forgets the version, and the next aura db push fails with relation "…" already exists. Mark the version applied again with repair --applied (Repair the ledger).

Revert one migration by name#

POST /v1/db/{project_id}/migrations/rollback takes the version to revert and the SQL that undoes it, with the service_role key. Here it reverts wf_rb_2_labels with the content of its down file:

down=$(ls aurabase/migrations/*_wf_rb_2_labels.down.sql)
rollback() {
  node -e 'const fs = require("fs"); const f = process.argv[1]; console.log(JSON.stringify({ version: f.split("/").pop().replace(".down.sql", ".sql"), sql: fs.readFileSync(f, "utf8") }))' "$down" \
  | curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/migrations/rollback" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' -d @- \
  | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/'
  echo
}
rollback
rollback
aura db table list | grep -c wf_rb_labels || true
{"data":{"version":"YYYYMMDDHHMMSS_wf_rb_2_labels.sql","reverted":true,"message":"Migration reverted and removed from the ledger"},"meta":null}
{"data":{"version":"YYYYMMDDHHMMSS_wf_rb_2_labels.sql","reverted":false,"message":"Version not in the ledger (already reverted or never applied)"},"meta":null}
0

The second call changes nothing: the version is no longer in the ledger. The body accepts version and sql only (any other field is refused with 400); without sql, the route removes the version from the ledger and runs nothing. If the SQL fails, the answer is 400 BAD_REQUEST with the PostgreSQL message, and neither the schema nor the ledger changes.

Repair the ledger#

repair writes to the ledger and runs no SQL. Use it when the schema is right and the ledger is not: a change made by hand, a migration recorded under another name, the ledger of a branch, or a version removed by down without its down file. Here the ledger forgets wf_rb_1_tags while its table stays, so push fails, then repair --applied records the file again:

one=$(basename "$(ls aurabase/migrations/*_wf_rb_1_tags.sql)")
aura db migration repair "$one" --reverted | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/'
aura db push 2>&1 | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/' | grep Error || true
aura db migration repair "$one" --applied | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/'
  • Repairing migration on linked project 11111111-2222-4333-8444-555555555555 via API...
  ✔ Migration 'YYYYMMDDHHMMSS_wf_rb_1_tags.sql' marked as REVERTED (no SQL was executed).
Error: Migration 'YYYYMMDDHHMMSS_wf_rb_1_tags.sql' failed: [400] BAD_REQUEST: Invalid request: Migration failed: Invalid request: relation "wf_rb_tags" already exists
  • Repairing migration on linked project 11111111-2222-4333-8444-555555555555 via API...
  ✔ Migration 'YYYYMMDDHHMMSS_wf_rb_1_tags.sql' marked as APPLIED (no SQL was executed).
  • --applied is the default when you give neither flag. It needs the file in aurabase/migrations: the ledger stores the checksum of its SQL, so a later push recognizes it. Without the file: cannot mark '<name>' as applied: file aurabase/migrations/<name> not found (the ledger stores the checksum of its SQL).
  • --reverted removes the version and answers marked as REVERTED even for a version the ledger never held.
  • The route is POST /v1/db/{project_id}/migrations/repair with {"version", "status": "applied" | "reverted", "sql", "description"}; sql is required for applied.
repair() {
  curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/migrations/repair" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' -d "$1"
  echo
}
repair '{"version": "wf_rb_by_hand", "status": "applied", "sql": "create table wf_rb_by_hand (id int)"}'
repair '{"version": "wf_rb_by_hand", "status": "reverted"}'
repair '{"version": "wf_rb_by_hand", "status": "applied"}' | node -e 'console.log(JSON.parse(require("fs").readFileSync(0, "utf8")).error.message)'
{"data":{"version":"wf_rb_by_hand","status":"applied","message":"Ledger updated; no SQL was executed"},"meta":null}
{"data":{"version":"wf_rb_by_hand","status":"reverted","message":"Ledger updated; no SQL was executed"},"meta":null}
Invalid request: Reparing a migration as 'applied' requires its `sql` (the ledger stores its checksum)

The first call recorded a table that does not exist: repair trusts you. Check the schema (aura db table list) before repairing.

What a rollback cannot undo#

  • Data. A down file that adds back a dropped column or table restores the structure, not the rows. Before a destructive migration, export what you may need, or try the change on a branch first.
  • Several migrations at once. Each version is reverted in its own transaction. With down N (or several route calls), a failure stops there: the versions reverted before it stay reverted.
  • What the platform did around the migration. Row level security turned on for new tables, and security_invoker on new views, are part of the objects: dropping the object removes them too.
  • Transaction control and security definer. A down file follows the rules of a migration: no begin/commit, no security definer.

Prefer changes that need no rollback: add before you remove, and remove in a later migration (Change a busy table).

Result#

aura db status | sed -E 's/[0-9]{14}_/YYYYMMDDHHMMSS_/'
aura db table list | grep wf_rb
» Migration Status - Project 11111111-2222-4333-8444-555555555555
  Migration                                Status       Date
  ------------------------------------------------------------------------
  YYYYMMDDHHMMSS_wf_rb_1_tags.sql          APPLIED      2026-11-23 10:00:00.000000+00
  YYYYMMDDHHMMSS_wf_rb_2_labels.sql        PENDING      -
  • wf_rb_tags [RLS]

wf_rb_1_tags is applied and its table exists; wf_rb_2_labels was reverted by the route and its table is gone.

Common errors#

MessageCauseFix
<version> was NOT reverted: Version not in the ledger (already reverted or never applied)down asked the server to revert a version the ledger no longer holds.Read the ledger with aura db status.
has no .down.sql — removing from migration history only.No down file next to the migration.Undo the schema by hand or with a new migration, or record it again with repair --applied.
relation "…" already exists on the next pushThe ledger forgot a version whose objects still exist.aura db migration repair <file> --applied.
cannot mark '<name>' as applied: file aurabase/migrations/<name> not foundrepair --applied (or repair without a flag) on a name with no local file.Use the exact file name, or --reverted.
Invalid JSON body — down_sql: unknown fieldThe rollback route only accepts version and sql.Rename the field to sql.
400 BAD_REQUEST, Migration failed: … on a rollbackThe down SQL failed; nothing changed.Fix the down SQL and call the route again.

Clean up#

aura db raw "drop table if exists wf_rb_tags; drop table if exists wf_rb_labels"
for f in aurabase/migrations/*_wf_rb_*.sql; do
  case "$f" in *.down.sql) continue ;; esac
  aura db migration repair "$(basename "$f")" --reverted > /dev/null
done
rm -rf aurabase/migrations
aura db status
  ℹ No results (0 rows).

» Migration Status - Project 11111111-2222-4333-8444-555555555555
  ℹ No migrations found, local or applied. Create one with aura db migration new <name>.

See also#

@aurabase/aurabase-js@0.7.5 · aura@0.3.4↗ 报告问题