Skip to content

AZ607: The database holds the migration already, or isn't where it starts ​

Each policy migration starts from the policy the one before it left, recorded in authz.policy_versions. This database is somewhere else: a migration was skipped, or the database was changed with rowstile push or apply since. Apply the migrations in order; a database changed by push takes the migrations from the start, or rowstile apply of the policy the previous migration left. A development database that rowstile dev or push brought to the newest policy already holds what this migration brings, and the migration says so ("this database already holds what this migration brings"): don't run it there. Tell the migration tool it is applied, or make the database again from the migrations. Alembic, Prisma, Django, Flyway and sqlx have a command that records a migration as applied; the others keep what ran in a table of their own, and a row in it does:

  • Alembic: alembic stamp head.
  • Prisma: prisma migrate resolve --applied <the migration's folder>, which also clears the failed migration Prisma kept.
  • Django: python manage.py migrate <app> <the migration> --fake.
  • Flyway: flyway -skipExecutingMigrations=true -target=<its version> migrate, which records the migrations up to it without running them.
  • sqlx: sqlx migrate override skip --target-version <its number> (sqlx-cli 0.9 or newer), the same.
  • Rails: INSERT INTO schema_migrations (version) VALUES ('<the number its file name starts with>').
  • Ecto: INSERT INTO schema_migrations (version, inserted_at) VALUES (<the number its file name starts with>, now()).
  • Laravel: INSERT INTO migrations (migration, batch) VALUES ('<its file name without .php>', <the last batch + 1>).
  • EF Core: INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('<the id in its [Migration] attribute>', '<EF Core's version>').
  • goose: INSERT INTO goose_db_version (version_id, is_applied) VALUES (<its number>, true).
  • dbmate: INSERT INTO schema_migrations (version) VALUES ('<its number>').
  • Drizzle Kit: INSERT INTO drizzle.__drizzle_migrations (hash, created_at) VALUES ('<the SHA-256 of its .sql file>', <the when of its entry in meta/_journal.json>). Drizzle runs the migrations whose when is after the latest created_at there.
  • SQL (files you run yourself): don't run this one there.

Reported when a policy migration runs.