flipped-aurora/gin-vue-admin · error

db type mismatch

Error message

db type mismatch

What it means

ErrDBTypeMismatch is a sentinel error in the init-db service of gin-vue-admin. Each DB-specific InitHandler's EnsureDB double-checks that the ctx value "dbtype" matches the handler's own database type; if it does not (or is absent), the handler refuses to run and returns this error. It is an internal consistency guard: the handler chosen in InitDB and the dbtype stamped into the context must agree.

Source

Thrown at server/service/system/sys_initdb.go:42

	Pgsql           = "pgsql"
	Sqlite          = "sqlite"
	Mssql           = "mssql"
	InitSuccess     = "\n[%v] --> 初始数据成功!\n"
	InitDataExist   = "\n[%v] --> %v 的初始数据已存在!\n"
	InitDataFailed  = "\n[%v] --> %v 初始数据失败! \nerr: %+v\n"
	InitDataSuccess = "\n[%v] --> %v 初始数据成功!\n"
)

const (
	InitOrderSystem   = 10
	InitOrderInternal = 1000
	InitOrderExternal = 100000
)

var (
	ErrMissingDBContext        = errors.New("missing db in context")
	ErrMissingDependentContext = errors.New("missing dependent value in context")
	ErrDBTypeMismatch          = errors.New("db type mismatch")
)

// SubInitializer 提供 source/*/init() 使用的接口,每个 initializer 完成一个初始化过程
type SubInitializer interface {
	InitializerName() string // 不一定代表单独一个表,所以改成了更宽泛的语义
	MigrateTable(ctx context.Context) (next context.Context, err error)
	InitializeData(ctx context.Context) (next context.Context, err error)
	TableCreated(ctx context.Context) bool
	DataInserted(ctx context.Context) bool
}

// TypedDBInitHandler 执行传入的 initializer
type TypedDBInitHandler interface {
	EnsureDB(ctx context.Context, conf *request.InitDB) (context.Context, error) // 建库,失败属于 fatal error,因此让它 panic
	WriteConfig(ctx context.Context) error                                       // 回写配置
	InitTables(ctx context.Context, inits initSlice) error                       // 建表 handler
	InitData(ctx context.Context, inits initSlice) error                         // 建数据 handler
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. When calling a handler's EnsureDB directly, stamp the context first: ctx = context.WithValue(ctx, "dbtype", "mysql") matching the handler.
  2. Prefer going through InitDBService.InitDB(conf), which picks the handler and sets dbtype from conf.DBType automatically.
  3. Ensure conf.DBType is one of mysql/pgsql/sqlite/mssql so the switch maps to the intended handler.
  4. If you wrote custom dispatch code, keep the dbtype string value in sync with the handler you instantiate.

Example fix

// before
ctx := context.WithValue(context.TODO(), "dbtype", "pgsql")
ctx, err = mysqlHandler.EnsureDB(ctx, &conf) // -> ErrDBTypeMismatch
// after
ctx := context.WithValue(context.TODO(), "dbtype", "mysql")
ctx, err = mysqlHandler.EnsureDB(ctx, &conf)
Defensive patterns

Strategy: validation

Validate before calling

func dbTypeStamped(ctx context.Context) (string, bool) {
	s, ok := ctx.Value("dbtype").(string)
	return s, ok
}
// before calling EnsureDB: if dbTypeStamped(ctx) != "mysql" { stamp it }

Type guard

func isDBType(ctx context.Context, want string) bool {
	s, ok := ctx.Value("dbtype").(string)
	return ok && s == want
}

Try / catch

ctx, err := handler.EnsureDB(ctx, &conf)
if err != nil {
	if errors.Is(err, system.ErrDBTypeMismatch) {
		// handler/dbtype pair mismatched; route through InitDBService.InitDB instead
	}
	return err
}

Prevention

When it happens

Trigger: Calling a DB-specific EnsureDB handler directly (e.g. MysqlInitHandler.EnsureDB) with a context that has no "dbtype" string value, or one stamped with a different type (e.g. ctx with dbtype="pgsql" passed to the mssql handler). Not reachable via the normal InitDB entry point, which always sets dbtype consistently from conf.DBType.

Common situations: Custom initializer code or plugins that construct their own context and call a specific handler; refactoring where conf.DBType mapping (sys_initdb.go:109-125) was changed or bypassed; tests invoking handlers in isolation without stamping dbtype.

Related errors


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