Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NormalizeDBType ¶
NormalizeDBType maps configured database type aliases onto the canonical engine names used by the registry.
func SetLoggerFallback ¶
func SetLoggerFallback(fn func() Logger)
SetLoggerFallback registers the function used to obtain a default logger when callers pass nil.
func SetSchemaNameResolver ¶
func SetSchemaNameResolver(fn func() string)
SetSchemaNameResolver registers the function used to resolve the current schema (database) name for engines that need schema-qualified identifiers.
Types ¶
type ColumnSpec ¶
type ColumnSpec struct {
Name string
Type string
NotNull bool
Default string
PrimaryKey bool
UniqueTag string
AutoIncrement bool
RenameFrom string
}
ColumnSpec describes a table column as declared by models (desired state) or reported by database introspection (existing state).
type ConnectionConfig ¶
type ConnectionConfig struct {
Type string `json:"type" toml:"type"` // postgres、mysql、dameng、sqlite、kingbase
Host string `json:"host" toml:"host"`
Port int `json:"port" toml:"port"`
Username string `json:"username" toml:"username"`
Password string `json:"password" toml:"password"`
DBName string `json:"dbname" toml:"dbname"`
AdminDBName string `json:"admin_dbname" toml:"admin_dbname"`
SSLMode string `json:"sslmode" toml:"sslmode"`
MaxIdleConns int `json:"max_idle_conns" toml:"max_idle_conns"`
MaxOpenConns int `json:"max_open_conns" toml:"max_open_conns"`
ConnMaxLifetime time.Duration `json:"conn_max_lifetime" toml:"conn_max_lifetime"`
ConnMaxIdleTime time.Duration `json:"conn_max_idle_time" toml:"conn_max_idle_time"`
ConnectTimeout time.Duration `json:"connect_timeout" toml:"connect_timeout"`
ReadTimeout time.Duration `json:"read_timeout" toml:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout" toml:"write_timeout"`
HealthCheckInterval time.Duration `json:"health_check_interval" toml:"health_check_interval"`
EnableQueryLog bool `json:"enable_query_log" toml:"enable_query_log"`
SlowQueryTime time.Duration `json:"slow_query_time" toml:"slow_query_time"`
AutoCreate bool `json:"auto_create" toml:"auto_create"`
Charset string `json:"charset" toml:"charset"` // MySQL/Dameng(MySQL mode):utf8mb4、Dameng(native):utf8、Postgres/Kingbase:UTF8
Template string `json:"template" toml:"template"`
}
ConnectionConfig describes how to connect to a database and tune its pool.
func DefaultConnectionConfig ¶
func DefaultConnectionConfig() *ConnectionConfig
DefaultConnectionConfig returns a connection config with sensible defaults.
type Engine ¶
type Engine interface {
Name() string
DriverName() string
BunDialect() schema.Dialect
QuoteIdent(name string) string
ClassifyError(err error) (bool, SQLError)
Upsert(ctx context.Context, db bun.IDB, model interface{}, fields []string, duplicateKeys []string, duplicateValues map[string]interface{}) error
}
Engine is the public database capability surface used outside the engine package. It exposes stable behavior needed by repositories and callers without leaking schema-sync internals such as column metadata structures.
func EngineForDB ¶
EngineForDB resolves the engine backing a live bun.DB from its dialect.
func MustEngine ¶
MustEngine is like GetEngine but panics for unknown database types.
type IndexSpec ¶
IndexSpec describes an index as declared by models or reported by database introspection.
type Logger ¶
type Logger interface {
SetLevel(LogLevel)
Debug(ctx context.Context, msg string, fields ...interface{})
Info(ctx context.Context, msg string, fields ...interface{})
Warn(ctx context.Context, msg string, fields ...interface{})
Error(ctx context.Context, msg string, fields ...interface{})
}
Logger is the logging contract the engine layer needs. The database package registers its global logger through SetLoggerFallback; any value with this method set (including database.Logger) satisfies it. All methods accept a context.Context for request tracing and cancellation.
type Migration ¶
type Migration struct {
bun.BaseModel `bun:"bun_migrations"`
Version string `bun:"version,pk"`
Name string `bun:"name"`
AppliedAt time.Time `bun:"applied_at"`
Description string `bun:"description"`
}
Migration records one applied migration / schema synchronization step in the bun_migrations bookkeeping table.
type SchemaEngine ¶
type SchemaEngine interface {
Engine
BuildDSN(cfg *ConnectionConfig) (string, error)
EnsureDatabase(ctx context.Context, cfg *ConnectionConfig, logger Logger) error
TableExists(ctx context.Context, db bun.IDB, table string) (bool, error)
ListTables(ctx context.Context, db bun.IDB) (map[string]struct{}, error)
ListColumns(ctx context.Context, db bun.IDB, table string) (map[string]ColumnSpec, error)
ListColumnsAll(ctx context.Context, db bun.IDB) (map[string]map[string]ColumnSpec, error)
ListColumnsSome(ctx context.Context, db bun.IDB, tables []string) (map[string]map[string]ColumnSpec, error)
ListIndexes(ctx context.Context, db bun.IDB, table string) ([]IndexSpec, error)
ListIndexesAll(ctx context.Context, db bun.IDB) (map[string][]IndexSpec, error)
ListIndexesSome(ctx context.Context, db bun.IDB, tables []string) (map[string][]IndexSpec, error)
AddColumnSQL(table string, c ColumnSpec) string
ModifyColumnSQL(table string, c ColumnSpec) string
RenameColumnSQL(table, oldName, newName string, c ColumnSpec) string
DropColumnSQL(table, col string) string
CreateIndexSQL(table string, idx IndexSpec) string
DropIndexSQL(table string, idx IndexSpec) string
RenameIndexSQL(table, oldName, newName string) string
NormalizeSQLType(typ string) string
NormalizeDefault(def string, nullable bool) string
NormalizeDeclaredColumnType(typ string) string
InferSQLType(rt reflect.Type) string
NeedsModification(desired, existing ColumnSpec) bool
// SupportsModifyAfterRename reports whether the planner should emit a
// separate ModifyColumn operation right after renaming a column. The
// PostgreSQL family needs it because RENAME COLUMN does not carry type
// or default changes; mysql's CHANGE COLUMN redeclares the full column
// definition itself, and dameng/sqlite keep the historical behavior of
// skipping it.
SupportsModifyAfterRename() bool
InsertMigrationRecord(ctx context.Context, db bun.IDB, rec *Migration) error
MigrationVersionExists(ctx context.Context, db bun.IDB, version string) (bool, error)
SelectMigrationVersions(ctx context.Context, db bun.IDB, prefix string) ([]string, error)
EnsureMigrationTableExists(ctx context.Context, db bun.IDB) error
}
SchemaEngine is the full extension point for database-specific behavior used by connection management, migrations, and schema synchronization.
func GetSchemaEngine ¶
func GetSchemaEngine(dbType string) (SchemaEngine, error)
GetSchemaEngine returns the full dialect engine for a database type.
func MustSchemaEngine ¶
func MustSchemaEngine(dbType string) SchemaEngine
MustSchemaEngine is like GetSchemaEngine but panics for unknown types.