dbcore

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const SystemVersionKey = "system_version"

SystemVersionKey 是记录“上次启动所用版本标识”的配置键(存于 configs 表)。 取代旧的 ./data/.komari-version 文件:版本标识随配置库一起备份/恢复, 也避免额外的裸文件依赖。

Variables

This section is empty.

Functions

func Close

func Close() error

Close 关闭底层数据库连接,供关闭流程调用。

func GetDBInstance

func GetDBInstance() *gorm.DB

GetDBInstance 返回全局数据库实例。 为兼容既有大量调用点,这里保留“出错即退出”的语义; 需要错误处理的启动流程应优先调用 Initialize()。

func Initialize

func Initialize() error

Initialize 显式初始化数据库连接与表结构,仅执行一次。 与 GetDBInstance 不同,Initialize 返回错误而非直接退出进程, 便于启动生命周期统一处理错误、以及在测试/CLI 命令中做隔离。

func ReadyDBInstance added in v0.1.9

func ReadyDBInstance() *gorm.DB

ReadyDBInstance 返回已经初始化好的数据库句柄;尚未初始化时返回 nil。

与 GetDBInstance 的区别是它【不会触发初始化】,因此也不会在初始化失败时 Fatalf。 给「数据库不可用也不该让功能失败」的可选数据用 —— 解锁探测结果就是这类:它由节点 单独上报,ip-info 的单元测试不建库,用 GetDBInstance 会让整个测试进程直接退出。

func ReclaimSpace

func ReclaimSpace(ctx context.Context) error

ReclaimSpace checkpoints the main database WAL and rewrites the SQLite file.

func SetVersionID

func SetVersionID(id string)

SetVersionID 设置当前构建的版本标识(通常为 CurrentVersion+"-"+VersionHash), 用于版本升级检测与自动备份。应在 Initialize() 之前调用;为空则跳过升级备份。

func StorageSize

func StorageSize() (int64, error)

StorageSize returns the bytes occupied by the main SQLite database and its WAL/SHM sidecar files.

Types

type MainDatabaseIntegrityReport added in v1.6.18

type MainDatabaseIntegrityReport struct {
	// QuickCheckErr is set when PRAGMA quick_check reported a problem, or when it could not run.
	QuickCheckErr error
	// MissingIndexes lists the authentication indexes the schema lacks, each with its impact.
	MissingIndexes []string
	// IndexCheckErr is set when the index check itself could not run. It does not mean an index
	// is missing.
	IndexCheckErr error
}

MainDatabaseIntegrityReport is the outcome of one periodic integrity check.

The fields stay separate instead of collapsing into one error: a structural problem (quick_check) and a schema problem (a missing index) have different consequences and different fixes, and the daily task logs them at different levels.

func RunMainDatabaseQuickCheck added in v1.6.18

func RunMainDatabaseQuickCheck() (MainDatabaseIntegrityReport, error)

RunMainDatabaseQuickCheck runs the integrity and authentication-index checks against the current main database. It is the body of the daily scheduled task registered in internal/server (registerScheduledWork).

Why a second entry point when startup already checks: the startup pass answers "what state did this process start on". A panel that has been running for weeks can end up in a state startup never saw — pages corrupted by the host, an index dropped by an external writer, a schema left half-migrated. One daily read is enough to notice all three.

It never panics and never initialises the database: when the handle is not ready it returns an error instead, so a scheduled tick cannot turn a not-yet-initialised panel into a FATAL. It does not log either — the caller chooses the level (Info when clean, Error when not), which keeps the daily and startup wording from drifting apart.

The call is synchronous and reads every page of the database; the scheduler runs it on its own goroutine, so the HTTP server never waits for it. On SQLite it also holds the single writer connection for the duration, which is the reason this runs daily rather than hourly.

func (MainDatabaseIntegrityReport) OK added in v1.6.18

OK reports whether the check found nothing worth logging.

type UpgradeArchivePruneResult added in v1.6.20

type UpgradeArchivePruneResult struct {
	// Dir is the directory that was swept.
	Dir string
	// Removed is how many old archives were deleted.
	Removed int
	// Remaining is how many upgrade-*.zip files are in the directory afterwards.
	Remaining int
	// Keep is the retention that was applied.
	Keep int
}

UpgradeArchivePruneResult is the outcome of one upgrade-archive sweep, for the caller's log line.

func RunUpgradeArchivePrune added in v1.6.20

func RunUpgradeArchivePrune() (UpgradeArchivePruneResult, error)

RunUpgradeArchivePrune applies the retention policy to the default archive directory (./data/backup) once and reports what it did.

Why it exists: the prune in backupOnVersionUpgrade only runs in the moment a new archive has been written. A panel that does not upgrade for months never takes that path, so its old archives stay forever — that is how 5.5 GB accumulated before v1.6.19, and v1.6.19 only guarantees the directory is trimmed at upgrade time. This entry point is for the daily scheduled task in internal/server.

It does not initialise the database, touch any database file, or keep global state: a missing directory simply has nothing to prune, which returns a zero result rather than an error (a panel that has never upgraded legitimately has no archives). Failures are returned for the caller to log as a warning — a failed cleanup must not affect the service.

Jump to

Keyboard shortcuts

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