db

package
v1.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migration

func Migration(sqlDir string, session *dbr.Session) error

func NewMySQL

func NewMySQL(addr string, sqlDir string, migration bool) *dbr.Session

NewMySQL 创建一个MySQL db,[path]db存储路径 [sqlDir]sql脚本目录

func NewRedis

func NewRedis(addr string, password string) *redis.Conn

NewRedis 兼容旧调用方,不会启用 TLS。 新代码应优先用 NewRedisFromConfig 以自动应用 TLS 等公共配置。

func NewRedisFromConfig added in v1.4.0

func NewRedisFromConfig(cfg *config.Config) *redis.Conn

NewRedisFromConfig 从 cfg 构造 Redis 连接,自动应用 RedisTLS / CA 等设置。

func NewSqlite

func NewSqlite(filepath string, sqlDir string) *dbr.Session

NewSqlite 创建一个sqlite db,[path]db存储路径 [sqlDir]sql脚本目录

func ReconcileThreadSchemaRecords

func ReconcileThreadSchemaRecords(ctx context.Context, db *sql.DB) error

ReconcileThreadSchemaRecords pre-seeds gorp_migrations with the thread module's migration IDs when the thread tables are already present from a prior snapshot-built install but the migration rows are missing. Without this, the next sql-migrate.Exec would try to apply CREATE TABLE `thread` (no IF NOT EXISTS) and panic with Error 1050.

Why a separate step from RewriteLegacyMigrationIDs: the legacy-ID rewrite is a 1:1 in-place rename, while this is a "the schema is here, please record that" reconciliation. Splitting them keeps each function's invariants narrow.

Idempotent on three axes:

  • fresh install (no thread tables): no-op.
  • already-reconciled install (rows present): no-op.
  • already-applied install (sql-migrate ran the migrations itself): no-op.

Call after RewriteLegacyMigrationIDs and before module.Setup.

func RewriteLegacyMigrationIDs

func RewriteLegacyMigrationIDs(ctx context.Context, db *sql.DB) error

RewriteLegacyMigrationIDs maps any legacy entries in `gorp_migrations.id` to their new timestamp-prefixed equivalents.

Why this exists: sql-migrate (rubenv/sql-migrate@v1.5.2 migrate.go:135-146) falls back to lexicographic `m.Id < other.Id` when filenames don't start with digits, so the historical `<module>-<YYYYMMDD>-<NN>.sql` scheme ordered migrations by module name first — which caused cross-module dependencies like `botfather-20260417-01.sql` (ALTERs `robot`) to run before `robot-20210926-01.sql` (CREATEs the table). The fix is to rename every file to a 14-digit timestamp prefix; the cost is that any already-applied database has the old IDs in `gorp_migrations` and would otherwise hit sql-migrate's "unknown migration in database" safety check.

This function is idempotent: it only rewrites rows whose old ID is present AND whose new ID is absent, and it leaves no trace on a fresh install (the table is empty, so the loop is a no-op).

Call this once at startup, before any call to migrate.Exec / module.Setup.

Types

type BaseModel

type BaseModel struct {
	Id        int64
	CreatedAt Time
	UpdatedAt Time
}

type FileDirMigrationSource

type FileDirMigrationSource struct {
	Dir string
}

FileDirMigrationSource 文件目录源 遇到目录进行递归获取

func (FileDirMigrationSource) FindMigrations

func (f FileDirMigrationSource) FindMigrations() ([]*migrate.Migration, error)

FindMigrations FindMigrations

type Time

type Time time.Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (Time) String

func (t Time) String() string

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL