Skip to content

fix: preserve typmods on timestamp, time, interval and bit columns (#699) - #723

Merged
tianzhou merged 3 commits into
mainfrom
devin/1795883257-time-bit-typmods
Nov 29, 2026
Merged

tianzhou merged 3 commits into
mainfrom
devin/1795883257-time-bit-typmods

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #699. resolved_type in GetColumns / GetColumnsForSchema used the bare udt_name for every pg_catalog base type, relying on character_maximum_length / numeric_precision for modifiers. Date/time and bit-string typmods aren't exposed there, so timestamp(3), timetz(1), interval hour to minute, bit(3), varbit(4), ... were dumped bare (a replayed bit is bit(1)) and plan saw no difference.

When atttypmod >= 0:

WHEN typname = 'interval' THEN format_type(atttypid, atttypmod)            -- interval(3), interval hour to minute
WHEN typname IN ('timestamp','timestamptz','time','timetz','bit','varbit')
  THEN udt_name || substring(format_type(atttypid, atttypmod) FROM '\([^)]*\)')  -- timestamp(3), bit(3)

Columns without a modifier (atttypmod = -1) are unchanged. varchar/numeric/char keep their existing path. A typmod change now produces ALTER COLUMN ... TYPE.

needsUsingClause adds an explicit cast when widening bit(n): assignment coercion rejects bit strings of a different length, so bit(3) → bit(5) would fail on a populated table; the explicit cast zero-pads. Narrowing gets no cast (it would silently drop bits) and fails on data, like varchar narrowing.

ir/queries/queries.sql.go was updated by hand to mirror queries.sql (sqlc generate needs a live DB).

Test: extended create_table/alter_column_types with typmod changes on every affected type, an unchanged timestamp(3) column (no diff), an added timestamp(0) column, and a populated row so the bit widening is applied against data. Before the fix plan only emitted ADD COLUMN added_at timestamp. Note the interval → interval day to second(2) change gets a redundant but valid USING clause.

Link to Devin session: https://app.devin.ai/sessions/314326a464fb4b3c9fbe6ccc32736cdf
Open in Devin Desktop: https://app.devin.ai/desktop/session/314326a464fb4b3c9fbe6ccc32736cdf?variant=devin
Requested by: @tianzhou


🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 · Fix fixed-length bit conversions before merging; the generated migration fails on populated columns.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches PostgreSQL schema inspection and the plans generated from it.

Fix fixed-length bit conversions before merging; the generated migration fails on populated columns.

Findings

  1. P1 Bit width changes fail ▶

Summary

Preserves precision, interval fields, and bit lengths when inspecting columns.

  • Schema plans now retain modifiers on date, time, interval, and bit columns.

Reviews (1) · Last reviewed commit: "fix: preserve typmods on timestamp, time..." · Reviewed by Greptile


ALTER TABLE user_pending_permissions ALTER COLUMN slot TYPE interval day to second(2) USING slot::interval day to second(2);

ALTER TABLE user_pending_permissions ALTER COLUMN flags TYPE bit(5);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Bit width changes fail

Changing a populated bit(3) column to bit(5) produces this statement without USING. PostgreSQL does not pad existing bit strings during this implicit conversion, so three-bit values make the migration fail.

Update needsUsingClause to handle fixed-length bit changes and emit USING flags::bit(5). Add a test that applies the migration to existing rows.

devin-ai-integration Bot and others added 2 commits November 28, 2026 16:45
Co-Authored-By: Tianzhou <t@bytebase.com>
Co-Authored-By: Tianzhou <t@bytebase.com>

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@tianzhou
tianzhou merged commit 9260370 into main Nov 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dump drops type modifiers on timestamp, time, interval and bit columns; plan does not detect the loss

1 participant