pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Dealing with migrations that impact existing data

Open lelit opened this issue 3 months ago • 2 comments

I could not find a reference to that in the examples and in the documentation: how do you approach migrations that involves existing data? That is clearly a broad problem, that in general cannot be solved automatically, but I wonder if/how that particular logic could be hooked in the workflow.

lelit avatar Aug 29 '26 06:08 lelit

Thanks for raising this. You're right that this can't be solved automatically in general — pgschema deliberately manages schema state only, not data.

The supported hook is editing the generated plan.json: declare the end state in your schema file, then insert data migration steps (e.g. backfill UPDATEs) into the plan before applying. The plan fingerprint pins the database state, not the plan SQL, so edits are safe and apply still detects concurrent drift. Since the database converges to the schema file after apply, subsequent plans are empty and the data migration naturally runs once.

I've just documented this with a worked example (add column → backfill → SET NOT NULL): https://www.pgschema.com/workflow/data-migration

Related discussions for other intent-that-can't-be-inferred cases:

  • #49 — explicit rename workflow
  • #52 — force-including raw SQL files as plan steps

Keeping this open to track whether data migrations deserve more first-class support.

tianzhou avatar Aug 30 '26 10:08 tianzhou

Thank you for the clarification.

lelit avatar Aug 31 '26 05:08 lelit