Ask
152
@goldenhourgus ·

how do people actually run d1 migrations in production, is wrangler apply it Wrangler

Coming from Rails, where migrations are a solved and slightly boring thing. Getting started on Workers and D1 and I cannot tell what the normal workflow is.

Is wrangler d1 migrations apply what people genuinely use in production, or is that the starter-project answer and everyone actually uses drizzle-kit or something else? Is there a rollback? Do people run this from CI or from their own machine? I would rather copy an established habit than invent one badly.

9 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @deploy_friday_ok · 2w ago · 3 replies

    Yes, wrangler d1 migrations apply <db> --remote is the real answer, not the toy one. The setup most teams converge on:

    • SQL files in migrations/, either hand-written or produced by drizzle-kit generate
    • --local against your dev database while you work
    • CI runs the --remote apply as a step before wrangler deploy

    Two things to internalise coming from Rails.

    There is no down migration. Wrangler records applied filenames in a d1_migrations table and only ever moves forward. So you write expand-then-contract: add the nullable column, deploy code that writes both old and new, backfill, and only in a later migration drop the old one. A rollback is a new migration, always. This feels primitive for about a month.

    And take an export before anything destructive:

    wrangler d1 export app-db --remote --output backup.sql
    

    It is slow and you will be glad exactly once, which is enough.

    87
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @ridgeline_rowan · 2w ago

      The no-down-migration thing feels primitive right up until you admit you have never once successfully run a down migration against a production database in your life. I certainly have not, and I wrote hundreds of them.

      21
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @goldenhourgus · 2w ago

      Expand-then-contract is the concept I was missing, and it makes the absence of rollbacks feel deliberate rather than unfinished. Thank you for spelling out the four steps.

      26
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @first_repo_finn · 2w ago

    From CI, never from a laptop — that answers your last question and it is the one with the most consequences. The moment two people can apply migrations from their own machines you get a schema nobody can reconstruct from the repository, and you find out during an incident.

    33
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @orm_tamsin · 3w ago · 2 replies

    drizzle-kit and D1 work well together and it is not either/or. drizzle-kit generate writes plain SQL into your migrations folder and wrangler applies it — you keep typed queries and the migration is still a file a human can read in review. That readability matters more than it sounds when someone has to approve a schema change at 6pm on a Thursday.

    44
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @visa_run_val · 2w ago

      Just point drizzle.config.ts at the same migrations directory wrangler uses, otherwise you end up with two folders and a very confusing afternoon.

      15
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @boundaries_bo · 3w ago

    One trap specific to this setup: anything in the migrations folder gets applied, including a seed.sql someone dropped in there for convenience. If that file starts with DELETE FROM, it will do exactly what it says against production the first time CI runs the apply. Keep seed data outside migrations/ and run it with an explicit separate command.

    22
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @seedstart_sim · 2w ago · 2 replies

    Coming from Rails you will miss the schema dump more than the rollbacks — there is no single file you can open to see the current shape of the database. Generate one in CI after applying and commit it, purely so code review can see what changed.

    13
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @goldenhourgus · 3w ago

      Committing a generated schema dump is exactly the habit I was hoping someone would hand me. Adding it to the deploy job today.

      8
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report