[BUG] Postgres error when changing column type of a table with dependant view

Open
#280 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
go, postgresql
Domain
databases

Research direction

Start with the schema-diff planning path used by the library and CLI, then reproduce the provided table-and-view change with pg-schema-diff plan and apply. Confirm the generated migration handles the dependent view before changing the column type and leaves the view available afterward.

Written by the indexing model from the issue text.

Description

bug
Describe the bug**

When a table column's type is changed and a view depends on that column, pg-schema-diff generates ALTER TABLE ... ALTER COLUMN ... SET
DATA TYPE without first dropping the dependent view. Postgres rejects the migration:

ERROR: 0A000: cannot alter type of a column used by a view or rule     
Expected behavior**

The view should be marked for recreation (dropped before, recreated after) whenever a column it depends on has its type altered, the same way it already is when a dependent column is deleted.

To Reproduce
  • Start with a schema containing a table and a view selecting a column from it, e.g.:
    CREATE TABLE foo (id int, val text);
    CREATE VIEW foo_v AS SELECT id, val FROM foo;
  • Change the column type in the new schema:
    CREATE TABLE foo (id int, val varchar(100));
    CREATE VIEW foo_v AS SELECT id, val FROM foo;
  • Run pg-schema-diff plan and apply the generated SQL.
  • The ALTER TABLE foo ALTER COLUMN val SET DATA TYPE varchar(100) statement fails with the error above.
Context

pg-schema-diff version: main (reproduces on current main)
pg-schema-diff usage: LIBRARY / CLI (both)

Dominant language
Go
Stars
884
Forks
82
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stripe/pg-schema-diff

All issues in stripe/pg-schema-diff

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.