Skip noop migrations, instead of executing SELECT 0. (#103086)

* Skip noop migrations, instead of executing SELECT 0.
This commit is contained in:
Peter Štibraný 2025-03-31 15:19:32 +02:00 committed by GitHub
parent f0a6327edc
commit f7f28757f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 8 deletions

View File

@ -74,7 +74,6 @@ type Dialect interface {
CleanDB(engine *xorm.Engine) error CleanDB(engine *xorm.Engine) error
TruncateDBTables(engine *xorm.Engine) error TruncateDBTables(engine *xorm.Engine) error
NoOpSQL() string
// CreateDatabaseFromSnapshot is called when migration log table is not found. // CreateDatabaseFromSnapshot is called when migration log table is not found.
// Dialect can recreate all tables from existing snapshot. After successful (nil error) return, // Dialect can recreate all tables from existing snapshot. After successful (nil error) return,
// migrator will list migrations from the log, and apply all missing migrations. // migrator will list migrations from the log, and apply all missing migrations.
@ -353,10 +352,6 @@ func (b *BaseDialect) CreateDatabaseFromSnapshot(ctx context.Context, engine *xo
return nil return nil
} }
func (b *BaseDialect) NoOpSQL() string {
return "SELECT 0;"
}
func (b *BaseDialect) TruncateDBTables(engine *xorm.Engine) error { func (b *BaseDialect) TruncateDBTables(engine *xorm.Engine) error {
return nil return nil
} }

View File

@ -53,7 +53,7 @@ func (m *RawSQLMigration) SQL(dialect Dialect) string {
} }
} }
return dialect.NoOpSQL() return ""
} }
func (m *RawSQLMigration) Set(dialect string, sql string) *RawSQLMigration { func (m *RawSQLMigration) Set(dialect string, sql string) *RawSQLMigration {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strings"
"time" "time"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -392,8 +393,12 @@ func (mg *Migrator) exec(ctx context.Context, m Migration, sess *xorm.Session) e
err = codeMigration.Exec(sess, mg) err = codeMigration.Exec(sess, mg)
} else { } else {
sql := m.SQL(mg.Dialect) sql := m.SQL(mg.Dialect)
logger.Debug("Executing sql migration", "id", m.Id(), "sql", sql) if strings.TrimSpace(sql) == "" {
_, err = sess.Exec(sql) logger.Debug("Skipping empty sql migration", "id", m.Id())
} else {
logger.Debug("Executing sql migration", "id", m.Id(), "sql", sql)
_, err = sess.Exec(sql)
}
} }
if err != nil { if err != nil {