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

Conversation

shushenghong
Copy link

some driver migration may run select querys to compare schemal change, like postgresql. we should only print not-select sqls when dry run migration

@@ -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.

@jinzhu jinzhu closed this Apr 26, 2024
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