fix(migrator): normalize DB-side "NULL" default to fix infinite AlterColumn loop on Postgres - #7806
Open
boobusy wants to merge 1 commit into
Open
fix(migrator): normalize DB-side "NULL" default to fix infinite AlterColumn loop on Postgres#7806boobusy wants to merge 1 commit into
boobusy wants to merge 1 commit into
Conversation
…Column loop on Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Normalize DB-side
DEFAULT NULLliteral to prevent infiniteAlterColumnloop on PostgreSQL.What did this pull request do?
!strings.EqualFold(dv, "NULL")to the first default value comparison case inMigrateColumndefault:NULLtag is treated as no-defaultAlterColumnfrom being triggered on every startup when a PostgreSQL column hasDEFAULT NULLin itscolumn_defaultcatalog entryUser Case Description
PostgreSQL's
information_schema.columns.column_defaultstores the literal string"NULL"for columns explicitly defined withDEFAULT NULL, while MySQL and other drivers return SQL NULL (empty) for the same semantic state.GORM's
MigrateColumnalready normalizes the model side —default:NULLtag is treated as "no default" viaEqualFold. But the DB side comparison was missing the same normalization, causing:dvNotNull = true(DB returns string"NULL")currentDefaultNotNull = false(model side normalized to no-default)This triggers
AlterColumnon every startup. The postgres driver then issuesALTER COLUMN TYPE ... USING ...(full table rewrite holding AccessExclusiveLock) due to a separate alias-comparison bug, turning a metadata mismatch into a production outage on large tables.Affected drivers: PostgreSQL only. MySQL/SQLite/SQLServer all collapse
DEFAULT NULLto "no default" in their column metadata.Related: AutoMigration always attempts to set the default value, even when it hasn’t changed. #7553, If a field in a database table uses the serializer tag and AutoMigrate is used, then even if there are no changes to this field, AutoMigrate will still perform an ALTER on the table every time #7590, fix(migrator): allow AutoMigrate to update explicit PK defaults #7769