flipped-aurora/gin-vue-admin · error

mysql config invalid

Error message

mysql config invalid

What it means

MysqlInitHandler.WriteConfig writes the MySQL settings chosen during init back into config.yaml (setting System.DbType="mysql" and rotating the JWT signing key). It type-asserts the context value "config" to config.Mysql and returns "mysql config invalid" when the assertion fails, meaning the context was not produced by this handler's EnsureDB.

Source

Thrown at server/service/system/sys_initdb_mysql.go:31

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/google/uuid"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type MysqlInitHandler struct{}

func NewMysqlInitHandler() *MysqlInitHandler {
	return &MysqlInitHandler{}
}

// WriteConfig mysql回写配置
func (h MysqlInitHandler) WriteConfig(ctx context.Context) error {
	c, ok := ctx.Value("config").(config.Mysql)
	if !ok {
		return errors.New("mysql config invalid")
	}
	global.GVA_CONFIG.System.DbType = "mysql"
	global.GVA_CONFIG.Mysql = c
	global.GVA_CONFIG.JWT.SigningKey = uuid.New().String()
	cs := utils.StructToMap(global.GVA_CONFIG)
	for k, v := range cs {
		global.GVA_VP.Set(k, v)
	}
	global.GVA_ACTIVE_DBNAME = &c.Dbname
	return global.GVA_VP.WriteConfig()
}

// EnsureDB 创建数据库并初始化 mysql
func (h MysqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {
	if s, ok := ctx.Value("dbtype").(string); !ok || s != "mysql" {
		return ctx, ErrDBTypeMismatch
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Obtain the context from MysqlInitHandler.EnsureDB (which stores conf.ToMysqlConfig() under key "config") before calling WriteConfig.
  2. Prefer the standard InitDBService.InitDB flow with DBType="mysql", which guarantees handler pairing.
  3. Verify no other handler overwrote the "config" key with a different DB struct.
  4. In custom code, set ctx = context.WithValue(ctx, "config", conf.ToMysqlConfig()) first.

Example fix

// before
ctx, _ = pgsqlHandler.EnsureDB(ctx, &conf)
err = mysqlHandler.WriteConfig(ctx) // -> mysql config invalid
// after
ctx, _ = mysqlHandler.EnsureDB(ctx, &conf)
err = mysqlHandler.WriteConfig(ctx)
Defensive patterns

Strategy: type-guard

Validate before calling

if _, ok := ctx.Value("config").(config.Mysql); !ok {
	return errors.New("mysql config missing in context")
}

Type guard

func hasMysqlConfig(ctx context.Context) bool {
	_, ok := ctx.Value("config").(config.Mysql)
	return ok
}

Try / catch

err := handler.WriteConfig(ctx)
if err != nil && err.Error() == "mysql config invalid" {
	// ctx not produced by MysqlInitHandler.EnsureDB; rerun EnsureDB first
}

Prevention

When it happens

Trigger: Calling MysqlInitHandler.WriteConfig(ctx) where ctx has no "config" value, or the value is config.Pgsql/Mssql/Sqlite (set by another handler) rather than config.Mysql.

Common situations: Calling WriteConfig from a different handler's pipeline; custom initialization code building its own context; tests invoking WriteConfig without running EnsureDB first.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/d83d8db257caffb4. Report an issue: GitHub.