database

package
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSQLitePath = "config/data/v2board.db"

DefaultSQLitePath is the SQLite database file used when none is configured.

Variables

This section is empty.

Functions

func AutoMigrate

func AutoMigrate(models ...any) error

AutoMigrate 自动迁移数据库表

func Close

func Close() error

Close 关闭数据库连接

func Get

func Get() *gorm.DB

Get 获取数据库实例

func GetDB

func GetDB() *gorm.DB

GetDB 获取数据库实例

func Init

func Init(cfg *config.DatabaseConfig) error

Init 初始化数据库连接

func PackageStorageSource added in v4.1.0

func PackageStorageSource(cfg *config.DatabaseConfig) (string, error)

PackageStorageSource returns what package storage derives package connections from: the kernel's PostgreSQL connection string, or the absolute path of the SQLite database file. It holds kernel credentials and must stay in the kernel.

func PostgresDSN added in v4.1.0

func PostgresDSN(cfg *config.DatabaseConfig) string

PostgresDSN builds a PostgreSQL keyword/value connection string from cfg.

Every value is single-quoted and escaped the way libpq and pgx expect (`\` becomes `\\`, `'` becomes `\'`), so empty values and values containing spaces, quotes, `=` or backslashes cannot swallow the next keyword. Without quoting, an empty password turned `password= dbname=x` into the password "dbname=x" and silently connected to the user's default database.

An empty Host and a Port <= 0 are omitted so the driver defaults apply (libpq and pgx: the local Unix socket directory or localhost, port 5432), matching libpq, which treats an empty host or port as unset.

The `sslmode` and `TimeZone` settings come first (defaults `disable` and `Asia/Shanghai`): gorm.io/driver/postgres also scans the raw DSN for the first `TimeZone=`, so a password that happens to contain that text cannot override the time zone.

A non-empty cfg.DSN is returned unchanged, for connection strings supplied whole by a secret store or database operator.

func Reset

func Reset()

Reset resets the database state (for testing only)

func SQLitePath added in v4.1.0

func SQLitePath(cfg *config.DatabaseConfig) string

SQLitePath returns the configured SQLite database file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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