Skip to content
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

feat: dryrun migration should only print not-select sqls #6828

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ type printSQLLogger struct {

func (l *printSQLLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
sql, _ := fc()
fmt.Println(sql + ";")
if len(sql) < 6 || strings.ToUpper(sql[0:6]) != "SELECT" {
fmt.Println(sql + ";")
Copy link
Member

@a631807682 a631807682 Feb 27, 2024

Choose a reason for hiding this comment

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

The select statement is executed by queryTx and will not be printed. The logs you see are all printed by Trace. https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go#L122
In fact, I don't agree with using fmt.Println here. It is equivalent to searching for the record I need in a lot of logs, and it may be inconsistent with the output of the original log settings, and level control cannot be used.
I think we can provide log implementation for migration through documentation, such as writing the change log to a file through the log.

Copy link
Author

Choose a reason for hiding this comment

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

postgresql dry run mode still need to run many select method and will printout by this method

Copy link
Member

Choose a reason for hiding this comment

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

gorm/migrator/migrator.go

Lines 112 to 114 in 3e2c4fc

if m.DB.DryRun {
queryTx.DryRun = false
execTx = m.DB.Session(&gorm.Session{Logger: &printSQLLogger{Interface: m.DB.Logger}})
It is expected to work in dry run mode.

}
l.Interface.Trace(ctx, begin, fc, err)
}

Expand Down
Loading