Skip to content

Conversation

@DasJott
Copy link

@DasJott DasJott commented Nov 13, 2025

  • [ X ] Do only one thing
  • [ X ] Non breaking API changes
  • [ X ] Tested

What did this pull request do?

Improves the mapping of types to aliases.

The mapping keys do not (and CAN not) include any additional information behind the actual type name.
Therefore to check for aliases in a certain mapping we need to strip that info off, like:
varchar(255)[] => varchar.
To really be sure for the mapping to work reliably the types are mapped best in both ways, like:
varchar => character varying, character varying => varchar.
For at least postgres this was not the case for exactly that case.
For not having to touch all the other driver packages, this code simply tries to map the other way around when the first attempt failed.

User Case Description

We had a data struct containing a field pq.StringArray with gorm:"type:varchar[]". That type was returned by postgres as character varying[]. The current code could not map that correctly and therefore on every service restart an unnecessary migration was started (which took very long on a table containing millions of entries).

This is fixed with this PR.

@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Nov 13, 2025

Improve AutoMigrate type-alias detection to prevent redundant migrations

Enhances the logic that compares struct field types against existing database column types during AutoMigrate. The new algorithm strips length/array modifiers, checks aliases bidirectionally, and therefore recognises cases such as varchar[] vs character varying[]. Indirect SQLite deps were also removed from Go modules.

Key Changes

• Re-implemented alias comparison in migrator/migrator.go:MigrateColumn – now normalises both fullDataType and realDataType, removes size/array suffixes, and looks up aliases in both directions
• Fixed loop condition to for i := 0; !isSameType && i < len(types); i++ { to avoid infinite iteration
• Added explanatory comments around the new comparison logic
• Removed unused blank lines and minor clean-ups in two other code blocks
• Deleted indirect deps github.com/mattn/go-sqlite3 and gorm.io/driver/sqlite from go.mod/go.sum

Affected Areas

migrator/MigrateColumn smart-migrate logic
• Go module dependency graph (SQLite indirect deps removed)

This summary was automatically generated by @propel-code-bot

@propel-code-bot propel-code-bot bot changed the title Fixes #6557 - AutoMigrate re-migrates some types Fix #6557: Prevent AutoMigrate from remigrating columns with type aliases Nov 13, 2025
@jinzhu
Copy link
Member

jinzhu commented Nov 14, 2025

Hi @DasJott

Can you add some tests?

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.

2 participants