Skip to content

fix decimal migrate error. #7450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged

fix decimal migrate error. #7450

merged 2 commits into from
Jun 6, 2025

Conversation

Chise1
Copy link
Contributor

@Chise1 Chise1 commented May 12, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

修复automigrate无法正确识别decimal精度是否变化的问题。

User Case Description

参考issues 7449

如果有一个对象如下:

type Change struct {
	RecID int64  `gorm:"column:recid;type:decimal(9,0);not null" json:"recid"`
}

每次自动同步都会触发

 ALTER TABLE `changes` MODIFY COLUMN `recid` decimal(9,0) NOT NULL    

@Chise1
Copy link
Contributor Author

Chise1 commented May 12, 2025

我在代码里面没有看到automigrate相关的测试内容。
如果需要补充测试代码,请留言。

Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

Can you add unit tests for my question?

Chise1 added a commit to Chise1/gorm that referenced this pull request May 19, 2025
Signed-off-by: Chise1 <chise123@live.com>
Chise1 added a commit to Chise1/gorm that referenced this pull request May 19, 2025
Signed-off-by: Chise1 <chise123@live.com>
Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

I think this can be a temporary solution, and the comments need to be handled later.
#7450 (comment)
#7450 (comment)

I think the specific types (decimal/numeric) should be abstracted out and handled by the driver, which may require extending the ColumnType interface

@jinzhu jinzhu requested a review from Copilot May 22, 2025 03:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where AutoMigrate would always emit an ALTER for decimal/numeric fields even when precision/scale hadn’t changed. It adds targeted precision checks in the migrator and a new test suite to verify correct behavior.

  • Added precision-aware logic for decimal/numeric in Migrator.MigrateColumn
  • Introduced TestAutoMigrateDecimal and helpers to validate no-op versus real ALTERs
  • Updated go.mod to bump GORM and driver versions

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/migrate_test.go Added testAutoMigrateDecimal, decimalColumnsTest, and TestAutoMigrateDecimal
tests/go.mod Bumped gorm.io/gorm to v1.26.1 and related driver versions
migrator/migrator.go Enhanced decimal/numeric precision detection to prevent redundant ALTERs
Comments suppressed due to low confidence (2)

tests/migrate_test.go:2004

  • [nitpick] The Go convention is to use uppercase acronyms; rename modifySql to modifySQL for consistency.
    var modifySql []string

tests/migrate_test.go:2028

  • [nitpick] Rename modSql to modSQL to follow Go naming conventions for acronyms.
    modSql := testAutoMigrateDecimal(t, t1, t2)

Chise1 added a commit to Chise1/gorm that referenced this pull request May 30, 2025
Signed-off-by: Chise1 <chise123@live.com>
Chise1 added a commit to Chise1/gorm that referenced this pull request May 30, 2025
Signed-off-by: Chise1 <chise123@live.com>
Signed-off-by: Chise1 <chise123@live.com>
# Conflicts:
#	tests/go.mod
@Chise1 Chise1 requested a review from demoManito May 30, 2025 11:32
@jinzhu jinzhu merged commit 842ee52 into go-gorm:master Jun 6, 2025
21 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.

4 participants