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

Migration: smarter SQL‐type alias detection & loop fix to avoid redundant ALTERs

Improves how the migrator recognises equivalent column types that are expressed through driver-specific aliases (e.g. varchar[] vs character varying[]). Length/array specifiers are stripped and alias lookup is attempted in both directions, eliminating superfluous migrations (especially on Postgres arrays). Also fixes an infinite-loop bug in the new comparison loop, removes unused SQLite deps, and adds an extensive test-suite that covers length, array and bidirectional alias scenarios.

Key Changes

• Enhanced MigrateColumn logic in migrator/migrator.go to strip (, [ specifiers and compare aliases both ways, setting alterColumn only when truly needed
• Corrected loop condition to for i := 0; !isSameType && i < len(types); i++ preventing potential infinite loops
• Added comprehensive tests in tests/migrate_test.go for length specifiers, Postgres array types, and bidirectional alias handling; auxiliary test logger introduced
• Expanded tests/upsert_test.go variables and assertions for pipeline stability
• Tidied go.mod / go.sum: removed indirect SQLite driver dependencies no longer required

Affected Areas

migrator/migrator.go – type comparison & migration decision path
tests/migrate_test.go, tests/upsert_test.go – new coverage
go.mod, go.sum – dependency set

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?

@propel-code-bot propel-code-bot bot changed the title Fix #6557: Prevent AutoMigrate from remigrating columns with type aliases Fix #6557: Skip redundant migrations for columns with type aliases Dec 1, 2025
@propel-code-bot propel-code-bot bot changed the title Fix #6557: Skip redundant migrations for columns with type aliases Fix #6557: Avoid redundant column migrations when using type aliases Dec 1, 2025
@PerryProjects
Copy link

Hey @jinzhu,
Hey @DasJott,

i added some tests for the given changes. Hopefully the tests were sufficient.

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.

3 participants