Questions about migrations and workflow
Hey! I found this project today and it looks promising. I tried looking for a place to ask questions but github discussions are disabled and I couldn't find a forum or discord link, so I am creating an issue.
I am trying to get used to the idea of creating plans rather than creating migrations, but I am stuck on:
- How do I deal with complex migrations? I like to use the dumb example of moving user.address TEXT column to its own table with street, postcode etc columns. This requires a lot more than just creating a table and a relation.
- Related, how do I deal with renames? I looked at a bunch of tools, one of which was sqldef which has an annotation for this:
-- @renamed from=user_accountsfor example. I couldn't find a solution in the documentation. - What is the workflow supposed to be like, do I check these plans into version control? The docs seem to say I do not, with sentences like "When a PR/MR is opened, CI automatically runs pgschema plan". But this confuses me, because this seems to imply I cannot alter the plan. What if the proposed plan is not to my liking? What if more complex or involved solution is needed? Do I pull every time or do I chech the schema in to version control?
- I find the whole flow on https://www.pgschema.com/workflow/gitops lacking some answers such as trying out the plan on a staging environment, or a branch (planetscale, neon etc) before merging and applying to production.
I come from a very traditional migration based workflow so I am having some trouble adjusting to this concept. I hope you can help me, as I am looking for a better solution (prevent drift, conflicts in epic branches etc).
Thank you for reading
Thanks for checking out pgschema and great questions! Let me address each one:
1. Complex migrations (e.g., splitting a column into a new table)
This is a known limitation of any declarative/state-based migration tool (including Terraform). pgschema compares desired state vs. current state and generates DDL to bridge the gap — it doesn't know how you want data moved, only what the final schema should look like.
For complex migrations that involve data transformations (like moving user.address into an addresses table), the recommended approach is to break it into multiple steps:
- Step 1: Add the new
addressestable (edit schema file, plan, apply) - Step 2: Run a manual data migration script (outside pgschema) to populate the new table
- Step 3: Remove the old
addresscolumn fromusers(edit schema file, plan, apply)
pgschema handles the DDL parts; data migration scripts live alongside your schema files and are executed separately. This is similar to how Terraform handles stateful resources — the tool manages structure, but data movement is a separate concern.
2. Renames
RENAME is currently not supported. Since pgschema compares desired vs. current state, renaming user_accounts to users would appear as "drop user_accounts, create users" — it can't infer rename intent.
For renames, the same multi-step approach applies:
- Create the new table/column
- Migrate data manually
- Drop the old table/column
Annotation-based rename hints (like sqldef's -- @renamed) are an interesting idea but not currently on our roadmap.
3. What goes into version control?
Schema files (schema.sql): Yes, always checked into version control. This is your source of truth.
Plan files (plan.json): This depends on your workflow:
-
GitOps workflow (documented here): CI generates the plan automatically on PR, posts it as a comment for review. If you don't like the plan, you adjust your schema file and push again — CI regenerates the plan. The plan artifact is saved and used by CD to apply after merge. You don't check
plan.jsoninto version control in this case. -
Local to Production workflow (documented here): You can generate
plan.jsonlocally or in CI, review it, and commit it alongside schema changes.
To answer "what if the proposed plan is not to my liking?" — you adjust your schema file. The plan is deterministic based on the diff between desired state and current state. If the plan does something you don't want (e.g., drops and recreates a table instead of altering it), that's either a pgschema improvement opportunity (file an issue!) or a case where you need to break the change into smaller steps.
4. Staging/branch environments
The Local to Production workflow covers this:
- Generate the plan against your staging database
- Apply to staging first:
pgschema apply --host staging.db.com --plan plan.json - Test on staging
- Apply the same plan to production:
pgschema apply --host prod.db.com --plan plan.json
pgschema's fingerprinting mechanism ensures that if the production schema differs from what the plan expects, the apply is rejected safely — you'll need to regenerate the plan.
For branch databases (Neon, PlanetScale-style), pgschema works great — just point --host at your branch database to generate and test plans before merging.
The CI examples in the GitOps docs show complete GitHub Actions, GitLab CI, and Azure Pipelines workflows that automate the plan-on-PR, apply-on-merge pattern.
In summary: pgschema excels at structural DDL changes where the tool can deterministically compute the diff. For data migrations and renames, you'll need to combine pgschema steps with manual scripts. This is a fundamental trade-off of declarative tools — you gain drift detection, conflict prevention, and reproducibility, but complex stateful transformations require manual orchestration.
Hope this helps with the transition from imperative migrations! Feel free to open follow-up issues for specific workflow questions.